isl29018.c 21 KB

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