tsl2583.c 24 KB

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