isl29018.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/err.h>
  26. #include <linux/mutex.h>
  27. #include <linux/delay.h>
  28. #include <linux/regmap.h>
  29. #include <linux/slab.h>
  30. #include <linux/iio/iio.h>
  31. #include <linux/iio/sysfs.h>
  32. #include <linux/acpi.h>
  33. #define CONVERSION_TIME_MS 100
  34. #define ISL29018_REG_ADD_COMMAND1 0x00
  35. #define COMMMAND1_OPMODE_SHIFT 5
  36. #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
  37. #define COMMMAND1_OPMODE_POWER_DOWN 0
  38. #define COMMMAND1_OPMODE_ALS_ONCE 1
  39. #define COMMMAND1_OPMODE_IR_ONCE 2
  40. #define COMMMAND1_OPMODE_PROX_ONCE 3
  41. #define ISL29018_REG_ADD_COMMANDII 0x01
  42. #define COMMANDII_RESOLUTION_SHIFT 2
  43. #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
  44. #define COMMANDII_RANGE_SHIFT 0
  45. #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
  46. #define COMMANDII_SCHEME_SHIFT 7
  47. #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
  48. #define ISL29018_REG_ADD_DATA_LSB 0x02
  49. #define ISL29018_REG_ADD_DATA_MSB 0x03
  50. #define ISL29018_REG_TEST 0x08
  51. #define ISL29018_TEST_SHIFT 0
  52. #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
  53. #define ISL29035_REG_DEVICE_ID 0x0F
  54. #define ISL29035_DEVICE_ID_SHIFT 0x03
  55. #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
  56. #define ISL29035_DEVICE_ID 0x5
  57. #define ISL29035_BOUT_SHIFT 0x07
  58. #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
  59. #define ISL29018_INT_TIME_AVAIL "0.090000 0.005630 0.000351 0.000021"
  60. #define ISL29023_INT_TIME_AVAIL "0.090000 0.005600 0.000352 0.000022"
  61. #define ISL29035_INT_TIME_AVAIL "0.105000 0.006500 0.000410 0.000025"
  62. static const char * const int_time_avail[] = {
  63. ISL29018_INT_TIME_AVAIL,
  64. ISL29023_INT_TIME_AVAIL,
  65. ISL29035_INT_TIME_AVAIL,
  66. };
  67. enum isl29018_int_time {
  68. ISL29018_INT_TIME_16,
  69. ISL29018_INT_TIME_12,
  70. ISL29018_INT_TIME_8,
  71. ISL29018_INT_TIME_4,
  72. };
  73. static const unsigned int isl29018_int_utimes[3][4] = {
  74. {90000, 5630, 351, 21},
  75. {90000, 5600, 352, 22},
  76. {105000, 6500, 410, 25},
  77. };
  78. static const struct isl29018_scale {
  79. unsigned int scale;
  80. unsigned int uscale;
  81. } isl29018_scales[4][4] = {
  82. { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
  83. { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
  84. { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
  85. { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
  86. };
  87. struct isl29018_chip {
  88. struct device *dev;
  89. struct regmap *regmap;
  90. struct mutex lock;
  91. int type;
  92. unsigned int calibscale;
  93. unsigned int ucalibscale;
  94. unsigned int int_time;
  95. struct isl29018_scale scale;
  96. int prox_scheme;
  97. bool suspended;
  98. };
  99. static int isl29018_set_integration_time(struct isl29018_chip *chip,
  100. unsigned int utime)
  101. {
  102. int i, ret;
  103. unsigned int int_time, new_int_time;
  104. for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
  105. if (utime == isl29018_int_utimes[chip->type][i]) {
  106. new_int_time = i;
  107. break;
  108. }
  109. }
  110. if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
  111. return -EINVAL;
  112. ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
  113. COMMANDII_RESOLUTION_MASK,
  114. i << COMMANDII_RESOLUTION_SHIFT);
  115. if (ret < 0)
  116. return ret;
  117. /* keep the same range when integration time changes */
  118. int_time = chip->int_time;
  119. for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
  120. if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
  121. chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
  122. chip->scale = isl29018_scales[new_int_time][i];
  123. break;
  124. }
  125. }
  126. chip->int_time = new_int_time;
  127. return 0;
  128. }
  129. static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
  130. {
  131. int i, ret;
  132. struct isl29018_scale new_scale;
  133. for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
  134. if (scale == isl29018_scales[chip->int_time][i].scale &&
  135. uscale == isl29018_scales[chip->int_time][i].uscale) {
  136. new_scale = isl29018_scales[chip->int_time][i];
  137. break;
  138. }
  139. }
  140. if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
  141. return -EINVAL;
  142. ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
  143. COMMANDII_RANGE_MASK,
  144. i << COMMANDII_RANGE_SHIFT);
  145. if (ret < 0)
  146. return ret;
  147. chip->scale = new_scale;
  148. return 0;
  149. }
  150. static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
  151. {
  152. int status;
  153. unsigned int lsb;
  154. unsigned int msb;
  155. /* Set mode */
  156. status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
  157. mode << COMMMAND1_OPMODE_SHIFT);
  158. if (status) {
  159. dev_err(chip->dev,
  160. "Error in setting operating mode err %d\n", status);
  161. return status;
  162. }
  163. msleep(CONVERSION_TIME_MS);
  164. status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
  165. if (status < 0) {
  166. dev_err(chip->dev,
  167. "Error in reading LSB DATA with err %d\n", status);
  168. return status;
  169. }
  170. status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
  171. if (status < 0) {
  172. dev_err(chip->dev,
  173. "Error in reading MSB DATA with error %d\n", status);
  174. return status;
  175. }
  176. dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
  177. return (msb << 8) | lsb;
  178. }
  179. static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
  180. {
  181. int lux_data;
  182. unsigned int data_x_range;
  183. lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
  184. if (lux_data < 0)
  185. return lux_data;
  186. data_x_range = lux_data * chip->scale.scale +
  187. lux_data * chip->scale.uscale / 1000000;
  188. *lux = data_x_range * chip->calibscale +
  189. data_x_range * chip->ucalibscale / 1000000;
  190. return 0;
  191. }
  192. static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
  193. {
  194. int ir_data;
  195. ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
  196. if (ir_data < 0)
  197. return ir_data;
  198. *ir = ir_data;
  199. return 0;
  200. }
  201. static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
  202. int *near_ir)
  203. {
  204. int status;
  205. int prox_data = -1;
  206. int ir_data = -1;
  207. /* Do proximity sensing with required scheme */
  208. status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
  209. COMMANDII_SCHEME_MASK,
  210. scheme << COMMANDII_SCHEME_SHIFT);
  211. if (status) {
  212. dev_err(chip->dev, "Error in setting operating mode\n");
  213. return status;
  214. }
  215. prox_data = isl29018_read_sensor_input(chip,
  216. COMMMAND1_OPMODE_PROX_ONCE);
  217. if (prox_data < 0)
  218. return prox_data;
  219. if (scheme == 1) {
  220. *near_ir = prox_data;
  221. return 0;
  222. }
  223. ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
  224. if (ir_data < 0)
  225. return ir_data;
  226. if (prox_data >= ir_data)
  227. *near_ir = prox_data - ir_data;
  228. else
  229. *near_ir = 0;
  230. return 0;
  231. }
  232. static ssize_t show_scale_available(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  236. struct isl29018_chip *chip = iio_priv(indio_dev);
  237. int i, len = 0;
  238. for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
  239. len += sprintf(buf + len, "%d.%06d ",
  240. isl29018_scales[chip->int_time][i].scale,
  241. isl29018_scales[chip->int_time][i].uscale);
  242. buf[len - 1] = '\n';
  243. return len;
  244. }
  245. static ssize_t show_int_time_available(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  249. struct isl29018_chip *chip = iio_priv(indio_dev);
  250. int i, 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. /* proximity scheme */
  258. static ssize_t show_prox_infrared_suppression(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  262. struct isl29018_chip *chip = iio_priv(indio_dev);
  263. /* return the "proximity scheme" i.e. if the chip does on chip
  264. infrared suppression (1 means perform on chip suppression) */
  265. return sprintf(buf, "%d\n", chip->prox_scheme);
  266. }
  267. static ssize_t store_prox_infrared_suppression(struct device *dev,
  268. struct device_attribute *attr, const char *buf, size_t count)
  269. {
  270. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  271. struct isl29018_chip *chip = iio_priv(indio_dev);
  272. int val;
  273. if (kstrtoint(buf, 10, &val))
  274. return -EINVAL;
  275. if (!(val == 0 || val == 1)) {
  276. dev_err(dev, "The mode is not supported\n");
  277. return -EINVAL;
  278. }
  279. /* get the "proximity scheme" i.e. if the chip does on chip
  280. infrared suppression (1 means perform on chip suppression) */
  281. mutex_lock(&chip->lock);
  282. chip->prox_scheme = val;
  283. mutex_unlock(&chip->lock);
  284. return count;
  285. }
  286. /* Channel IO */
  287. static int isl29018_write_raw(struct iio_dev *indio_dev,
  288. struct iio_chan_spec const *chan,
  289. int val,
  290. int val2,
  291. long mask)
  292. {
  293. struct isl29018_chip *chip = iio_priv(indio_dev);
  294. int ret = -EINVAL;
  295. mutex_lock(&chip->lock);
  296. switch (mask) {
  297. case IIO_CHAN_INFO_CALIBSCALE:
  298. if (chan->type == IIO_LIGHT) {
  299. chip->calibscale = val;
  300. chip->ucalibscale = val2;
  301. ret = 0;
  302. }
  303. break;
  304. case IIO_CHAN_INFO_INT_TIME:
  305. if (chan->type == IIO_LIGHT) {
  306. if (val) {
  307. mutex_unlock(&chip->lock);
  308. return -EINVAL;
  309. }
  310. ret = isl29018_set_integration_time(chip, val2);
  311. }
  312. break;
  313. case IIO_CHAN_INFO_SCALE:
  314. if (chan->type == IIO_LIGHT)
  315. ret = isl29018_set_scale(chip, val, val2);
  316. break;
  317. default:
  318. break;
  319. }
  320. mutex_unlock(&chip->lock);
  321. return ret;
  322. }
  323. static int isl29018_read_raw(struct iio_dev *indio_dev,
  324. struct iio_chan_spec const *chan,
  325. int *val,
  326. int *val2,
  327. long mask)
  328. {
  329. int ret = -EINVAL;
  330. struct isl29018_chip *chip = iio_priv(indio_dev);
  331. mutex_lock(&chip->lock);
  332. if (chip->suspended) {
  333. mutex_unlock(&chip->lock);
  334. return -EBUSY;
  335. }
  336. switch (mask) {
  337. case IIO_CHAN_INFO_RAW:
  338. case IIO_CHAN_INFO_PROCESSED:
  339. switch (chan->type) {
  340. case IIO_LIGHT:
  341. ret = isl29018_read_lux(chip, val);
  342. break;
  343. case IIO_INTENSITY:
  344. ret = isl29018_read_ir(chip, val);
  345. break;
  346. case IIO_PROXIMITY:
  347. ret = isl29018_read_proximity_ir(chip,
  348. chip->prox_scheme, val);
  349. break;
  350. default:
  351. break;
  352. }
  353. if (!ret)
  354. ret = IIO_VAL_INT;
  355. break;
  356. case IIO_CHAN_INFO_INT_TIME:
  357. if (chan->type == IIO_LIGHT) {
  358. *val = 0;
  359. *val2 = isl29018_int_utimes[chip->type][chip->int_time];
  360. ret = IIO_VAL_INT_PLUS_MICRO;
  361. }
  362. break;
  363. case IIO_CHAN_INFO_SCALE:
  364. if (chan->type == IIO_LIGHT) {
  365. *val = chip->scale.scale;
  366. *val2 = chip->scale.uscale;
  367. ret = IIO_VAL_INT_PLUS_MICRO;
  368. }
  369. break;
  370. case IIO_CHAN_INFO_CALIBSCALE:
  371. if (chan->type == IIO_LIGHT) {
  372. *val = chip->calibscale;
  373. *val2 = chip->ucalibscale;
  374. ret = IIO_VAL_INT_PLUS_MICRO;
  375. }
  376. break;
  377. default:
  378. break;
  379. }
  380. mutex_unlock(&chip->lock);
  381. return ret;
  382. }
  383. #define ISL29018_LIGHT_CHANNEL { \
  384. .type = IIO_LIGHT, \
  385. .indexed = 1, \
  386. .channel = 0, \
  387. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
  388. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  389. BIT(IIO_CHAN_INFO_SCALE) | \
  390. BIT(IIO_CHAN_INFO_INT_TIME), \
  391. }
  392. #define ISL29018_IR_CHANNEL { \
  393. .type = IIO_INTENSITY, \
  394. .modified = 1, \
  395. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  396. .channel2 = IIO_MOD_LIGHT_IR, \
  397. }
  398. #define ISL29018_PROXIMITY_CHANNEL { \
  399. .type = IIO_PROXIMITY, \
  400. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  401. }
  402. static const struct iio_chan_spec isl29018_channels[] = {
  403. ISL29018_LIGHT_CHANNEL,
  404. ISL29018_IR_CHANNEL,
  405. ISL29018_PROXIMITY_CHANNEL,
  406. };
  407. static const struct iio_chan_spec isl29023_channels[] = {
  408. ISL29018_LIGHT_CHANNEL,
  409. ISL29018_IR_CHANNEL,
  410. };
  411. static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
  412. show_int_time_available, NULL, 0);
  413. static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
  414. show_scale_available, NULL, 0);
  415. static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
  416. S_IRUGO | S_IWUSR,
  417. show_prox_infrared_suppression,
  418. store_prox_infrared_suppression, 0);
  419. #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
  420. static struct attribute *isl29018_attributes[] = {
  421. ISL29018_DEV_ATTR(in_illuminance_scale_available),
  422. ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
  423. ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
  424. NULL
  425. };
  426. static struct attribute *isl29023_attributes[] = {
  427. ISL29018_DEV_ATTR(in_illuminance_scale_available),
  428. ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
  429. NULL
  430. };
  431. static const struct attribute_group isl29018_group = {
  432. .attrs = isl29018_attributes,
  433. };
  434. static const struct attribute_group isl29023_group = {
  435. .attrs = isl29023_attributes,
  436. };
  437. static int isl29035_detect(struct isl29018_chip *chip)
  438. {
  439. int status;
  440. unsigned int id;
  441. status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
  442. if (status < 0) {
  443. dev_err(chip->dev,
  444. "Error reading ID register with error %d\n",
  445. status);
  446. return status;
  447. }
  448. id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
  449. if (id != ISL29035_DEVICE_ID)
  450. return -ENODEV;
  451. /* clear out brownout bit */
  452. return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
  453. ISL29035_BOUT_MASK, 0);
  454. }
  455. enum {
  456. isl29018,
  457. isl29023,
  458. isl29035,
  459. };
  460. static int isl29018_chip_init(struct isl29018_chip *chip)
  461. {
  462. int status;
  463. if (chip->type == isl29035) {
  464. status = isl29035_detect(chip);
  465. if (status < 0)
  466. return status;
  467. }
  468. /* Code added per Intersil Application Note 1534:
  469. * When VDD sinks to approximately 1.8V or below, some of
  470. * the part's registers may change their state. When VDD
  471. * recovers to 2.25V (or greater), the part may thus be in an
  472. * unknown mode of operation. The user can return the part to
  473. * a known mode of operation either by (a) setting VDD = 0V for
  474. * 1 second or more and then powering back up with a slew rate
  475. * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
  476. * conversions, clear the test registers, and then rewrite all
  477. * registers to the desired values.
  478. * ...
  479. * FOR ISL29011, ISL29018, ISL29021, ISL29023
  480. * 1. Write 0x00 to register 0x08 (TEST)
  481. * 2. Write 0x00 to register 0x00 (CMD1)
  482. * 3. Rewrite all registers to the desired values
  483. *
  484. * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
  485. * the same thing EXCEPT the data sheet asks for a 1ms delay after
  486. * writing the CMD1 register.
  487. */
  488. status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
  489. if (status < 0) {
  490. dev_err(chip->dev, "Failed to clear isl29018 TEST reg.(%d)\n",
  491. status);
  492. return status;
  493. }
  494. /* See Intersil AN1534 comments above.
  495. * "Operating Mode" (COMMAND1) register is reprogrammed when
  496. * data is read from the device.
  497. */
  498. status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
  499. if (status < 0) {
  500. dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
  501. status);
  502. return status;
  503. }
  504. usleep_range(1000, 2000); /* per data sheet, page 10 */
  505. /* set defaults */
  506. status = isl29018_set_scale(chip, chip->scale.scale,
  507. chip->scale.uscale);
  508. if (status < 0) {
  509. dev_err(chip->dev, "Init of isl29018 fails\n");
  510. return status;
  511. }
  512. status = isl29018_set_integration_time(chip,
  513. isl29018_int_utimes[chip->type][chip->int_time]);
  514. if (status < 0) {
  515. dev_err(chip->dev, "Init of isl29018 fails\n");
  516. return status;
  517. }
  518. return 0;
  519. }
  520. static const struct iio_info isl29018_info = {
  521. .attrs = &isl29018_group,
  522. .driver_module = THIS_MODULE,
  523. .read_raw = &isl29018_read_raw,
  524. .write_raw = &isl29018_write_raw,
  525. };
  526. static const struct iio_info isl29023_info = {
  527. .attrs = &isl29023_group,
  528. .driver_module = THIS_MODULE,
  529. .read_raw = &isl29018_read_raw,
  530. .write_raw = &isl29018_write_raw,
  531. };
  532. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  533. {
  534. switch (reg) {
  535. case ISL29018_REG_ADD_DATA_LSB:
  536. case ISL29018_REG_ADD_DATA_MSB:
  537. case ISL29018_REG_ADD_COMMAND1:
  538. case ISL29018_REG_TEST:
  539. case ISL29035_REG_DEVICE_ID:
  540. return true;
  541. default:
  542. return false;
  543. }
  544. }
  545. /*
  546. * isl29018_regmap_config: regmap configuration.
  547. * Use RBTREE mechanism for caching.
  548. */
  549. static const struct regmap_config isl29018_regmap_config = {
  550. .reg_bits = 8,
  551. .val_bits = 8,
  552. .volatile_reg = is_volatile_reg,
  553. .max_register = ISL29018_REG_TEST,
  554. .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
  555. .cache_type = REGCACHE_RBTREE,
  556. };
  557. /* isl29035_regmap_config: regmap configuration for ISL29035 */
  558. static const struct regmap_config isl29035_regmap_config = {
  559. .reg_bits = 8,
  560. .val_bits = 8,
  561. .volatile_reg = 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 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 chip_info 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 int isl29018_probe(struct i2c_client *client,
  602. const struct i2c_device_id *id)
  603. {
  604. struct isl29018_chip *chip;
  605. struct iio_dev *indio_dev;
  606. int err;
  607. const char *name = NULL;
  608. int dev_id = 0;
  609. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  610. if (!indio_dev) {
  611. dev_err(&client->dev, "iio allocation fails\n");
  612. return -ENOMEM;
  613. }
  614. chip = iio_priv(indio_dev);
  615. i2c_set_clientdata(client, indio_dev);
  616. chip->dev = &client->dev;
  617. if (id) {
  618. name = id->name;
  619. dev_id = id->driver_data;
  620. }
  621. if (ACPI_HANDLE(&client->dev))
  622. name = isl29018_match_acpi_device(&client->dev, &dev_id);
  623. mutex_init(&chip->lock);
  624. chip->type = dev_id;
  625. chip->calibscale = 1;
  626. chip->ucalibscale = 0;
  627. chip->int_time = ISL29018_INT_TIME_16;
  628. chip->scale = isl29018_scales[chip->int_time][0];
  629. chip->suspended = false;
  630. chip->regmap = devm_regmap_init_i2c(client,
  631. chip_info_tbl[dev_id].regmap_cfg);
  632. if (IS_ERR(chip->regmap)) {
  633. err = PTR_ERR(chip->regmap);
  634. dev_err(chip->dev, "regmap initialization failed: %d\n", err);
  635. return err;
  636. }
  637. err = isl29018_chip_init(chip);
  638. if (err)
  639. return err;
  640. indio_dev->info = chip_info_tbl[dev_id].indio_info;
  641. indio_dev->channels = chip_info_tbl[dev_id].channels;
  642. indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
  643. indio_dev->name = name;
  644. indio_dev->dev.parent = &client->dev;
  645. indio_dev->modes = INDIO_DIRECT_MODE;
  646. err = devm_iio_device_register(&client->dev, indio_dev);
  647. if (err) {
  648. dev_err(&client->dev, "iio registration fails\n");
  649. return err;
  650. }
  651. return 0;
  652. }
  653. #ifdef CONFIG_PM_SLEEP
  654. static int isl29018_suspend(struct device *dev)
  655. {
  656. struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
  657. mutex_lock(&chip->lock);
  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. static const struct acpi_device_id isl29018_acpi_match[] = {
  683. {"ISL29018", isl29018},
  684. {"ISL29023", isl29023},
  685. {"ISL29035", isl29035},
  686. {},
  687. };
  688. MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
  689. static const struct i2c_device_id isl29018_id[] = {
  690. {"isl29018", isl29018},
  691. {"isl29023", isl29023},
  692. {"isl29035", isl29035},
  693. {}
  694. };
  695. MODULE_DEVICE_TABLE(i2c, isl29018_id);
  696. static const struct of_device_id isl29018_of_match[] = {
  697. { .compatible = "isil,isl29018", },
  698. { .compatible = "isil,isl29023", },
  699. { .compatible = "isil,isl29035", },
  700. { },
  701. };
  702. MODULE_DEVICE_TABLE(of, isl29018_of_match);
  703. static struct i2c_driver isl29018_driver = {
  704. .class = I2C_CLASS_HWMON,
  705. .driver = {
  706. .name = "isl29018",
  707. .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
  708. .pm = ISL29018_PM_OPS,
  709. .of_match_table = isl29018_of_match,
  710. },
  711. .probe = isl29018_probe,
  712. .id_table = isl29018_id,
  713. };
  714. module_i2c_driver(isl29018_driver);
  715. MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
  716. MODULE_LICENSE("GPL");