tsl2591.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2021 Joe Sandom <[email protected]>
  4. *
  5. * Datasheet: https://ams.com/tsl25911#tab/documents
  6. *
  7. * Device driver for the TAOS TSL2591. This is a very-high sensitivity
  8. * light-to-digital converter that transforms light intensity into a digital
  9. * signal.
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/sysfs.h>
  22. #include <asm/unaligned.h>
  23. #include <linux/iio/events.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. /* ADC integration time, field value to time in ms */
  27. #define TSL2591_FVAL_TO_MSEC(x) (((x) + 1) * 100)
  28. /* ADC integration time, field value to time in seconds */
  29. #define TSL2591_FVAL_TO_SEC(x) ((x) + 1)
  30. /* ADC integration time, time in seconds to field value */
  31. #define TSL2591_SEC_TO_FVAL(x) ((x) - 1)
  32. /* TSL2591 register set */
  33. #define TSL2591_ENABLE 0x00
  34. #define TSL2591_CONTROL 0x01
  35. #define TSL2591_AILTL 0x04
  36. #define TSL2591_AILTH 0x05
  37. #define TSL2591_AIHTL 0x06
  38. #define TSL2591_AIHTH 0x07
  39. #define TSL2591_NP_AILTL 0x08
  40. #define TSL2591_NP_AILTH 0x09
  41. #define TSL2591_NP_AIHTL 0x0A
  42. #define TSL2591_NP_AIHTH 0x0B
  43. #define TSL2591_PERSIST 0x0C
  44. #define TSL2591_PACKAGE_ID 0x11
  45. #define TSL2591_DEVICE_ID 0x12
  46. #define TSL2591_STATUS 0x13
  47. #define TSL2591_C0_DATAL 0x14
  48. #define TSL2591_C0_DATAH 0x15
  49. #define TSL2591_C1_DATAL 0x16
  50. #define TSL2591_C1_DATAH 0x17
  51. /* TSL2591 command register definitions */
  52. #define TSL2591_CMD_NOP 0xA0
  53. #define TSL2591_CMD_SF_INTSET 0xE4
  54. #define TSL2591_CMD_SF_CALS_I 0xE5
  55. #define TSL2591_CMD_SF_CALS_NPI 0xE7
  56. #define TSL2591_CMD_SF_CNP_ALSI 0xEA
  57. /* TSL2591 enable register definitions */
  58. #define TSL2591_PWR_ON 0x01
  59. #define TSL2591_PWR_OFF 0x00
  60. #define TSL2591_ENABLE_ALS 0x02
  61. #define TSL2591_ENABLE_ALS_INT 0x10
  62. #define TSL2591_ENABLE_SLEEP_INT 0x40
  63. #define TSL2591_ENABLE_NP_INT 0x80
  64. /* TSL2591 control register definitions */
  65. #define TSL2591_CTRL_ALS_INTEGRATION_100MS 0x00
  66. #define TSL2591_CTRL_ALS_INTEGRATION_200MS 0x01
  67. #define TSL2591_CTRL_ALS_INTEGRATION_300MS 0x02
  68. #define TSL2591_CTRL_ALS_INTEGRATION_400MS 0x03
  69. #define TSL2591_CTRL_ALS_INTEGRATION_500MS 0x04
  70. #define TSL2591_CTRL_ALS_INTEGRATION_600MS 0x05
  71. #define TSL2591_CTRL_ALS_LOW_GAIN 0x00
  72. #define TSL2591_CTRL_ALS_MED_GAIN 0x10
  73. #define TSL2591_CTRL_ALS_HIGH_GAIN 0x20
  74. #define TSL2591_CTRL_ALS_MAX_GAIN 0x30
  75. #define TSL2591_CTRL_SYS_RESET 0x80
  76. /* TSL2591 persist register definitions */
  77. #define TSL2591_PRST_ALS_INT_CYCLE_0 0x00
  78. #define TSL2591_PRST_ALS_INT_CYCLE_ANY 0x01
  79. #define TSL2591_PRST_ALS_INT_CYCLE_2 0x02
  80. #define TSL2591_PRST_ALS_INT_CYCLE_3 0x03
  81. #define TSL2591_PRST_ALS_INT_CYCLE_5 0x04
  82. #define TSL2591_PRST_ALS_INT_CYCLE_10 0x05
  83. #define TSL2591_PRST_ALS_INT_CYCLE_15 0x06
  84. #define TSL2591_PRST_ALS_INT_CYCLE_20 0x07
  85. #define TSL2591_PRST_ALS_INT_CYCLE_25 0x08
  86. #define TSL2591_PRST_ALS_INT_CYCLE_30 0x09
  87. #define TSL2591_PRST_ALS_INT_CYCLE_35 0x0A
  88. #define TSL2591_PRST_ALS_INT_CYCLE_40 0x0B
  89. #define TSL2591_PRST_ALS_INT_CYCLE_45 0x0C
  90. #define TSL2591_PRST_ALS_INT_CYCLE_50 0x0D
  91. #define TSL2591_PRST_ALS_INT_CYCLE_55 0x0E
  92. #define TSL2591_PRST_ALS_INT_CYCLE_60 0x0F
  93. #define TSL2591_PRST_ALS_INT_CYCLE_MAX (BIT(4) - 1)
  94. /* TSL2591 PID register mask */
  95. #define TSL2591_PACKAGE_ID_MASK GENMASK(5, 4)
  96. /* TSL2591 ID register mask */
  97. #define TSL2591_DEVICE_ID_MASK GENMASK(7, 0)
  98. /* TSL2591 status register masks */
  99. #define TSL2591_STS_ALS_VALID_MASK BIT(0)
  100. #define TSL2591_STS_ALS_INT_MASK BIT(4)
  101. #define TSL2591_STS_NPERS_INT_MASK BIT(5)
  102. #define TSL2591_STS_VAL_HIGH_MASK BIT(0)
  103. /* TSL2591 constant values */
  104. #define TSL2591_PACKAGE_ID_VAL 0x00
  105. #define TSL2591_DEVICE_ID_VAL 0x50
  106. /* Power off suspend delay time MS */
  107. #define TSL2591_POWER_OFF_DELAY_MS 2000
  108. /* TSL2591 default values */
  109. #define TSL2591_DEFAULT_ALS_INT_TIME TSL2591_CTRL_ALS_INTEGRATION_300MS
  110. #define TSL2591_DEFAULT_ALS_GAIN TSL2591_CTRL_ALS_MED_GAIN
  111. #define TSL2591_DEFAULT_ALS_PERSIST TSL2591_PRST_ALS_INT_CYCLE_ANY
  112. #define TSL2591_DEFAULT_ALS_LOWER_THRESH 100
  113. #define TSL2591_DEFAULT_ALS_UPPER_THRESH 1500
  114. /* TSL2591 number of data registers */
  115. #define TSL2591_NUM_DATA_REGISTERS 4
  116. /* TSL2591 number of valid status reads on ADC complete */
  117. #define TSL2591_ALS_STS_VALID_COUNT 10
  118. /* TSL2591 delay period between polls when checking for ALS valid flag */
  119. #define TSL2591_DELAY_PERIOD_US 10000
  120. /* TSL2591 maximum values */
  121. #define TSL2591_MAX_ALS_INT_TIME_MS 600
  122. #define TSL2591_ALS_MAX_VALUE (BIT(16) - 1)
  123. /*
  124. * LUX calculations;
  125. * AGAIN values from Adafruit's TSL2591 Arduino library
  126. * https://github.com/adafruit/Adafruit_TSL2591_Library
  127. */
  128. #define TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER 1
  129. #define TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER 25
  130. #define TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER 428
  131. #define TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER 9876
  132. #define TSL2591_LUX_COEFFICIENT 408
  133. struct tsl2591_als_settings {
  134. u16 als_lower_thresh;
  135. u16 als_upper_thresh;
  136. u8 als_int_time;
  137. u8 als_persist;
  138. u8 als_gain;
  139. };
  140. struct tsl2591_chip {
  141. struct tsl2591_als_settings als_settings;
  142. struct i2c_client *client;
  143. /*
  144. * Keep als_settings in sync with hardware state
  145. * and ensure multiple readers are serialized.
  146. */
  147. struct mutex als_mutex;
  148. bool events_enabled;
  149. };
  150. /*
  151. * Period table is ALS persist cycle x integration time setting
  152. * Integration times: 100ms, 200ms, 300ms, 400ms, 500ms, 600ms
  153. * ALS cycles: 1, 2, 3, 5, 10, 20, 25, 30, 35, 40, 45, 50, 55, 60
  154. */
  155. static const char * const tsl2591_als_period_list[] = {
  156. "0.1 0.2 0.3 0.5 1.0 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0",
  157. "0.2 0.4 0.6 1.0 2.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0",
  158. "0.3 0.6 0.9 1.5 3.0 6.0 7.5 9.0 10.5 12.0 13.5 15.0 16.5 18.0",
  159. "0.4 0.8 1.2 2.0 4.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0",
  160. "0.5 1.0 1.5 2.5 5.0 10.0 12.5 15.0 17.5 20.0 22.5 25.0 27.5 30.0",
  161. "0.6 1.2 1.8 3.0 6.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0 33.0 36.0",
  162. };
  163. static const int tsl2591_int_time_available[] = {
  164. 1, 2, 3, 4, 5, 6,
  165. };
  166. static const int tsl2591_calibscale_available[] = {
  167. 1, 25, 428, 9876,
  168. };
  169. static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
  170. u16 als_lower_threshold);
  171. static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
  172. u16 als_upper_threshold);
  173. static int tsl2591_gain_to_multiplier(const u8 als_gain)
  174. {
  175. switch (als_gain) {
  176. case TSL2591_CTRL_ALS_LOW_GAIN:
  177. return TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER;
  178. case TSL2591_CTRL_ALS_MED_GAIN:
  179. return TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER;
  180. case TSL2591_CTRL_ALS_HIGH_GAIN:
  181. return TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER;
  182. case TSL2591_CTRL_ALS_MAX_GAIN:
  183. return TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER;
  184. default:
  185. return -EINVAL;
  186. }
  187. }
  188. static int tsl2591_multiplier_to_gain(const u32 multiplier)
  189. {
  190. switch (multiplier) {
  191. case TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER:
  192. return TSL2591_CTRL_ALS_LOW_GAIN;
  193. case TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER:
  194. return TSL2591_CTRL_ALS_MED_GAIN;
  195. case TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER:
  196. return TSL2591_CTRL_ALS_HIGH_GAIN;
  197. case TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER:
  198. return TSL2591_CTRL_ALS_MAX_GAIN;
  199. default:
  200. return -EINVAL;
  201. }
  202. }
  203. static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
  204. {
  205. switch (als_persist) {
  206. case TSL2591_PRST_ALS_INT_CYCLE_ANY:
  207. return 1;
  208. case TSL2591_PRST_ALS_INT_CYCLE_2:
  209. return 2;
  210. case TSL2591_PRST_ALS_INT_CYCLE_3:
  211. return 3;
  212. case TSL2591_PRST_ALS_INT_CYCLE_5:
  213. return 5;
  214. case TSL2591_PRST_ALS_INT_CYCLE_10:
  215. return 10;
  216. case TSL2591_PRST_ALS_INT_CYCLE_15:
  217. return 15;
  218. case TSL2591_PRST_ALS_INT_CYCLE_20:
  219. return 20;
  220. case TSL2591_PRST_ALS_INT_CYCLE_25:
  221. return 25;
  222. case TSL2591_PRST_ALS_INT_CYCLE_30:
  223. return 30;
  224. case TSL2591_PRST_ALS_INT_CYCLE_35:
  225. return 35;
  226. case TSL2591_PRST_ALS_INT_CYCLE_40:
  227. return 40;
  228. case TSL2591_PRST_ALS_INT_CYCLE_45:
  229. return 45;
  230. case TSL2591_PRST_ALS_INT_CYCLE_50:
  231. return 50;
  232. case TSL2591_PRST_ALS_INT_CYCLE_55:
  233. return 55;
  234. case TSL2591_PRST_ALS_INT_CYCLE_60:
  235. return 60;
  236. default:
  237. return -EINVAL;
  238. }
  239. }
  240. static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
  241. {
  242. switch (als_persist) {
  243. case 1:
  244. return TSL2591_PRST_ALS_INT_CYCLE_ANY;
  245. case 2:
  246. return TSL2591_PRST_ALS_INT_CYCLE_2;
  247. case 3:
  248. return TSL2591_PRST_ALS_INT_CYCLE_3;
  249. case 5:
  250. return TSL2591_PRST_ALS_INT_CYCLE_5;
  251. case 10:
  252. return TSL2591_PRST_ALS_INT_CYCLE_10;
  253. case 15:
  254. return TSL2591_PRST_ALS_INT_CYCLE_15;
  255. case 20:
  256. return TSL2591_PRST_ALS_INT_CYCLE_20;
  257. case 25:
  258. return TSL2591_PRST_ALS_INT_CYCLE_25;
  259. case 30:
  260. return TSL2591_PRST_ALS_INT_CYCLE_30;
  261. case 35:
  262. return TSL2591_PRST_ALS_INT_CYCLE_35;
  263. case 40:
  264. return TSL2591_PRST_ALS_INT_CYCLE_40;
  265. case 45:
  266. return TSL2591_PRST_ALS_INT_CYCLE_45;
  267. case 50:
  268. return TSL2591_PRST_ALS_INT_CYCLE_50;
  269. case 55:
  270. return TSL2591_PRST_ALS_INT_CYCLE_55;
  271. case 60:
  272. return TSL2591_PRST_ALS_INT_CYCLE_60;
  273. default:
  274. return -EINVAL;
  275. }
  276. }
  277. static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
  278. const u32 als_integration_time)
  279. {
  280. switch (als_integration_time) {
  281. case TSL2591_CTRL_ALS_INTEGRATION_100MS:
  282. case TSL2591_CTRL_ALS_INTEGRATION_200MS:
  283. case TSL2591_CTRL_ALS_INTEGRATION_300MS:
  284. case TSL2591_CTRL_ALS_INTEGRATION_400MS:
  285. case TSL2591_CTRL_ALS_INTEGRATION_500MS:
  286. case TSL2591_CTRL_ALS_INTEGRATION_600MS:
  287. return 0;
  288. default:
  289. return -EINVAL;
  290. }
  291. }
  292. static int tsl2591_als_time_to_fval(const u32 als_integration_time)
  293. {
  294. int i;
  295. for (i = 0; i < ARRAY_SIZE(tsl2591_int_time_available); i++) {
  296. if (als_integration_time == tsl2591_int_time_available[i])
  297. return TSL2591_SEC_TO_FVAL(als_integration_time);
  298. }
  299. return -EINVAL;
  300. }
  301. static int tsl2591_compatible_gain(struct tsl2591_chip *chip, const u8 als_gain)
  302. {
  303. switch (als_gain) {
  304. case TSL2591_CTRL_ALS_LOW_GAIN:
  305. case TSL2591_CTRL_ALS_MED_GAIN:
  306. case TSL2591_CTRL_ALS_HIGH_GAIN:
  307. case TSL2591_CTRL_ALS_MAX_GAIN:
  308. return 0;
  309. default:
  310. return -EINVAL;
  311. }
  312. }
  313. static int tsl2591_compatible_als_persist_cycle(struct tsl2591_chip *chip,
  314. const u32 als_persist)
  315. {
  316. switch (als_persist) {
  317. case TSL2591_PRST_ALS_INT_CYCLE_ANY:
  318. case TSL2591_PRST_ALS_INT_CYCLE_2:
  319. case TSL2591_PRST_ALS_INT_CYCLE_3:
  320. case TSL2591_PRST_ALS_INT_CYCLE_5:
  321. case TSL2591_PRST_ALS_INT_CYCLE_10:
  322. case TSL2591_PRST_ALS_INT_CYCLE_15:
  323. case TSL2591_PRST_ALS_INT_CYCLE_20:
  324. case TSL2591_PRST_ALS_INT_CYCLE_25:
  325. case TSL2591_PRST_ALS_INT_CYCLE_30:
  326. case TSL2591_PRST_ALS_INT_CYCLE_35:
  327. case TSL2591_PRST_ALS_INT_CYCLE_40:
  328. case TSL2591_PRST_ALS_INT_CYCLE_45:
  329. case TSL2591_PRST_ALS_INT_CYCLE_50:
  330. case TSL2591_PRST_ALS_INT_CYCLE_55:
  331. case TSL2591_PRST_ALS_INT_CYCLE_60:
  332. return 0;
  333. default:
  334. return -EINVAL;
  335. }
  336. }
  337. static int tsl2591_check_als_valid(struct i2c_client *client)
  338. {
  339. int ret;
  340. ret = i2c_smbus_read_byte_data(client, TSL2591_CMD_NOP | TSL2591_STATUS);
  341. if (ret < 0) {
  342. dev_err(&client->dev, "Failed to read register\n");
  343. return -EINVAL;
  344. }
  345. return FIELD_GET(TSL2591_STS_ALS_VALID_MASK, ret);
  346. }
  347. static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)
  348. {
  349. struct tsl2591_als_settings settings = chip->als_settings;
  350. struct i2c_client *client = chip->client;
  351. int delay;
  352. int val;
  353. int ret;
  354. delay = TSL2591_FVAL_TO_MSEC(settings.als_int_time);
  355. if (!delay)
  356. return -EINVAL;
  357. /*
  358. * Sleep for ALS integration time to allow enough time or an ADC read
  359. * cycle to complete. Check status after delay for ALS valid.
  360. */
  361. msleep(delay);
  362. /* Check for status ALS valid flag for up to 100ms */
  363. ret = readx_poll_timeout(tsl2591_check_als_valid, client,
  364. val, val == TSL2591_STS_VAL_HIGH_MASK,
  365. TSL2591_DELAY_PERIOD_US,
  366. TSL2591_DELAY_PERIOD_US * TSL2591_ALS_STS_VALID_COUNT);
  367. if (ret)
  368. dev_err(&client->dev, "Timed out waiting for valid ALS data\n");
  369. return ret;
  370. }
  371. /*
  372. * tsl2591_read_channel_data - Reads raw channel data and calculates lux
  373. *
  374. * Formula for lux calculation;
  375. * Derived from Adafruit's TSL2591 library
  376. * Link: https://github.com/adafruit/Adafruit_TSL2591_Library
  377. * Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF
  378. * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
  379. *
  380. * Scale values to get more representative value of lux i.e.
  381. * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
  382. *
  383. * Channel 0 = IR + Visible
  384. * Channel 1 = IR only
  385. */
  386. static int tsl2591_read_channel_data(struct iio_dev *indio_dev,
  387. struct iio_chan_spec const *chan,
  388. int *val, int *val2)
  389. {
  390. struct tsl2591_chip *chip = iio_priv(indio_dev);
  391. struct tsl2591_als_settings *settings = &chip->als_settings;
  392. struct i2c_client *client = chip->client;
  393. u8 als_data[TSL2591_NUM_DATA_REGISTERS];
  394. int counts_per_lux, int_time_fval, gain_multi, lux;
  395. u16 als_ch0, als_ch1;
  396. int ret;
  397. ret = tsl2591_wait_adc_complete(chip);
  398. if (ret < 0) {
  399. dev_err(&client->dev, "No data available. Err: %d\n", ret);
  400. return ret;
  401. }
  402. ret = i2c_smbus_read_i2c_block_data(client,
  403. TSL2591_CMD_NOP | TSL2591_C0_DATAL,
  404. sizeof(als_data), als_data);
  405. if (ret < 0) {
  406. dev_err(&client->dev, "Failed to read data bytes");
  407. return ret;
  408. }
  409. als_ch0 = get_unaligned_le16(&als_data[0]);
  410. als_ch1 = get_unaligned_le16(&als_data[2]);
  411. switch (chan->type) {
  412. case IIO_INTENSITY:
  413. if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
  414. *val = als_ch0;
  415. else if (chan->channel2 == IIO_MOD_LIGHT_IR)
  416. *val = als_ch1;
  417. else
  418. return -EINVAL;
  419. break;
  420. case IIO_LIGHT:
  421. gain_multi = tsl2591_gain_to_multiplier(settings->als_gain);
  422. if (gain_multi < 0) {
  423. dev_err(&client->dev, "Invalid multiplier");
  424. return gain_multi;
  425. }
  426. int_time_fval = TSL2591_FVAL_TO_MSEC(settings->als_int_time);
  427. /* Calculate counts per lux value */
  428. counts_per_lux = (int_time_fval * gain_multi) / TSL2591_LUX_COEFFICIENT;
  429. dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux);
  430. /* Calculate lux value */
  431. lux = ((als_ch0 - als_ch1) *
  432. (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;
  433. dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux);
  434. /* Divide by 1000 to get real lux value before scaling */
  435. *val = lux / 1000;
  436. /* Get the decimal part of lux reading */
  437. *val2 = (lux - (*val * 1000)) * 1000;
  438. break;
  439. default:
  440. return -EINVAL;
  441. }
  442. return 0;
  443. }
  444. static int tsl2591_set_als_gain_int_time(struct tsl2591_chip *chip)
  445. {
  446. struct tsl2591_als_settings als_settings = chip->als_settings;
  447. struct i2c_client *client = chip->client;
  448. int ret;
  449. ret = i2c_smbus_write_byte_data(client,
  450. TSL2591_CMD_NOP | TSL2591_CONTROL,
  451. als_settings.als_int_time | als_settings.als_gain);
  452. if (ret)
  453. dev_err(&client->dev, "Failed to set als gain & int time\n");
  454. return ret;
  455. }
  456. static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
  457. u16 als_lower_threshold)
  458. {
  459. struct tsl2591_als_settings als_settings = chip->als_settings;
  460. struct i2c_client *client = chip->client;
  461. u16 als_upper_threshold;
  462. u8 als_lower_l;
  463. u8 als_lower_h;
  464. int ret;
  465. chip->als_settings.als_lower_thresh = als_lower_threshold;
  466. /*
  467. * Lower threshold should not be greater or equal to upper.
  468. * If this is the case, then assert upper threshold to new lower
  469. * threshold + 1 to avoid ordering issues when setting thresholds.
  470. */
  471. if (als_lower_threshold >= als_settings.als_upper_thresh) {
  472. als_upper_threshold = als_lower_threshold + 1;
  473. tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
  474. }
  475. als_lower_l = als_lower_threshold;
  476. als_lower_h = als_lower_threshold >> 8;
  477. ret = i2c_smbus_write_byte_data(client,
  478. TSL2591_CMD_NOP | TSL2591_AILTL,
  479. als_lower_l);
  480. if (ret) {
  481. dev_err(&client->dev, "Failed to set als lower threshold\n");
  482. return ret;
  483. }
  484. ret = i2c_smbus_write_byte_data(client,
  485. TSL2591_CMD_NOP | TSL2591_AILTH,
  486. als_lower_h);
  487. if (ret) {
  488. dev_err(&client->dev, "Failed to set als lower threshold\n");
  489. return ret;
  490. }
  491. return 0;
  492. }
  493. static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
  494. u16 als_upper_threshold)
  495. {
  496. struct tsl2591_als_settings als_settings = chip->als_settings;
  497. struct i2c_client *client = chip->client;
  498. u16 als_lower_threshold;
  499. u8 als_upper_l;
  500. u8 als_upper_h;
  501. int ret;
  502. if (als_upper_threshold > TSL2591_ALS_MAX_VALUE)
  503. return -EINVAL;
  504. chip->als_settings.als_upper_thresh = als_upper_threshold;
  505. /*
  506. * Upper threshold should not be less than lower. If this
  507. * is the case, then assert lower threshold to new upper
  508. * threshold - 1 to avoid ordering issues when setting thresholds.
  509. */
  510. if (als_upper_threshold < als_settings.als_lower_thresh) {
  511. als_lower_threshold = als_upper_threshold - 1;
  512. tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
  513. }
  514. als_upper_l = als_upper_threshold;
  515. als_upper_h = als_upper_threshold >> 8;
  516. ret = i2c_smbus_write_byte_data(client,
  517. TSL2591_CMD_NOP | TSL2591_AIHTL,
  518. als_upper_l);
  519. if (ret) {
  520. dev_err(&client->dev, "Failed to set als upper threshold\n");
  521. return ret;
  522. }
  523. ret = i2c_smbus_write_byte_data(client,
  524. TSL2591_CMD_NOP | TSL2591_AIHTH,
  525. als_upper_h);
  526. if (ret) {
  527. dev_err(&client->dev, "Failed to set als upper threshold\n");
  528. return ret;
  529. }
  530. return 0;
  531. }
  532. static int tsl2591_set_als_persist_cycle(struct tsl2591_chip *chip,
  533. u8 als_persist)
  534. {
  535. struct i2c_client *client = chip->client;
  536. int ret;
  537. ret = i2c_smbus_write_byte_data(client,
  538. TSL2591_CMD_NOP | TSL2591_PERSIST,
  539. als_persist);
  540. if (ret)
  541. dev_err(&client->dev, "Failed to set als persist cycle\n");
  542. chip->als_settings.als_persist = als_persist;
  543. return ret;
  544. }
  545. static int tsl2591_set_power_state(struct tsl2591_chip *chip, u8 state)
  546. {
  547. struct i2c_client *client = chip->client;
  548. int ret;
  549. ret = i2c_smbus_write_byte_data(client,
  550. TSL2591_CMD_NOP | TSL2591_ENABLE,
  551. state);
  552. if (ret)
  553. dev_err(&client->dev,
  554. "Failed to set the power state to %#04x\n", state);
  555. return ret;
  556. }
  557. static ssize_t tsl2591_in_illuminance_period_available_show(struct device *dev,
  558. struct device_attribute *attr,
  559. char *buf)
  560. {
  561. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  562. struct tsl2591_chip *chip = iio_priv(indio_dev);
  563. return sysfs_emit(buf, "%s\n",
  564. tsl2591_als_period_list[chip->als_settings.als_int_time]);
  565. }
  566. static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available, 0);
  567. static struct attribute *tsl2591_event_attrs_ctrl[] = {
  568. &iio_dev_attr_tsl2591_in_illuminance_period_available.dev_attr.attr,
  569. NULL
  570. };
  571. static const struct attribute_group tsl2591_event_attribute_group = {
  572. .attrs = tsl2591_event_attrs_ctrl,
  573. };
  574. static const struct iio_event_spec tsl2591_events[] = {
  575. {
  576. .type = IIO_EV_TYPE_THRESH,
  577. .dir = IIO_EV_DIR_RISING,
  578. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  579. }, {
  580. .type = IIO_EV_TYPE_THRESH,
  581. .dir = IIO_EV_DIR_FALLING,
  582. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  583. }, {
  584. .type = IIO_EV_TYPE_THRESH,
  585. .dir = IIO_EV_DIR_EITHER,
  586. .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
  587. BIT(IIO_EV_INFO_ENABLE),
  588. },
  589. };
  590. static const struct iio_chan_spec tsl2591_channels[] = {
  591. {
  592. .type = IIO_INTENSITY,
  593. .modified = 1,
  594. .channel2 = IIO_MOD_LIGHT_IR,
  595. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  596. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
  597. BIT(IIO_CHAN_INFO_CALIBSCALE),
  598. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
  599. BIT(IIO_CHAN_INFO_CALIBSCALE)
  600. },
  601. {
  602. .type = IIO_INTENSITY,
  603. .modified = 1,
  604. .channel2 = IIO_MOD_LIGHT_BOTH,
  605. .event_spec = tsl2591_events,
  606. .num_event_specs = ARRAY_SIZE(tsl2591_events),
  607. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  608. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
  609. BIT(IIO_CHAN_INFO_CALIBSCALE),
  610. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
  611. BIT(IIO_CHAN_INFO_CALIBSCALE)
  612. },
  613. {
  614. .type = IIO_LIGHT,
  615. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  616. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
  617. BIT(IIO_CHAN_INFO_CALIBSCALE),
  618. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
  619. BIT(IIO_CHAN_INFO_CALIBSCALE)
  620. },
  621. };
  622. static int tsl2591_read_raw(struct iio_dev *indio_dev,
  623. struct iio_chan_spec const *chan,
  624. int *val, int *val2, long mask)
  625. {
  626. struct tsl2591_chip *chip = iio_priv(indio_dev);
  627. struct i2c_client *client = chip->client;
  628. int ret;
  629. pm_runtime_get_sync(&client->dev);
  630. mutex_lock(&chip->als_mutex);
  631. switch (mask) {
  632. case IIO_CHAN_INFO_RAW:
  633. if (chan->type != IIO_INTENSITY) {
  634. ret = -EINVAL;
  635. goto err_unlock;
  636. }
  637. ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
  638. if (ret < 0)
  639. goto err_unlock;
  640. ret = IIO_VAL_INT;
  641. break;
  642. case IIO_CHAN_INFO_PROCESSED:
  643. if (chan->type != IIO_LIGHT) {
  644. ret = -EINVAL;
  645. goto err_unlock;
  646. }
  647. ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
  648. if (ret < 0)
  649. break;
  650. ret = IIO_VAL_INT_PLUS_MICRO;
  651. break;
  652. case IIO_CHAN_INFO_INT_TIME:
  653. if (chan->type != IIO_INTENSITY) {
  654. ret = -EINVAL;
  655. goto err_unlock;
  656. }
  657. *val = TSL2591_FVAL_TO_SEC(chip->als_settings.als_int_time);
  658. ret = IIO_VAL_INT;
  659. break;
  660. case IIO_CHAN_INFO_CALIBSCALE:
  661. if (chan->type != IIO_INTENSITY) {
  662. ret = -EINVAL;
  663. goto err_unlock;
  664. }
  665. *val = tsl2591_gain_to_multiplier(chip->als_settings.als_gain);
  666. ret = IIO_VAL_INT;
  667. break;
  668. default:
  669. ret = -EINVAL;
  670. break;
  671. }
  672. err_unlock:
  673. mutex_unlock(&chip->als_mutex);
  674. pm_runtime_mark_last_busy(&client->dev);
  675. pm_runtime_put_autosuspend(&client->dev);
  676. return ret;
  677. }
  678. static int tsl2591_write_raw(struct iio_dev *indio_dev,
  679. struct iio_chan_spec const *chan,
  680. int val, int val2, long mask)
  681. {
  682. struct tsl2591_chip *chip = iio_priv(indio_dev);
  683. int int_time;
  684. int gain;
  685. int ret;
  686. mutex_lock(&chip->als_mutex);
  687. switch (mask) {
  688. case IIO_CHAN_INFO_INT_TIME:
  689. int_time = tsl2591_als_time_to_fval(val);
  690. if (int_time < 0) {
  691. ret = int_time;
  692. goto err_unlock;
  693. }
  694. ret = tsl2591_compatible_int_time(chip, int_time);
  695. if (ret < 0)
  696. goto err_unlock;
  697. chip->als_settings.als_int_time = int_time;
  698. break;
  699. case IIO_CHAN_INFO_CALIBSCALE:
  700. gain = tsl2591_multiplier_to_gain(val);
  701. if (gain < 0) {
  702. ret = gain;
  703. goto err_unlock;
  704. }
  705. ret = tsl2591_compatible_gain(chip, gain);
  706. if (ret < 0)
  707. goto err_unlock;
  708. chip->als_settings.als_gain = gain;
  709. break;
  710. default:
  711. ret = -EINVAL;
  712. goto err_unlock;
  713. }
  714. ret = tsl2591_set_als_gain_int_time(chip);
  715. err_unlock:
  716. mutex_unlock(&chip->als_mutex);
  717. return ret;
  718. }
  719. static int tsl2591_read_available(struct iio_dev *indio_dev,
  720. struct iio_chan_spec const *chan,
  721. const int **vals, int *type, int *length,
  722. long mask)
  723. {
  724. switch (mask) {
  725. case IIO_CHAN_INFO_INT_TIME:
  726. *length = ARRAY_SIZE(tsl2591_int_time_available);
  727. *vals = tsl2591_int_time_available;
  728. *type = IIO_VAL_INT;
  729. return IIO_AVAIL_LIST;
  730. case IIO_CHAN_INFO_CALIBSCALE:
  731. *length = ARRAY_SIZE(tsl2591_calibscale_available);
  732. *vals = tsl2591_calibscale_available;
  733. *type = IIO_VAL_INT;
  734. return IIO_AVAIL_LIST;
  735. default:
  736. return -EINVAL;
  737. }
  738. }
  739. static int tsl2591_read_event_value(struct iio_dev *indio_dev,
  740. const struct iio_chan_spec *chan,
  741. enum iio_event_type type,
  742. enum iio_event_direction dir,
  743. enum iio_event_info info, int *val,
  744. int *val2)
  745. {
  746. struct tsl2591_chip *chip = iio_priv(indio_dev);
  747. struct i2c_client *client = chip->client;
  748. int als_persist, int_time, period;
  749. int ret;
  750. mutex_lock(&chip->als_mutex);
  751. switch (info) {
  752. case IIO_EV_INFO_VALUE:
  753. switch (dir) {
  754. case IIO_EV_DIR_RISING:
  755. *val = chip->als_settings.als_upper_thresh;
  756. break;
  757. case IIO_EV_DIR_FALLING:
  758. *val = chip->als_settings.als_lower_thresh;
  759. break;
  760. default:
  761. ret = -EINVAL;
  762. goto err_unlock;
  763. }
  764. ret = IIO_VAL_INT;
  765. break;
  766. case IIO_EV_INFO_PERIOD:
  767. ret = i2c_smbus_read_byte_data(client,
  768. TSL2591_CMD_NOP | TSL2591_PERSIST);
  769. if (ret <= 0 || ret > TSL2591_PRST_ALS_INT_CYCLE_MAX)
  770. goto err_unlock;
  771. als_persist = tsl2591_persist_cycle_to_lit(ret);
  772. int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
  773. period = als_persist * (int_time * MSEC_PER_SEC);
  774. *val = period / USEC_PER_SEC;
  775. *val2 = period % USEC_PER_SEC;
  776. ret = IIO_VAL_INT_PLUS_MICRO;
  777. break;
  778. default:
  779. ret = -EINVAL;
  780. break;
  781. }
  782. err_unlock:
  783. mutex_unlock(&chip->als_mutex);
  784. return ret;
  785. }
  786. static int tsl2591_write_event_value(struct iio_dev *indio_dev,
  787. const struct iio_chan_spec *chan,
  788. enum iio_event_type type,
  789. enum iio_event_direction dir,
  790. enum iio_event_info info, int val,
  791. int val2)
  792. {
  793. struct tsl2591_chip *chip = iio_priv(indio_dev);
  794. int period, int_time, als_persist;
  795. int ret;
  796. if (val < 0 || val2 < 0)
  797. return -EINVAL;
  798. mutex_lock(&chip->als_mutex);
  799. switch (info) {
  800. case IIO_EV_INFO_VALUE:
  801. if (val > TSL2591_ALS_MAX_VALUE) {
  802. ret = -EINVAL;
  803. goto err_unlock;
  804. }
  805. switch (dir) {
  806. case IIO_EV_DIR_RISING:
  807. ret = tsl2591_set_als_upper_threshold(chip, val);
  808. if (ret < 0)
  809. goto err_unlock;
  810. break;
  811. case IIO_EV_DIR_FALLING:
  812. ret = tsl2591_set_als_lower_threshold(chip, val);
  813. if (ret < 0)
  814. goto err_unlock;
  815. break;
  816. default:
  817. ret = -EINVAL;
  818. goto err_unlock;
  819. }
  820. break;
  821. case IIO_EV_INFO_PERIOD:
  822. int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
  823. period = ((val * MSEC_PER_SEC) +
  824. (val2 / MSEC_PER_SEC)) / int_time;
  825. als_persist = tsl2591_persist_lit_to_cycle(period);
  826. if (als_persist < 0) {
  827. ret = -EINVAL;
  828. goto err_unlock;
  829. }
  830. ret = tsl2591_compatible_als_persist_cycle(chip, als_persist);
  831. if (ret < 0)
  832. goto err_unlock;
  833. ret = tsl2591_set_als_persist_cycle(chip, als_persist);
  834. if (ret < 0)
  835. goto err_unlock;
  836. break;
  837. default:
  838. ret = -EINVAL;
  839. break;
  840. }
  841. err_unlock:
  842. mutex_unlock(&chip->als_mutex);
  843. return ret;
  844. }
  845. static int tsl2591_read_event_config(struct iio_dev *indio_dev,
  846. const struct iio_chan_spec *chan,
  847. enum iio_event_type type,
  848. enum iio_event_direction dir)
  849. {
  850. struct tsl2591_chip *chip = iio_priv(indio_dev);
  851. return chip->events_enabled;
  852. }
  853. static int tsl2591_write_event_config(struct iio_dev *indio_dev,
  854. const struct iio_chan_spec *chan,
  855. enum iio_event_type type,
  856. enum iio_event_direction dir,
  857. int state)
  858. {
  859. struct tsl2591_chip *chip = iio_priv(indio_dev);
  860. struct i2c_client *client = chip->client;
  861. if (state && !chip->events_enabled) {
  862. chip->events_enabled = true;
  863. pm_runtime_get_sync(&client->dev);
  864. } else if (!state && chip->events_enabled) {
  865. chip->events_enabled = false;
  866. pm_runtime_mark_last_busy(&client->dev);
  867. pm_runtime_put_autosuspend(&client->dev);
  868. }
  869. return 0;
  870. }
  871. static const struct iio_info tsl2591_info = {
  872. .event_attrs = &tsl2591_event_attribute_group,
  873. .read_raw = tsl2591_read_raw,
  874. .write_raw = tsl2591_write_raw,
  875. .read_avail = tsl2591_read_available,
  876. .read_event_value = tsl2591_read_event_value,
  877. .write_event_value = tsl2591_write_event_value,
  878. .read_event_config = tsl2591_read_event_config,
  879. .write_event_config = tsl2591_write_event_config,
  880. };
  881. static const struct iio_info tsl2591_info_no_irq = {
  882. .read_raw = tsl2591_read_raw,
  883. .write_raw = tsl2591_write_raw,
  884. .read_avail = tsl2591_read_available,
  885. };
  886. static int tsl2591_suspend(struct device *dev)
  887. {
  888. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  889. struct tsl2591_chip *chip = iio_priv(indio_dev);
  890. int ret;
  891. mutex_lock(&chip->als_mutex);
  892. ret = tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
  893. mutex_unlock(&chip->als_mutex);
  894. return ret;
  895. }
  896. static int tsl2591_resume(struct device *dev)
  897. {
  898. int power_state = TSL2591_PWR_ON | TSL2591_ENABLE_ALS;
  899. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  900. struct tsl2591_chip *chip = iio_priv(indio_dev);
  901. int ret;
  902. if (chip->events_enabled)
  903. power_state |= TSL2591_ENABLE_ALS_INT;
  904. mutex_lock(&chip->als_mutex);
  905. ret = tsl2591_set_power_state(chip, power_state);
  906. mutex_unlock(&chip->als_mutex);
  907. return ret;
  908. }
  909. static DEFINE_RUNTIME_DEV_PM_OPS(tsl2591_pm_ops, tsl2591_suspend,
  910. tsl2591_resume, NULL);
  911. static irqreturn_t tsl2591_event_handler(int irq, void *private)
  912. {
  913. struct iio_dev *dev_info = private;
  914. struct tsl2591_chip *chip = iio_priv(dev_info);
  915. struct i2c_client *client = chip->client;
  916. if (!chip->events_enabled)
  917. return IRQ_NONE;
  918. iio_push_event(dev_info,
  919. IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
  920. IIO_EV_TYPE_THRESH,
  921. IIO_EV_DIR_EITHER),
  922. iio_get_time_ns(dev_info));
  923. /* Clear ALS irq */
  924. i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
  925. return IRQ_HANDLED;
  926. }
  927. static int tsl2591_load_defaults(struct tsl2591_chip *chip)
  928. {
  929. int ret;
  930. chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME;
  931. chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN;
  932. chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH;
  933. chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH;
  934. ret = tsl2591_set_als_gain_int_time(chip);
  935. if (ret < 0)
  936. return ret;
  937. ret = tsl2591_set_als_persist_cycle(chip, TSL2591_DEFAULT_ALS_PERSIST);
  938. if (ret < 0)
  939. return ret;
  940. ret = tsl2591_set_als_lower_threshold(chip, TSL2591_DEFAULT_ALS_LOWER_THRESH);
  941. if (ret < 0)
  942. return ret;
  943. ret = tsl2591_set_als_upper_threshold(chip, TSL2591_DEFAULT_ALS_UPPER_THRESH);
  944. if (ret < 0)
  945. return ret;
  946. return 0;
  947. }
  948. static void tsl2591_chip_off(void *data)
  949. {
  950. struct iio_dev *indio_dev = data;
  951. struct tsl2591_chip *chip = iio_priv(indio_dev);
  952. struct i2c_client *client = chip->client;
  953. pm_runtime_disable(&client->dev);
  954. pm_runtime_set_suspended(&client->dev);
  955. pm_runtime_put_noidle(&client->dev);
  956. tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
  957. }
  958. static int tsl2591_probe(struct i2c_client *client)
  959. {
  960. struct tsl2591_chip *chip;
  961. struct iio_dev *indio_dev;
  962. int ret;
  963. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  964. dev_err(&client->dev,
  965. "I2C smbus byte data functionality is not supported\n");
  966. return -EOPNOTSUPP;
  967. }
  968. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  969. if (!indio_dev)
  970. return -ENOMEM;
  971. chip = iio_priv(indio_dev);
  972. chip->client = client;
  973. i2c_set_clientdata(client, indio_dev);
  974. if (client->irq) {
  975. ret = devm_request_threaded_irq(&client->dev, client->irq,
  976. NULL, tsl2591_event_handler,
  977. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  978. "tsl2591_irq", indio_dev);
  979. if (ret) {
  980. dev_err_probe(&client->dev, ret, "IRQ request error\n");
  981. return -EINVAL;
  982. }
  983. indio_dev->info = &tsl2591_info;
  984. } else {
  985. indio_dev->info = &tsl2591_info_no_irq;
  986. }
  987. mutex_init(&chip->als_mutex);
  988. ret = i2c_smbus_read_byte_data(client,
  989. TSL2591_CMD_NOP | TSL2591_DEVICE_ID);
  990. if (ret < 0) {
  991. dev_err(&client->dev,
  992. "Failed to read the device ID register\n");
  993. return ret;
  994. }
  995. ret = FIELD_GET(TSL2591_DEVICE_ID_MASK, ret);
  996. if (ret != TSL2591_DEVICE_ID_VAL) {
  997. dev_err(&client->dev, "Device ID: %#04x unknown\n", ret);
  998. return -EINVAL;
  999. }
  1000. indio_dev->channels = tsl2591_channels;
  1001. indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels);
  1002. indio_dev->modes = INDIO_DIRECT_MODE;
  1003. indio_dev->name = chip->client->name;
  1004. chip->events_enabled = false;
  1005. pm_runtime_enable(&client->dev);
  1006. pm_runtime_set_autosuspend_delay(&client->dev,
  1007. TSL2591_POWER_OFF_DELAY_MS);
  1008. pm_runtime_use_autosuspend(&client->dev);
  1009. /*
  1010. * Add chip off to automatically managed path and disable runtime
  1011. * power management. This ensures that the chip power management
  1012. * is handled correctly on driver remove. tsl2591_chip_off() must be
  1013. * added to the managed path after pm runtime is enabled and before
  1014. * any error exit paths are met to ensure we're not left in a state
  1015. * of pm runtime not being disabled properly.
  1016. */
  1017. ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off,
  1018. indio_dev);
  1019. if (ret < 0)
  1020. return -EINVAL;
  1021. ret = tsl2591_load_defaults(chip);
  1022. if (ret < 0) {
  1023. dev_err(&client->dev, "Failed to load sensor defaults\n");
  1024. return -EINVAL;
  1025. }
  1026. ret = i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
  1027. if (ret < 0) {
  1028. dev_err(&client->dev, "Failed to clear als irq\n");
  1029. return -EINVAL;
  1030. }
  1031. return devm_iio_device_register(&client->dev, indio_dev);
  1032. }
  1033. static const struct of_device_id tsl2591_of_match[] = {
  1034. { .compatible = "amstaos,tsl2591"},
  1035. {}
  1036. };
  1037. MODULE_DEVICE_TABLE(of, tsl2591_of_match);
  1038. static struct i2c_driver tsl2591_driver = {
  1039. .driver = {
  1040. .name = "tsl2591",
  1041. .pm = pm_ptr(&tsl2591_pm_ops),
  1042. .of_match_table = tsl2591_of_match,
  1043. },
  1044. .probe_new = tsl2591_probe
  1045. };
  1046. module_i2c_driver(tsl2591_driver);
  1047. MODULE_AUTHOR("Joe Sandom <[email protected]>");
  1048. MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");
  1049. MODULE_LICENSE("GPL");