tsl2583.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * Device driver for monitoring ambient light intensity (lux)
  3. * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
  4. *
  5. * Copyright (c) 2011, TAOS Corporation.
  6. * Copyright (c) 2016 Brian Masney <masneyb@onstation.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/i2c.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/string.h>
  23. #include <linux/mutex.h>
  24. #include <linux/unistd.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. /* Device Registers and Masks */
  30. #define TSL2583_CNTRL 0x00
  31. #define TSL2583_ALS_TIME 0X01
  32. #define TSL2583_INTERRUPT 0x02
  33. #define TSL2583_GAIN 0x07
  34. #define TSL2583_REVID 0x11
  35. #define TSL2583_CHIPID 0x12
  36. #define TSL2583_ALS_CHAN0LO 0x14
  37. #define TSL2583_ALS_CHAN0HI 0x15
  38. #define TSL2583_ALS_CHAN1LO 0x16
  39. #define TSL2583_ALS_CHAN1HI 0x17
  40. #define TSL2583_TMR_LO 0x18
  41. #define TSL2583_TMR_HI 0x19
  42. /* tsl2583 cmd reg masks */
  43. #define TSL2583_CMD_REG 0x80
  44. #define TSL2583_CMD_SPL_FN 0x60
  45. #define TSL2583_CMD_ALS_INT_CLR 0x01
  46. /* tsl2583 cntrl reg masks */
  47. #define TSL2583_CNTL_ADC_ENBL 0x02
  48. #define TSL2583_CNTL_PWR_OFF 0x00
  49. #define TSL2583_CNTL_PWR_ON 0x01
  50. /* tsl2583 status reg masks */
  51. #define TSL2583_STA_ADC_VALID 0x01
  52. #define TSL2583_STA_ADC_INTR 0x10
  53. /* Lux calculation constants */
  54. #define TSL2583_LUX_CALC_OVER_FLOW 65535
  55. #define TSL2583_INTERRUPT_DISABLED 0x00
  56. #define TSL2583_CHIP_ID 0x90
  57. #define TSL2583_CHIP_ID_MASK 0xf0
  58. /* Per-device data */
  59. struct tsl2583_als_info {
  60. u16 als_ch0;
  61. u16 als_ch1;
  62. u16 lux;
  63. };
  64. struct tsl2583_lux {
  65. unsigned int ratio;
  66. unsigned int ch0;
  67. unsigned int ch1;
  68. };
  69. static const struct tsl2583_lux tsl2583_default_lux[] = {
  70. { 9830, 8520, 15729 },
  71. { 12452, 10807, 23344 },
  72. { 14746, 6383, 11705 },
  73. { 17695, 4063, 6554 },
  74. { 0, 0, 0 } /* Termination segment */
  75. };
  76. #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
  77. struct tsl2583_settings {
  78. int als_time;
  79. int als_gain;
  80. int als_gain_trim;
  81. int als_cal_target;
  82. /*
  83. * This structure is intentionally large to accommodate updates via
  84. * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
  85. * Assumption is that one and only one type of glass used.
  86. */
  87. struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
  88. };
  89. struct tsl2583_chip {
  90. struct mutex als_mutex;
  91. struct i2c_client *client;
  92. struct tsl2583_als_info als_cur_info;
  93. struct tsl2583_settings als_settings;
  94. int als_time_scale;
  95. int als_saturation;
  96. bool suspended;
  97. };
  98. struct gainadj {
  99. s16 ch0;
  100. s16 ch1;
  101. s16 mean;
  102. };
  103. /* Index = (0 - 3) Used to validate the gain selection index */
  104. static const struct gainadj gainadj[] = {
  105. { 1, 1, 1 },
  106. { 8, 8, 8 },
  107. { 16, 16, 16 },
  108. { 107, 115, 111 }
  109. };
  110. /*
  111. * Provides initial operational parameter defaults.
  112. * These defaults may be changed through the device's sysfs files.
  113. */
  114. static void tsl2583_defaults(struct tsl2583_chip *chip)
  115. {
  116. /*
  117. * The integration time must be a multiple of 50ms and within the
  118. * range [50, 600] ms.
  119. */
  120. chip->als_settings.als_time = 100;
  121. /*
  122. * This is an index into the gainadj table. Assume clear glass as the
  123. * default.
  124. */
  125. chip->als_settings.als_gain = 0;
  126. /* Default gain trim to account for aperture effects */
  127. chip->als_settings.als_gain_trim = 1000;
  128. /* Known external ALS reading used for calibration */
  129. chip->als_settings.als_cal_target = 130;
  130. /* Default lux table. */
  131. memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
  132. sizeof(tsl2583_default_lux));
  133. }
  134. /*
  135. * Reads and calculates current lux value.
  136. * The raw ch0 and ch1 values of the ambient light sensed in the last
  137. * integration cycle are read from the device.
  138. * Time scale factor array values are adjusted based on the integration time.
  139. * The raw values are multiplied by a scale factor, and device gain is obtained
  140. * using gain index. Limit checks are done next, then the ratio of a multiple
  141. * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
  142. * declared above is then scanned to find the first ratio value that is just
  143. * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
  144. * the array are then used along with the time scale factor array values, to
  145. * calculate the lux.
  146. */
  147. static int tsl2583_get_lux(struct iio_dev *indio_dev)
  148. {
  149. u16 ch0, ch1; /* separated ch0/ch1 data from device */
  150. u32 lux; /* raw lux calculated from device data */
  151. u64 lux64;
  152. u32 ratio;
  153. u8 buf[5];
  154. struct tsl2583_lux *p;
  155. struct tsl2583_chip *chip = iio_priv(indio_dev);
  156. int i, ret;
  157. ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
  158. if (ret < 0) {
  159. dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
  160. __func__);
  161. goto done;
  162. }
  163. /* is data new & valid */
  164. if (!(ret & TSL2583_STA_ADC_INTR)) {
  165. dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
  166. __func__);
  167. ret = chip->als_cur_info.lux; /* return LAST VALUE */
  168. goto done;
  169. }
  170. for (i = 0; i < 4; i++) {
  171. int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
  172. ret = i2c_smbus_read_byte_data(chip->client, reg);
  173. if (ret < 0) {
  174. dev_err(&chip->client->dev, "%s: failed to read register %x\n",
  175. __func__, reg);
  176. goto done;
  177. }
  178. buf[i] = ret;
  179. }
  180. /*
  181. * Clear the pending interrupt status bit on the chip to allow the next
  182. * integration cycle to start. This has to be done even though this
  183. * driver currently does not support interrupts.
  184. */
  185. ret = i2c_smbus_write_byte(chip->client,
  186. (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
  187. TSL2583_CMD_ALS_INT_CLR));
  188. if (ret < 0) {
  189. dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
  190. __func__);
  191. goto done; /* have no data, so return failure */
  192. }
  193. /* extract ALS/lux data */
  194. ch0 = le16_to_cpup((const __le16 *)&buf[0]);
  195. ch1 = le16_to_cpup((const __le16 *)&buf[2]);
  196. chip->als_cur_info.als_ch0 = ch0;
  197. chip->als_cur_info.als_ch1 = ch1;
  198. if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
  199. goto return_max;
  200. if (!ch0) {
  201. /*
  202. * The sensor appears to be in total darkness so set the
  203. * calculated lux to 0 and return early to avoid a division by
  204. * zero below when calculating the ratio.
  205. */
  206. ret = 0;
  207. chip->als_cur_info.lux = 0;
  208. goto done;
  209. }
  210. /* calculate ratio */
  211. ratio = (ch1 << 15) / ch0;
  212. /* convert to unscaled lux using the pointer to the table */
  213. for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
  214. p->ratio != 0 && p->ratio < ratio; p++)
  215. ;
  216. if (p->ratio == 0) {
  217. lux = 0;
  218. } else {
  219. u32 ch0lux, ch1lux;
  220. ch0lux = ((ch0 * p->ch0) +
  221. (gainadj[chip->als_settings.als_gain].ch0 >> 1))
  222. / gainadj[chip->als_settings.als_gain].ch0;
  223. ch1lux = ((ch1 * p->ch1) +
  224. (gainadj[chip->als_settings.als_gain].ch1 >> 1))
  225. / gainadj[chip->als_settings.als_gain].ch1;
  226. /* note: lux is 31 bit max at this point */
  227. if (ch1lux > ch0lux) {
  228. dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
  229. __func__);
  230. ret = 0;
  231. chip->als_cur_info.lux = 0;
  232. goto done;
  233. }
  234. lux = ch0lux - ch1lux;
  235. }
  236. /* adjust for active time scale */
  237. if (chip->als_time_scale == 0)
  238. lux = 0;
  239. else
  240. lux = (lux + (chip->als_time_scale >> 1)) /
  241. chip->als_time_scale;
  242. /*
  243. * Adjust for active gain scale.
  244. * The tsl2583_default_lux tables above have a factor of 8192 built in,
  245. * so we need to shift right.
  246. * User-specified gain provides a multiplier.
  247. * Apply user-specified gain before shifting right to retain precision.
  248. * Use 64 bits to avoid overflow on multiplication.
  249. * Then go back to 32 bits before division to avoid using div_u64().
  250. */
  251. lux64 = lux;
  252. lux64 = lux64 * chip->als_settings.als_gain_trim;
  253. lux64 >>= 13;
  254. lux = lux64;
  255. lux = (lux + 500) / 1000;
  256. if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
  257. return_max:
  258. lux = TSL2583_LUX_CALC_OVER_FLOW;
  259. }
  260. /* Update the structure with the latest VALID lux. */
  261. chip->als_cur_info.lux = lux;
  262. ret = lux;
  263. done:
  264. return ret;
  265. }
  266. /*
  267. * Obtain single reading and calculate the als_gain_trim (later used
  268. * to derive actual lux).
  269. * Return updated gain_trim value.
  270. */
  271. static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
  272. {
  273. struct tsl2583_chip *chip = iio_priv(indio_dev);
  274. unsigned int gain_trim_val;
  275. int ret;
  276. int lux_val;
  277. ret = i2c_smbus_read_byte_data(chip->client,
  278. TSL2583_CMD_REG | TSL2583_CNTRL);
  279. if (ret < 0) {
  280. dev_err(&chip->client->dev,
  281. "%s: failed to read from the CNTRL register\n",
  282. __func__);
  283. return ret;
  284. }
  285. if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
  286. != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
  287. dev_err(&chip->client->dev,
  288. "%s: Device is not powered on and/or ADC is not enabled\n",
  289. __func__);
  290. return -EINVAL;
  291. } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
  292. dev_err(&chip->client->dev,
  293. "%s: The two ADC channels have not completed an integration cycle\n",
  294. __func__);
  295. return -ENODATA;
  296. }
  297. lux_val = tsl2583_get_lux(indio_dev);
  298. if (lux_val < 0) {
  299. dev_err(&chip->client->dev, "%s: failed to get lux\n",
  300. __func__);
  301. return lux_val;
  302. }
  303. gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
  304. * chip->als_settings.als_gain_trim) / lux_val);
  305. if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
  306. dev_err(&chip->client->dev,
  307. "%s: trim_val of %d is not within the range [250, 4000]\n",
  308. __func__, gain_trim_val);
  309. return -ENODATA;
  310. }
  311. chip->als_settings.als_gain_trim = (int)gain_trim_val;
  312. return 0;
  313. }
  314. static int tsl2583_set_als_time(struct tsl2583_chip *chip)
  315. {
  316. int als_count, als_time, ret;
  317. u8 val;
  318. /* determine als integration register */
  319. als_count = (chip->als_settings.als_time * 100 + 135) / 270;
  320. if (!als_count)
  321. als_count = 1; /* ensure at least one cycle */
  322. /* convert back to time (encompasses overrides) */
  323. als_time = (als_count * 27 + 5) / 10;
  324. val = 256 - als_count;
  325. ret = i2c_smbus_write_byte_data(chip->client,
  326. TSL2583_CMD_REG | TSL2583_ALS_TIME,
  327. val);
  328. if (ret < 0) {
  329. dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
  330. __func__, val);
  331. return ret;
  332. }
  333. /* set chip struct re scaling and saturation */
  334. chip->als_saturation = als_count * 922; /* 90% of full scale */
  335. chip->als_time_scale = (als_time + 25) / 50;
  336. return ret;
  337. }
  338. static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
  339. {
  340. int ret;
  341. /* Set the gain based on als_settings struct */
  342. ret = i2c_smbus_write_byte_data(chip->client,
  343. TSL2583_CMD_REG | TSL2583_GAIN,
  344. chip->als_settings.als_gain);
  345. if (ret < 0)
  346. dev_err(&chip->client->dev,
  347. "%s: failed to set the gain to %d\n", __func__,
  348. chip->als_settings.als_gain);
  349. return ret;
  350. }
  351. static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
  352. {
  353. int ret;
  354. ret = i2c_smbus_write_byte_data(chip->client,
  355. TSL2583_CMD_REG | TSL2583_CNTRL, state);
  356. if (ret < 0)
  357. dev_err(&chip->client->dev,
  358. "%s: failed to set the power state to %d\n", __func__,
  359. state);
  360. return ret;
  361. }
  362. /*
  363. * Turn the device on.
  364. * Configuration must be set before calling this function.
  365. */
  366. static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
  367. {
  368. struct tsl2583_chip *chip = iio_priv(indio_dev);
  369. int ret;
  370. /* Power on the device; ADC off. */
  371. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
  372. if (ret < 0)
  373. return ret;
  374. ret = i2c_smbus_write_byte_data(chip->client,
  375. TSL2583_CMD_REG | TSL2583_INTERRUPT,
  376. TSL2583_INTERRUPT_DISABLED);
  377. if (ret < 0) {
  378. dev_err(&chip->client->dev,
  379. "%s: failed to disable interrupts\n", __func__);
  380. return ret;
  381. }
  382. ret = tsl2583_set_als_time(chip);
  383. if (ret < 0)
  384. return ret;
  385. ret = tsl2583_set_als_gain(chip);
  386. if (ret < 0)
  387. return ret;
  388. usleep_range(3000, 3500);
  389. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
  390. TSL2583_CNTL_ADC_ENBL);
  391. if (ret < 0)
  392. return ret;
  393. chip->suspended = false;
  394. return ret;
  395. }
  396. /* Sysfs Interface Functions */
  397. static ssize_t in_illuminance_input_target_show(struct device *dev,
  398. struct device_attribute *attr,
  399. char *buf)
  400. {
  401. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  402. struct tsl2583_chip *chip = iio_priv(indio_dev);
  403. int ret;
  404. mutex_lock(&chip->als_mutex);
  405. ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
  406. mutex_unlock(&chip->als_mutex);
  407. return ret;
  408. }
  409. static ssize_t in_illuminance_input_target_store(struct device *dev,
  410. struct device_attribute *attr,
  411. const char *buf, size_t len)
  412. {
  413. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  414. struct tsl2583_chip *chip = iio_priv(indio_dev);
  415. int value;
  416. if (kstrtoint(buf, 0, &value) || !value)
  417. return -EINVAL;
  418. mutex_lock(&chip->als_mutex);
  419. chip->als_settings.als_cal_target = value;
  420. mutex_unlock(&chip->als_mutex);
  421. return len;
  422. }
  423. static ssize_t in_illuminance_calibrate_store(struct device *dev,
  424. struct device_attribute *attr,
  425. const char *buf, size_t len)
  426. {
  427. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  428. struct tsl2583_chip *chip = iio_priv(indio_dev);
  429. int value, ret;
  430. if (kstrtoint(buf, 0, &value) || value != 1)
  431. return -EINVAL;
  432. mutex_lock(&chip->als_mutex);
  433. if (chip->suspended) {
  434. ret = -EBUSY;
  435. goto done;
  436. }
  437. ret = tsl2583_als_calibrate(indio_dev);
  438. if (ret < 0)
  439. goto done;
  440. ret = len;
  441. done:
  442. mutex_unlock(&chip->als_mutex);
  443. return ret;
  444. }
  445. static ssize_t in_illuminance_lux_table_show(struct device *dev,
  446. struct device_attribute *attr,
  447. char *buf)
  448. {
  449. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  450. struct tsl2583_chip *chip = iio_priv(indio_dev);
  451. unsigned int i;
  452. int offset = 0;
  453. for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
  454. offset += sprintf(buf + offset, "%u,%u,%u,",
  455. chip->als_settings.als_device_lux[i].ratio,
  456. chip->als_settings.als_device_lux[i].ch0,
  457. chip->als_settings.als_device_lux[i].ch1);
  458. if (chip->als_settings.als_device_lux[i].ratio == 0) {
  459. /*
  460. * We just printed the first "0" entry.
  461. * Now get rid of the extra "," and break.
  462. */
  463. offset--;
  464. break;
  465. }
  466. }
  467. offset += sprintf(buf + offset, "\n");
  468. return offset;
  469. }
  470. static ssize_t in_illuminance_lux_table_store(struct device *dev,
  471. struct device_attribute *attr,
  472. const char *buf, size_t len)
  473. {
  474. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  475. struct tsl2583_chip *chip = iio_priv(indio_dev);
  476. const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
  477. int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
  478. int ret = -EINVAL;
  479. unsigned int n;
  480. mutex_lock(&chip->als_mutex);
  481. get_options(buf, ARRAY_SIZE(value), value);
  482. /*
  483. * We now have an array of ints starting at value[1], and
  484. * enumerated by value[0].
  485. * We expect each group of three ints is one table entry,
  486. * and the last table entry is all 0.
  487. */
  488. n = value[0];
  489. if ((n % 3) || n < 6 || n > max_ints) {
  490. dev_err(dev,
  491. "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
  492. __func__, max_ints);
  493. goto done;
  494. }
  495. if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
  496. dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
  497. __func__);
  498. goto done;
  499. }
  500. memcpy(chip->als_settings.als_device_lux, &value[1],
  501. value[0] * sizeof(value[1]));
  502. ret = len;
  503. done:
  504. mutex_unlock(&chip->als_mutex);
  505. return ret;
  506. }
  507. static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
  508. static IIO_CONST_ATTR(in_illuminance_integration_time_available,
  509. "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650");
  510. static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
  511. static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
  512. static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
  513. static struct attribute *sysfs_attrs_ctrl[] = {
  514. &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
  515. &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
  516. &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
  517. &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
  518. &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
  519. NULL
  520. };
  521. static const struct attribute_group tsl2583_attribute_group = {
  522. .attrs = sysfs_attrs_ctrl,
  523. };
  524. static const struct iio_chan_spec tsl2583_channels[] = {
  525. {
  526. .type = IIO_LIGHT,
  527. .modified = 1,
  528. .channel2 = IIO_MOD_LIGHT_IR,
  529. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  530. },
  531. {
  532. .type = IIO_LIGHT,
  533. .modified = 1,
  534. .channel2 = IIO_MOD_LIGHT_BOTH,
  535. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  536. },
  537. {
  538. .type = IIO_LIGHT,
  539. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  540. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  541. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  542. BIT(IIO_CHAN_INFO_INT_TIME),
  543. },
  544. };
  545. static int tsl2583_read_raw(struct iio_dev *indio_dev,
  546. struct iio_chan_spec const *chan,
  547. int *val, int *val2, long mask)
  548. {
  549. struct tsl2583_chip *chip = iio_priv(indio_dev);
  550. int ret = -EINVAL;
  551. mutex_lock(&chip->als_mutex);
  552. if (chip->suspended) {
  553. ret = -EBUSY;
  554. goto read_done;
  555. }
  556. switch (mask) {
  557. case IIO_CHAN_INFO_RAW:
  558. if (chan->type == IIO_LIGHT) {
  559. ret = tsl2583_get_lux(indio_dev);
  560. if (ret < 0)
  561. goto read_done;
  562. /*
  563. * From page 20 of the TSL2581, TSL2583 data
  564. * sheet (TAOS134 − MARCH 2011):
  565. *
  566. * One of the photodiodes (channel 0) is
  567. * sensitive to both visible and infrared light,
  568. * while the second photodiode (channel 1) is
  569. * sensitive primarily to infrared light.
  570. */
  571. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  572. *val = chip->als_cur_info.als_ch0;
  573. else
  574. *val = chip->als_cur_info.als_ch1;
  575. ret = IIO_VAL_INT;
  576. }
  577. break;
  578. case IIO_CHAN_INFO_PROCESSED:
  579. if (chan->type == IIO_LIGHT) {
  580. ret = tsl2583_get_lux(indio_dev);
  581. if (ret < 0)
  582. goto read_done;
  583. *val = ret;
  584. ret = IIO_VAL_INT;
  585. }
  586. break;
  587. case IIO_CHAN_INFO_CALIBBIAS:
  588. if (chan->type == IIO_LIGHT) {
  589. *val = chip->als_settings.als_gain_trim;
  590. ret = IIO_VAL_INT;
  591. }
  592. break;
  593. case IIO_CHAN_INFO_CALIBSCALE:
  594. if (chan->type == IIO_LIGHT) {
  595. *val = gainadj[chip->als_settings.als_gain].mean;
  596. ret = IIO_VAL_INT;
  597. }
  598. break;
  599. case IIO_CHAN_INFO_INT_TIME:
  600. if (chan->type == IIO_LIGHT) {
  601. *val = 0;
  602. *val2 = chip->als_settings.als_time;
  603. ret = IIO_VAL_INT_PLUS_MICRO;
  604. }
  605. break;
  606. default:
  607. break;
  608. }
  609. read_done:
  610. mutex_unlock(&chip->als_mutex);
  611. return ret;
  612. }
  613. static int tsl2583_write_raw(struct iio_dev *indio_dev,
  614. struct iio_chan_spec const *chan,
  615. int val, int val2, long mask)
  616. {
  617. struct tsl2583_chip *chip = iio_priv(indio_dev);
  618. int ret = -EINVAL;
  619. mutex_lock(&chip->als_mutex);
  620. if (chip->suspended) {
  621. ret = -EBUSY;
  622. goto write_done;
  623. }
  624. switch (mask) {
  625. case IIO_CHAN_INFO_CALIBBIAS:
  626. if (chan->type == IIO_LIGHT) {
  627. chip->als_settings.als_gain_trim = val;
  628. ret = 0;
  629. }
  630. break;
  631. case IIO_CHAN_INFO_CALIBSCALE:
  632. if (chan->type == IIO_LIGHT) {
  633. unsigned int i;
  634. for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
  635. if (gainadj[i].mean == val) {
  636. chip->als_settings.als_gain = i;
  637. ret = tsl2583_set_als_gain(chip);
  638. break;
  639. }
  640. }
  641. }
  642. break;
  643. case IIO_CHAN_INFO_INT_TIME:
  644. if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
  645. val2 <= 650 && !(val2 % 50)) {
  646. chip->als_settings.als_time = val2;
  647. ret = tsl2583_set_als_time(chip);
  648. }
  649. break;
  650. default:
  651. break;
  652. }
  653. write_done:
  654. mutex_unlock(&chip->als_mutex);
  655. return ret;
  656. }
  657. static const struct iio_info tsl2583_info = {
  658. .attrs = &tsl2583_attribute_group,
  659. .driver_module = THIS_MODULE,
  660. .read_raw = tsl2583_read_raw,
  661. .write_raw = tsl2583_write_raw,
  662. };
  663. static int tsl2583_probe(struct i2c_client *clientp,
  664. const struct i2c_device_id *idp)
  665. {
  666. int ret;
  667. struct tsl2583_chip *chip;
  668. struct iio_dev *indio_dev;
  669. if (!i2c_check_functionality(clientp->adapter,
  670. I2C_FUNC_SMBUS_BYTE_DATA)) {
  671. dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
  672. __func__);
  673. return -EOPNOTSUPP;
  674. }
  675. indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
  676. if (!indio_dev)
  677. return -ENOMEM;
  678. chip = iio_priv(indio_dev);
  679. chip->client = clientp;
  680. i2c_set_clientdata(clientp, indio_dev);
  681. mutex_init(&chip->als_mutex);
  682. chip->suspended = true;
  683. ret = i2c_smbus_read_byte_data(clientp,
  684. TSL2583_CMD_REG | TSL2583_CHIPID);
  685. if (ret < 0) {
  686. dev_err(&clientp->dev,
  687. "%s: failed to read the chip ID register\n", __func__);
  688. return ret;
  689. }
  690. if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
  691. dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
  692. __func__, ret);
  693. return -EINVAL;
  694. }
  695. indio_dev->info = &tsl2583_info;
  696. indio_dev->channels = tsl2583_channels;
  697. indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
  698. indio_dev->dev.parent = &clientp->dev;
  699. indio_dev->modes = INDIO_DIRECT_MODE;
  700. indio_dev->name = chip->client->name;
  701. ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  702. if (ret) {
  703. dev_err(&clientp->dev, "%s: iio registration failed\n",
  704. __func__);
  705. return ret;
  706. }
  707. /* Load up the V2 defaults (these are hard coded defaults for now) */
  708. tsl2583_defaults(chip);
  709. /* Make sure the chip is on */
  710. ret = tsl2583_chip_init_and_power_on(indio_dev);
  711. if (ret < 0)
  712. return ret;
  713. dev_info(&clientp->dev, "Light sensor found.\n");
  714. return 0;
  715. }
  716. static int __maybe_unused tsl2583_suspend(struct device *dev)
  717. {
  718. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  719. struct tsl2583_chip *chip = iio_priv(indio_dev);
  720. int ret;
  721. mutex_lock(&chip->als_mutex);
  722. ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
  723. chip->suspended = true;
  724. mutex_unlock(&chip->als_mutex);
  725. return ret;
  726. }
  727. static int __maybe_unused tsl2583_resume(struct device *dev)
  728. {
  729. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  730. struct tsl2583_chip *chip = iio_priv(indio_dev);
  731. int ret;
  732. mutex_lock(&chip->als_mutex);
  733. ret = tsl2583_chip_init_and_power_on(indio_dev);
  734. mutex_unlock(&chip->als_mutex);
  735. return ret;
  736. }
  737. static SIMPLE_DEV_PM_OPS(tsl2583_pm_ops, tsl2583_suspend, tsl2583_resume);
  738. static struct i2c_device_id tsl2583_idtable[] = {
  739. { "tsl2580", 0 },
  740. { "tsl2581", 1 },
  741. { "tsl2583", 2 },
  742. {}
  743. };
  744. MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
  745. static const struct of_device_id tsl2583_of_match[] = {
  746. { .compatible = "amstaos,tsl2580", },
  747. { .compatible = "amstaos,tsl2581", },
  748. { .compatible = "amstaos,tsl2583", },
  749. { },
  750. };
  751. MODULE_DEVICE_TABLE(of, tsl2583_of_match);
  752. /* Driver definition */
  753. static struct i2c_driver tsl2583_driver = {
  754. .driver = {
  755. .name = "tsl2583",
  756. .pm = &tsl2583_pm_ops,
  757. .of_match_table = tsl2583_of_match,
  758. },
  759. .id_table = tsl2583_idtable,
  760. .probe = tsl2583_probe,
  761. };
  762. module_i2c_driver(tsl2583_driver);
  763. MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
  764. MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
  765. MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
  766. MODULE_LICENSE("GPL");