isl29018.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * A iio driver for the light sensor ISL 29018/29023/29035.
  4. *
  5. * IIO driver for monitoring ambient light intensity in luxi, proximity
  6. * sensing and infrared sensing.
  7. *
  8. * Copyright (c) 2010, NVIDIA Corporation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/err.h>
  13. #include <linux/mutex.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/acpi.h>
  21. #define ISL29018_CONV_TIME_MS 100
  22. #define ISL29018_REG_ADD_COMMAND1 0x00
  23. #define ISL29018_CMD1_OPMODE_SHIFT 5
  24. #define ISL29018_CMD1_OPMODE_MASK (7 << ISL29018_CMD1_OPMODE_SHIFT)
  25. #define ISL29018_CMD1_OPMODE_POWER_DOWN 0
  26. #define ISL29018_CMD1_OPMODE_ALS_ONCE 1
  27. #define ISL29018_CMD1_OPMODE_IR_ONCE 2
  28. #define ISL29018_CMD1_OPMODE_PROX_ONCE 3
  29. #define ISL29018_REG_ADD_COMMAND2 0x01
  30. #define ISL29018_CMD2_RESOLUTION_SHIFT 2
  31. #define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
  32. #define ISL29018_CMD2_RANGE_SHIFT 0
  33. #define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
  34. #define ISL29018_CMD2_SCHEME_SHIFT 7
  35. #define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
  36. #define ISL29018_REG_ADD_DATA_LSB 0x02
  37. #define ISL29018_REG_ADD_DATA_MSB 0x03
  38. #define ISL29018_REG_TEST 0x08
  39. #define ISL29018_TEST_SHIFT 0
  40. #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
  41. #define ISL29035_REG_DEVICE_ID 0x0F
  42. #define ISL29035_DEVICE_ID_SHIFT 0x03
  43. #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
  44. #define ISL29035_DEVICE_ID 0x5
  45. #define ISL29035_BOUT_SHIFT 0x07
  46. #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
  47. enum isl29018_int_time {
  48. ISL29018_INT_TIME_16,
  49. ISL29018_INT_TIME_12,
  50. ISL29018_INT_TIME_8,
  51. ISL29018_INT_TIME_4,
  52. };
  53. static const unsigned int isl29018_int_utimes[3][4] = {
  54. {90000, 5630, 351, 21},
  55. {90000, 5600, 352, 22},
  56. {105000, 6500, 410, 25},
  57. };
  58. static const struct isl29018_scale {
  59. unsigned int scale;
  60. unsigned int uscale;
  61. } isl29018_scales[4][4] = {
  62. { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
  63. { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
  64. { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
  65. { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
  66. };
  67. struct isl29018_chip {
  68. struct regmap *regmap;
  69. struct mutex lock;
  70. int type;
  71. unsigned int calibscale;
  72. unsigned int ucalibscale;
  73. unsigned int int_time;
  74. struct isl29018_scale scale;
  75. int prox_scheme;
  76. bool suspended;
  77. struct regulator *vcc_reg;
  78. };
  79. static int isl29018_set_integration_time(struct isl29018_chip *chip,
  80. unsigned int utime)
  81. {
  82. unsigned int i;
  83. int ret;
  84. unsigned int int_time, new_int_time;
  85. for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
  86. if (utime == isl29018_int_utimes[chip->type][i]) {
  87. new_int_time = i;
  88. break;
  89. }
  90. }
  91. if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
  92. return -EINVAL;
  93. ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
  94. ISL29018_CMD2_RESOLUTION_MASK,
  95. i << ISL29018_CMD2_RESOLUTION_SHIFT);
  96. if (ret < 0)
  97. return ret;
  98. /* Keep the same range when integration time changes */
  99. int_time = chip->int_time;
  100. for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
  101. if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
  102. chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
  103. chip->scale = isl29018_scales[new_int_time][i];
  104. break;
  105. }
  106. }
  107. chip->int_time = new_int_time;
  108. return 0;
  109. }
  110. static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
  111. {
  112. unsigned int i;
  113. int ret;
  114. struct isl29018_scale new_scale;
  115. for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
  116. if (scale == isl29018_scales[chip->int_time][i].scale &&
  117. uscale == isl29018_scales[chip->int_time][i].uscale) {
  118. new_scale = isl29018_scales[chip->int_time][i];
  119. break;
  120. }
  121. }
  122. if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
  123. return -EINVAL;
  124. ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
  125. ISL29018_CMD2_RANGE_MASK,
  126. i << ISL29018_CMD2_RANGE_SHIFT);
  127. if (ret < 0)
  128. return ret;
  129. chip->scale = new_scale;
  130. return 0;
  131. }
  132. static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
  133. {
  134. int status;
  135. unsigned int lsb;
  136. unsigned int msb;
  137. struct device *dev = regmap_get_device(chip->regmap);
  138. /* Set mode */
  139. status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
  140. mode << ISL29018_CMD1_OPMODE_SHIFT);
  141. if (status) {
  142. dev_err(dev,
  143. "Error in setting operating mode err %d\n", status);
  144. return status;
  145. }
  146. msleep(ISL29018_CONV_TIME_MS);
  147. status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
  148. if (status < 0) {
  149. dev_err(dev,
  150. "Error in reading LSB DATA with err %d\n", status);
  151. return status;
  152. }
  153. status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
  154. if (status < 0) {
  155. dev_err(dev,
  156. "Error in reading MSB DATA with error %d\n", status);
  157. return status;
  158. }
  159. dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
  160. return (msb << 8) | lsb;
  161. }
  162. static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
  163. {
  164. int lux_data;
  165. unsigned int data_x_range;
  166. lux_data = isl29018_read_sensor_input(chip,
  167. ISL29018_CMD1_OPMODE_ALS_ONCE);
  168. if (lux_data < 0)
  169. return lux_data;
  170. data_x_range = lux_data * chip->scale.scale +
  171. lux_data * chip->scale.uscale / 1000000;
  172. *lux = data_x_range * chip->calibscale +
  173. data_x_range * chip->ucalibscale / 1000000;
  174. return 0;
  175. }
  176. static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
  177. {
  178. int ir_data;
  179. ir_data = isl29018_read_sensor_input(chip,
  180. ISL29018_CMD1_OPMODE_IR_ONCE);
  181. if (ir_data < 0)
  182. return ir_data;
  183. *ir = ir_data;
  184. return 0;
  185. }
  186. static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
  187. int *near_ir)
  188. {
  189. int status;
  190. int prox_data = -1;
  191. int ir_data = -1;
  192. struct device *dev = regmap_get_device(chip->regmap);
  193. /* Do proximity sensing with required scheme */
  194. status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
  195. ISL29018_CMD2_SCHEME_MASK,
  196. scheme << ISL29018_CMD2_SCHEME_SHIFT);
  197. if (status) {
  198. dev_err(dev, "Error in setting operating mode\n");
  199. return status;
  200. }
  201. prox_data = isl29018_read_sensor_input(chip,
  202. ISL29018_CMD1_OPMODE_PROX_ONCE);
  203. if (prox_data < 0)
  204. return prox_data;
  205. if (scheme == 1) {
  206. *near_ir = prox_data;
  207. return 0;
  208. }
  209. ir_data = isl29018_read_sensor_input(chip,
  210. ISL29018_CMD1_OPMODE_IR_ONCE);
  211. if (ir_data < 0)
  212. return ir_data;
  213. if (prox_data >= ir_data)
  214. *near_ir = prox_data - ir_data;
  215. else
  216. *near_ir = 0;
  217. return 0;
  218. }
  219. static ssize_t in_illuminance_scale_available_show
  220. (struct device *dev, struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  224. struct isl29018_chip *chip = iio_priv(indio_dev);
  225. unsigned int i;
  226. int len = 0;
  227. mutex_lock(&chip->lock);
  228. for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
  229. len += sprintf(buf + len, "%d.%06d ",
  230. isl29018_scales[chip->int_time][i].scale,
  231. isl29018_scales[chip->int_time][i].uscale);
  232. mutex_unlock(&chip->lock);
  233. buf[len - 1] = '\n';
  234. return len;
  235. }
  236. static ssize_t in_illuminance_integration_time_available_show
  237. (struct device *dev, struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  241. struct isl29018_chip *chip = iio_priv(indio_dev);
  242. unsigned int i;
  243. int len = 0;
  244. for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
  245. len += sprintf(buf + len, "0.%06d ",
  246. isl29018_int_utimes[chip->type][i]);
  247. buf[len - 1] = '\n';
  248. return len;
  249. }
  250. /*
  251. * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
  252. * infrared suppression:
  253. *
  254. * Proximity Sensing Scheme: Bit 7. This bit programs the function
  255. * of the proximity detection. Logic 0 of this bit, Scheme 0, makes
  256. * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range
  257. * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit,
  258. * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary)
  259. * proximity_less_ambient detection. The range of Scheme 1
  260. * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended
  261. * for resolutions less than 16. While Scheme 0 has wider dynamic
  262. * range, Scheme 1 proximity detection is less affected by the
  263. * ambient IR noise variation.
  264. *
  265. * 0 Sensing IR from LED and ambient
  266. * 1 Sensing IR from LED with ambient IR rejection
  267. */
  268. static ssize_t proximity_on_chip_ambient_infrared_suppression_show
  269. (struct device *dev, struct device_attribute *attr,
  270. char *buf)
  271. {
  272. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  273. struct isl29018_chip *chip = iio_priv(indio_dev);
  274. /*
  275. * Return the "proximity scheme" i.e. if the chip does on chip
  276. * infrared suppression (1 means perform on chip suppression)
  277. */
  278. return sprintf(buf, "%d\n", chip->prox_scheme);
  279. }
  280. static ssize_t proximity_on_chip_ambient_infrared_suppression_store
  281. (struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  285. struct isl29018_chip *chip = iio_priv(indio_dev);
  286. int val;
  287. if (kstrtoint(buf, 10, &val))
  288. return -EINVAL;
  289. if (!(val == 0 || val == 1))
  290. return -EINVAL;
  291. /*
  292. * Get the "proximity scheme" i.e. if the chip does on chip
  293. * infrared suppression (1 means perform on chip suppression)
  294. */
  295. mutex_lock(&chip->lock);
  296. chip->prox_scheme = val;
  297. mutex_unlock(&chip->lock);
  298. return count;
  299. }
  300. static int isl29018_write_raw(struct iio_dev *indio_dev,
  301. struct iio_chan_spec const *chan,
  302. int val,
  303. int val2,
  304. long mask)
  305. {
  306. struct isl29018_chip *chip = iio_priv(indio_dev);
  307. int ret = -EINVAL;
  308. mutex_lock(&chip->lock);
  309. if (chip->suspended) {
  310. ret = -EBUSY;
  311. goto write_done;
  312. }
  313. switch (mask) {
  314. case IIO_CHAN_INFO_CALIBSCALE:
  315. if (chan->type == IIO_LIGHT) {
  316. chip->calibscale = val;
  317. chip->ucalibscale = val2;
  318. ret = 0;
  319. }
  320. break;
  321. case IIO_CHAN_INFO_INT_TIME:
  322. if (chan->type == IIO_LIGHT && !val)
  323. ret = isl29018_set_integration_time(chip, val2);
  324. break;
  325. case IIO_CHAN_INFO_SCALE:
  326. if (chan->type == IIO_LIGHT)
  327. ret = isl29018_set_scale(chip, val, val2);
  328. break;
  329. default:
  330. break;
  331. }
  332. write_done:
  333. mutex_unlock(&chip->lock);
  334. return ret;
  335. }
  336. static int isl29018_read_raw(struct iio_dev *indio_dev,
  337. struct iio_chan_spec const *chan,
  338. int *val,
  339. int *val2,
  340. long mask)
  341. {
  342. int ret = -EINVAL;
  343. struct isl29018_chip *chip = iio_priv(indio_dev);
  344. mutex_lock(&chip->lock);
  345. if (chip->suspended) {
  346. ret = -EBUSY;
  347. goto read_done;
  348. }
  349. switch (mask) {
  350. case IIO_CHAN_INFO_RAW:
  351. case IIO_CHAN_INFO_PROCESSED:
  352. switch (chan->type) {
  353. case IIO_LIGHT:
  354. ret = isl29018_read_lux(chip, val);
  355. break;
  356. case IIO_INTENSITY:
  357. ret = isl29018_read_ir(chip, val);
  358. break;
  359. case IIO_PROXIMITY:
  360. ret = isl29018_read_proximity_ir(chip,
  361. chip->prox_scheme,
  362. val);
  363. break;
  364. default:
  365. break;
  366. }
  367. if (!ret)
  368. ret = IIO_VAL_INT;
  369. break;
  370. case IIO_CHAN_INFO_INT_TIME:
  371. if (chan->type == IIO_LIGHT) {
  372. *val = 0;
  373. *val2 = isl29018_int_utimes[chip->type][chip->int_time];
  374. ret = IIO_VAL_INT_PLUS_MICRO;
  375. }
  376. break;
  377. case IIO_CHAN_INFO_SCALE:
  378. if (chan->type == IIO_LIGHT) {
  379. *val = chip->scale.scale;
  380. *val2 = chip->scale.uscale;
  381. ret = IIO_VAL_INT_PLUS_MICRO;
  382. }
  383. break;
  384. case IIO_CHAN_INFO_CALIBSCALE:
  385. if (chan->type == IIO_LIGHT) {
  386. *val = chip->calibscale;
  387. *val2 = chip->ucalibscale;
  388. ret = IIO_VAL_INT_PLUS_MICRO;
  389. }
  390. break;
  391. default:
  392. break;
  393. }
  394. read_done:
  395. mutex_unlock(&chip->lock);
  396. return ret;
  397. }
  398. #define ISL29018_LIGHT_CHANNEL { \
  399. .type = IIO_LIGHT, \
  400. .indexed = 1, \
  401. .channel = 0, \
  402. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
  403. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  404. BIT(IIO_CHAN_INFO_SCALE) | \
  405. BIT(IIO_CHAN_INFO_INT_TIME), \
  406. }
  407. #define ISL29018_IR_CHANNEL { \
  408. .type = IIO_INTENSITY, \
  409. .modified = 1, \
  410. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  411. .channel2 = IIO_MOD_LIGHT_IR, \
  412. }
  413. #define ISL29018_PROXIMITY_CHANNEL { \
  414. .type = IIO_PROXIMITY, \
  415. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  416. }
  417. static const struct iio_chan_spec isl29018_channels[] = {
  418. ISL29018_LIGHT_CHANNEL,
  419. ISL29018_IR_CHANNEL,
  420. ISL29018_PROXIMITY_CHANNEL,
  421. };
  422. static const struct iio_chan_spec isl29023_channels[] = {
  423. ISL29018_LIGHT_CHANNEL,
  424. ISL29018_IR_CHANNEL,
  425. };
  426. static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
  427. static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
  428. static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
  429. #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
  430. static struct attribute *isl29018_attributes[] = {
  431. ISL29018_DEV_ATTR(in_illuminance_scale_available),
  432. ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
  433. ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
  434. NULL
  435. };
  436. static struct attribute *isl29023_attributes[] = {
  437. ISL29018_DEV_ATTR(in_illuminance_scale_available),
  438. ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
  439. NULL
  440. };
  441. static const struct attribute_group isl29018_group = {
  442. .attrs = isl29018_attributes,
  443. };
  444. static const struct attribute_group isl29023_group = {
  445. .attrs = isl29023_attributes,
  446. };
  447. enum {
  448. isl29018,
  449. isl29023,
  450. isl29035,
  451. };
  452. static int isl29018_chip_init(struct isl29018_chip *chip)
  453. {
  454. int status;
  455. struct device *dev = regmap_get_device(chip->regmap);
  456. if (chip->type == isl29035) {
  457. unsigned int id;
  458. status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
  459. if (status < 0) {
  460. dev_err(dev,
  461. "Error reading ID register with error %d\n",
  462. status);
  463. return status;
  464. }
  465. id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
  466. if (id != ISL29035_DEVICE_ID)
  467. return -ENODEV;
  468. /* Clear brownout bit */
  469. status = regmap_clear_bits(chip->regmap,
  470. ISL29035_REG_DEVICE_ID,
  471. ISL29035_BOUT_MASK);
  472. if (status < 0)
  473. return status;
  474. }
  475. /*
  476. * Code added per Intersil Application Note 1534:
  477. * When VDD sinks to approximately 1.8V or below, some of
  478. * the part's registers may change their state. When VDD
  479. * recovers to 2.25V (or greater), the part may thus be in an
  480. * unknown mode of operation. The user can return the part to
  481. * a known mode of operation either by (a) setting VDD = 0V for
  482. * 1 second or more and then powering back up with a slew rate
  483. * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
  484. * conversions, clear the test registers, and then rewrite all
  485. * registers to the desired values.
  486. * ...
  487. * For ISL29011, ISL29018, ISL29021, ISL29023
  488. * 1. Write 0x00 to register 0x08 (TEST)
  489. * 2. Write 0x00 to register 0x00 (CMD1)
  490. * 3. Rewrite all registers to the desired values
  491. *
  492. * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
  493. * the same thing EXCEPT the data sheet asks for a 1ms delay after
  494. * writing the CMD1 register.
  495. */
  496. status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
  497. if (status < 0) {
  498. dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
  499. status);
  500. return status;
  501. }
  502. /*
  503. * See Intersil AN1534 comments above.
  504. * "Operating Mode" (COMMAND1) register is reprogrammed when
  505. * data is read from the device.
  506. */
  507. status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
  508. if (status < 0) {
  509. dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
  510. status);
  511. return status;
  512. }
  513. usleep_range(1000, 2000); /* per data sheet, page 10 */
  514. /* Set defaults */
  515. status = isl29018_set_scale(chip, chip->scale.scale,
  516. chip->scale.uscale);
  517. if (status < 0) {
  518. dev_err(dev, "Init of isl29018 fails\n");
  519. return status;
  520. }
  521. status = isl29018_set_integration_time(chip,
  522. isl29018_int_utimes[chip->type][chip->int_time]);
  523. if (status < 0)
  524. dev_err(dev, "Init of isl29018 fails\n");
  525. return status;
  526. }
  527. static const struct iio_info isl29018_info = {
  528. .attrs = &isl29018_group,
  529. .read_raw = isl29018_read_raw,
  530. .write_raw = isl29018_write_raw,
  531. };
  532. static const struct iio_info isl29023_info = {
  533. .attrs = &isl29023_group,
  534. .read_raw = isl29018_read_raw,
  535. .write_raw = isl29018_write_raw,
  536. };
  537. static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
  538. {
  539. switch (reg) {
  540. case ISL29018_REG_ADD_DATA_LSB:
  541. case ISL29018_REG_ADD_DATA_MSB:
  542. case ISL29018_REG_ADD_COMMAND1:
  543. case ISL29018_REG_TEST:
  544. case ISL29035_REG_DEVICE_ID:
  545. return true;
  546. default:
  547. return false;
  548. }
  549. }
  550. static const struct regmap_config isl29018_regmap_config = {
  551. .reg_bits = 8,
  552. .val_bits = 8,
  553. .volatile_reg = isl29018_is_volatile_reg,
  554. .max_register = ISL29018_REG_TEST,
  555. .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
  556. .cache_type = REGCACHE_RBTREE,
  557. };
  558. static const struct regmap_config isl29035_regmap_config = {
  559. .reg_bits = 8,
  560. .val_bits = 8,
  561. .volatile_reg = isl29018_is_volatile_reg,
  562. .max_register = ISL29035_REG_DEVICE_ID,
  563. .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
  564. .cache_type = REGCACHE_RBTREE,
  565. };
  566. struct isl29018_chip_info {
  567. const struct iio_chan_spec *channels;
  568. int num_channels;
  569. const struct iio_info *indio_info;
  570. const struct regmap_config *regmap_cfg;
  571. };
  572. static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
  573. [isl29018] = {
  574. .channels = isl29018_channels,
  575. .num_channels = ARRAY_SIZE(isl29018_channels),
  576. .indio_info = &isl29018_info,
  577. .regmap_cfg = &isl29018_regmap_config,
  578. },
  579. [isl29023] = {
  580. .channels = isl29023_channels,
  581. .num_channels = ARRAY_SIZE(isl29023_channels),
  582. .indio_info = &isl29023_info,
  583. .regmap_cfg = &isl29018_regmap_config,
  584. },
  585. [isl29035] = {
  586. .channels = isl29023_channels,
  587. .num_channels = ARRAY_SIZE(isl29023_channels),
  588. .indio_info = &isl29023_info,
  589. .regmap_cfg = &isl29035_regmap_config,
  590. },
  591. };
  592. static const char *isl29018_match_acpi_device(struct device *dev, int *data)
  593. {
  594. const struct acpi_device_id *id;
  595. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  596. if (!id)
  597. return NULL;
  598. *data = (int)id->driver_data;
  599. return dev_name(dev);
  600. }
  601. static void isl29018_disable_regulator_action(void *_data)
  602. {
  603. struct isl29018_chip *chip = _data;
  604. int err;
  605. err = regulator_disable(chip->vcc_reg);
  606. if (err)
  607. pr_err("failed to disable isl29018's VCC regulator!\n");
  608. }
  609. static int isl29018_probe(struct i2c_client *client)
  610. {
  611. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  612. struct isl29018_chip *chip;
  613. struct iio_dev *indio_dev;
  614. int err;
  615. const char *name = NULL;
  616. int dev_id = 0;
  617. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  618. if (!indio_dev)
  619. return -ENOMEM;
  620. chip = iio_priv(indio_dev);
  621. i2c_set_clientdata(client, indio_dev);
  622. if (id) {
  623. name = id->name;
  624. dev_id = id->driver_data;
  625. }
  626. if (ACPI_HANDLE(&client->dev))
  627. name = isl29018_match_acpi_device(&client->dev, &dev_id);
  628. mutex_init(&chip->lock);
  629. chip->type = dev_id;
  630. chip->calibscale = 1;
  631. chip->ucalibscale = 0;
  632. chip->int_time = ISL29018_INT_TIME_16;
  633. chip->scale = isl29018_scales[chip->int_time][0];
  634. chip->suspended = false;
  635. chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
  636. if (IS_ERR(chip->vcc_reg))
  637. return dev_err_probe(&client->dev, PTR_ERR(chip->vcc_reg),
  638. "failed to get VCC regulator!\n");
  639. err = regulator_enable(chip->vcc_reg);
  640. if (err) {
  641. dev_err(&client->dev, "failed to enable VCC regulator!\n");
  642. return err;
  643. }
  644. err = devm_add_action_or_reset(&client->dev, isl29018_disable_regulator_action,
  645. chip);
  646. if (err) {
  647. dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
  648. return err;
  649. }
  650. chip->regmap = devm_regmap_init_i2c(client,
  651. isl29018_chip_info_tbl[dev_id].regmap_cfg);
  652. if (IS_ERR(chip->regmap)) {
  653. err = PTR_ERR(chip->regmap);
  654. dev_err(&client->dev, "regmap initialization fails: %d\n", err);
  655. return err;
  656. }
  657. err = isl29018_chip_init(chip);
  658. if (err)
  659. return err;
  660. indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
  661. indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
  662. indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
  663. indio_dev->name = name;
  664. indio_dev->modes = INDIO_DIRECT_MODE;
  665. return devm_iio_device_register(&client->dev, indio_dev);
  666. }
  667. static int isl29018_suspend(struct device *dev)
  668. {
  669. struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
  670. int ret;
  671. mutex_lock(&chip->lock);
  672. /*
  673. * Since this driver uses only polling commands, we are by default in
  674. * auto shutdown (ie, power-down) mode.
  675. * So we do not have much to do here.
  676. */
  677. chip->suspended = true;
  678. ret = regulator_disable(chip->vcc_reg);
  679. if (ret)
  680. dev_err(dev, "failed to disable VCC regulator\n");
  681. mutex_unlock(&chip->lock);
  682. return ret;
  683. }
  684. static int isl29018_resume(struct device *dev)
  685. {
  686. struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
  687. int err;
  688. mutex_lock(&chip->lock);
  689. err = regulator_enable(chip->vcc_reg);
  690. if (err) {
  691. dev_err(dev, "failed to enable VCC regulator\n");
  692. mutex_unlock(&chip->lock);
  693. return err;
  694. }
  695. err = isl29018_chip_init(chip);
  696. if (!err)
  697. chip->suspended = false;
  698. mutex_unlock(&chip->lock);
  699. return err;
  700. }
  701. static DEFINE_SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend,
  702. isl29018_resume);
  703. #ifdef CONFIG_ACPI
  704. static const struct acpi_device_id isl29018_acpi_match[] = {
  705. {"ISL29018", isl29018},
  706. {"ISL29023", isl29023},
  707. {"ISL29035", isl29035},
  708. {},
  709. };
  710. MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
  711. #endif
  712. static const struct i2c_device_id isl29018_id[] = {
  713. {"isl29018", isl29018},
  714. {"isl29023", isl29023},
  715. {"isl29035", isl29035},
  716. {}
  717. };
  718. MODULE_DEVICE_TABLE(i2c, isl29018_id);
  719. static const struct of_device_id isl29018_of_match[] = {
  720. { .compatible = "isil,isl29018", },
  721. { .compatible = "isil,isl29023", },
  722. { .compatible = "isil,isl29035", },
  723. { },
  724. };
  725. MODULE_DEVICE_TABLE(of, isl29018_of_match);
  726. static struct i2c_driver isl29018_driver = {
  727. .driver = {
  728. .name = "isl29018",
  729. .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
  730. .pm = pm_sleep_ptr(&isl29018_pm_ops),
  731. .of_match_table = isl29018_of_match,
  732. },
  733. .probe = isl29018_probe,
  734. .id_table = isl29018_id,
  735. };
  736. module_i2c_driver(isl29018_driver);
  737. MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
  738. MODULE_LICENSE("GPL");