tsl2772.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Device driver for monitoring ambient light intensity in (lux) and proximity
  4. * detection (prox) within the TAOS TSL2571, TSL2671, TMD2671, TSL2771, TMD2771,
  5. * TSL2572, TSL2672, TMD2672, TSL2772, and TMD2772 devices.
  6. *
  7. * Copyright (c) 2012, TAOS Corporation.
  8. * Copyright (c) 2017-2018 Brian Masney <[email protected]>
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/errno.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/property.h>
  18. #include <linux/slab.h>
  19. #include <linux/iio/events.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #include <linux/platform_data/tsl2772.h>
  23. #include <linux/regulator/consumer.h>
  24. /* Cal defs */
  25. #define PROX_STAT_CAL 0
  26. #define PROX_STAT_SAMP 1
  27. #define MAX_SAMPLES_CAL 200
  28. /* TSL2772 Device ID */
  29. #define TRITON_ID 0x00
  30. #define SWORDFISH_ID 0x30
  31. #define HALIBUT_ID 0x20
  32. /* Lux calculation constants */
  33. #define TSL2772_LUX_CALC_OVER_FLOW 65535
  34. /*
  35. * TAOS Register definitions - Note: depending on device, some of these register
  36. * are not used and the register address is benign.
  37. */
  38. /* Register offsets */
  39. #define TSL2772_MAX_CONFIG_REG 16
  40. /* Device Registers and Masks */
  41. #define TSL2772_CNTRL 0x00
  42. #define TSL2772_ALS_TIME 0X01
  43. #define TSL2772_PRX_TIME 0x02
  44. #define TSL2772_WAIT_TIME 0x03
  45. #define TSL2772_ALS_MINTHRESHLO 0X04
  46. #define TSL2772_ALS_MINTHRESHHI 0X05
  47. #define TSL2772_ALS_MAXTHRESHLO 0X06
  48. #define TSL2772_ALS_MAXTHRESHHI 0X07
  49. #define TSL2772_PRX_MINTHRESHLO 0X08
  50. #define TSL2772_PRX_MINTHRESHHI 0X09
  51. #define TSL2772_PRX_MAXTHRESHLO 0X0A
  52. #define TSL2772_PRX_MAXTHRESHHI 0X0B
  53. #define TSL2772_PERSISTENCE 0x0C
  54. #define TSL2772_ALS_PRX_CONFIG 0x0D
  55. #define TSL2772_PRX_COUNT 0x0E
  56. #define TSL2772_GAIN 0x0F
  57. #define TSL2772_NOTUSED 0x10
  58. #define TSL2772_REVID 0x11
  59. #define TSL2772_CHIPID 0x12
  60. #define TSL2772_STATUS 0x13
  61. #define TSL2772_ALS_CHAN0LO 0x14
  62. #define TSL2772_ALS_CHAN0HI 0x15
  63. #define TSL2772_ALS_CHAN1LO 0x16
  64. #define TSL2772_ALS_CHAN1HI 0x17
  65. #define TSL2772_PRX_LO 0x18
  66. #define TSL2772_PRX_HI 0x19
  67. /* tsl2772 cmd reg masks */
  68. #define TSL2772_CMD_REG 0x80
  69. #define TSL2772_CMD_SPL_FN 0x60
  70. #define TSL2772_CMD_REPEAT_PROTO 0x00
  71. #define TSL2772_CMD_AUTOINC_PROTO 0x20
  72. #define TSL2772_CMD_PROX_INT_CLR 0X05
  73. #define TSL2772_CMD_ALS_INT_CLR 0x06
  74. #define TSL2772_CMD_PROXALS_INT_CLR 0X07
  75. /* tsl2772 cntrl reg masks */
  76. #define TSL2772_CNTL_ADC_ENBL 0x02
  77. #define TSL2772_CNTL_PWR_ON 0x01
  78. /* tsl2772 status reg masks */
  79. #define TSL2772_STA_ADC_VALID 0x01
  80. #define TSL2772_STA_PRX_VALID 0x02
  81. #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
  82. TSL2772_STA_PRX_VALID)
  83. #define TSL2772_STA_ALS_INTR 0x10
  84. #define TSL2772_STA_PRX_INTR 0x20
  85. /* tsl2772 cntrl reg masks */
  86. #define TSL2772_CNTL_REG_CLEAR 0x00
  87. #define TSL2772_CNTL_PROX_INT_ENBL 0X20
  88. #define TSL2772_CNTL_ALS_INT_ENBL 0X10
  89. #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
  90. #define TSL2772_CNTL_PROX_DET_ENBL 0X04
  91. #define TSL2772_CNTL_PWRON 0x01
  92. #define TSL2772_CNTL_ALSPON_ENBL 0x03
  93. #define TSL2772_CNTL_INTALSPON_ENBL 0x13
  94. #define TSL2772_CNTL_PROXPON_ENBL 0x0F
  95. #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
  96. #define TSL2772_ALS_GAIN_TRIM_MIN 250
  97. #define TSL2772_ALS_GAIN_TRIM_MAX 4000
  98. #define TSL2772_MAX_PROX_LEDS 2
  99. #define TSL2772_BOOT_MIN_SLEEP_TIME 10000
  100. #define TSL2772_BOOT_MAX_SLEEP_TIME 28000
  101. /* Device family members */
  102. enum {
  103. tsl2571,
  104. tsl2671,
  105. tmd2671,
  106. tsl2771,
  107. tmd2771,
  108. tsl2572,
  109. tsl2672,
  110. tmd2672,
  111. tsl2772,
  112. tmd2772,
  113. apds9930,
  114. };
  115. enum {
  116. TSL2772_CHIP_UNKNOWN = 0,
  117. TSL2772_CHIP_WORKING = 1,
  118. TSL2772_CHIP_SUSPENDED = 2
  119. };
  120. enum {
  121. TSL2772_SUPPLY_VDD = 0,
  122. TSL2772_SUPPLY_VDDIO = 1,
  123. TSL2772_NUM_SUPPLIES = 2
  124. };
  125. /* Per-device data */
  126. struct tsl2772_als_info {
  127. u16 als_ch0;
  128. u16 als_ch1;
  129. u16 lux;
  130. };
  131. struct tsl2772_chip_info {
  132. int chan_table_elements;
  133. struct iio_chan_spec channel_with_events[4];
  134. struct iio_chan_spec channel_without_events[4];
  135. const struct iio_info *info;
  136. };
  137. static const int tsl2772_led_currents[][2] = {
  138. { 100000, TSL2772_100_mA },
  139. { 50000, TSL2772_50_mA },
  140. { 25000, TSL2772_25_mA },
  141. { 13000, TSL2772_13_mA },
  142. { 0, 0 }
  143. };
  144. struct tsl2772_chip {
  145. kernel_ulong_t id;
  146. struct mutex prox_mutex;
  147. struct mutex als_mutex;
  148. struct i2c_client *client;
  149. struct regulator_bulk_data supplies[TSL2772_NUM_SUPPLIES];
  150. u16 prox_data;
  151. struct tsl2772_als_info als_cur_info;
  152. struct tsl2772_settings settings;
  153. struct tsl2772_platform_data *pdata;
  154. int als_gain_time_scale;
  155. int als_saturation;
  156. int tsl2772_chip_status;
  157. u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
  158. const struct tsl2772_chip_info *chip_info;
  159. const struct iio_info *info;
  160. s64 event_timestamp;
  161. /*
  162. * This structure is intentionally large to accommodate
  163. * updates via sysfs.
  164. * Sized to 9 = max 8 segments + 1 termination segment
  165. */
  166. struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
  167. };
  168. /*
  169. * Different devices require different coefficents, and these numbers were
  170. * derived from the 'Lux Equation' section of the various device datasheets.
  171. * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
  172. * The coefficients are multiplied by 1000 to avoid floating point operations.
  173. * The two rows in each table correspond to the Lux1 and Lux2 equations from
  174. * the datasheets.
  175. */
  176. static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
  177. { 53000, 106000 },
  178. { 31800, 53000 },
  179. { 0, 0 },
  180. };
  181. static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
  182. { 24000, 48000 },
  183. { 14400, 24000 },
  184. { 0, 0 },
  185. };
  186. static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
  187. { 60000, 112200 },
  188. { 37800, 60000 },
  189. { 0, 0 },
  190. };
  191. static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
  192. { 20000, 35000 },
  193. { 12600, 20000 },
  194. { 0, 0 },
  195. };
  196. static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
  197. { 52000, 96824 },
  198. { 38792, 67132 },
  199. { 0, 0 },
  200. };
  201. static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
  202. [tsl2571] = tsl2x71_lux_table,
  203. [tsl2671] = tsl2x71_lux_table,
  204. [tmd2671] = tmd2x71_lux_table,
  205. [tsl2771] = tsl2x71_lux_table,
  206. [tmd2771] = tmd2x71_lux_table,
  207. [tsl2572] = tsl2x72_lux_table,
  208. [tsl2672] = tsl2x72_lux_table,
  209. [tmd2672] = tmd2x72_lux_table,
  210. [tsl2772] = tsl2x72_lux_table,
  211. [tmd2772] = tmd2x72_lux_table,
  212. [apds9930] = apds9930_lux_table,
  213. };
  214. static const struct tsl2772_settings tsl2772_default_settings = {
  215. .als_time = 255, /* 2.72 / 2.73 ms */
  216. .als_gain = 0,
  217. .prox_time = 255, /* 2.72 / 2.73 ms */
  218. .prox_gain = 0,
  219. .wait_time = 255,
  220. .als_prox_config = 0,
  221. .als_gain_trim = 1000,
  222. .als_cal_target = 150,
  223. .als_persistence = 1,
  224. .als_interrupt_en = false,
  225. .als_thresh_low = 200,
  226. .als_thresh_high = 256,
  227. .prox_persistence = 1,
  228. .prox_interrupt_en = false,
  229. .prox_thres_low = 0,
  230. .prox_thres_high = 512,
  231. .prox_max_samples_cal = 30,
  232. .prox_pulse_count = 8,
  233. .prox_diode = TSL2772_DIODE1,
  234. .prox_power = TSL2772_100_mA
  235. };
  236. static const s16 tsl2772_als_gain[] = {
  237. 1,
  238. 8,
  239. 16,
  240. 120
  241. };
  242. static const s16 tsl2772_prox_gain[] = {
  243. 1,
  244. 2,
  245. 4,
  246. 8
  247. };
  248. static const int tsl2772_int_time_avail[][6] = {
  249. [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
  250. [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
  251. [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
  252. [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
  253. [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
  254. [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
  255. [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
  256. [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
  257. [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
  258. [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
  259. [apds9930] = { 0, 2730, 0, 2730, 0, 699000 },
  260. };
  261. static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
  262. static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
  263. /* Channel variations */
  264. enum {
  265. ALS,
  266. PRX,
  267. ALSPRX,
  268. PRX2,
  269. ALSPRX2,
  270. };
  271. static const u8 device_channel_config[] = {
  272. [tsl2571] = ALS,
  273. [tsl2671] = PRX,
  274. [tmd2671] = PRX,
  275. [tsl2771] = ALSPRX,
  276. [tmd2771] = ALSPRX,
  277. [tsl2572] = ALS,
  278. [tsl2672] = PRX2,
  279. [tmd2672] = PRX2,
  280. [tsl2772] = ALSPRX2,
  281. [tmd2772] = ALSPRX2,
  282. [apds9930] = ALSPRX2,
  283. };
  284. static int tsl2772_read_status(struct tsl2772_chip *chip)
  285. {
  286. int ret;
  287. ret = i2c_smbus_read_byte_data(chip->client,
  288. TSL2772_CMD_REG | TSL2772_STATUS);
  289. if (ret < 0)
  290. dev_err(&chip->client->dev,
  291. "%s: failed to read STATUS register: %d\n", __func__,
  292. ret);
  293. return ret;
  294. }
  295. static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
  296. {
  297. int ret;
  298. ret = i2c_smbus_write_byte_data(chip->client,
  299. TSL2772_CMD_REG | TSL2772_CNTRL, data);
  300. if (ret < 0) {
  301. dev_err(&chip->client->dev,
  302. "%s: failed to write to control register %x: %d\n",
  303. __func__, data, ret);
  304. }
  305. return ret;
  306. }
  307. static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
  308. int upper_reg)
  309. {
  310. u8 buf[2];
  311. int ret;
  312. ret = i2c_smbus_write_byte(chip->client,
  313. TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
  314. lower_reg);
  315. if (ret < 0) {
  316. dev_err(&chip->client->dev,
  317. "%s: failed to enable auto increment protocol: %d\n",
  318. __func__, ret);
  319. return ret;
  320. }
  321. ret = i2c_smbus_read_byte_data(chip->client,
  322. TSL2772_CMD_REG | lower_reg);
  323. if (ret < 0) {
  324. dev_err(&chip->client->dev,
  325. "%s: failed to read from register %x: %d\n", __func__,
  326. lower_reg, ret);
  327. return ret;
  328. }
  329. buf[0] = ret;
  330. ret = i2c_smbus_read_byte_data(chip->client,
  331. TSL2772_CMD_REG | upper_reg);
  332. if (ret < 0) {
  333. dev_err(&chip->client->dev,
  334. "%s: failed to read from register %x: %d\n", __func__,
  335. upper_reg, ret);
  336. return ret;
  337. }
  338. buf[1] = ret;
  339. ret = i2c_smbus_write_byte(chip->client,
  340. TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
  341. lower_reg);
  342. if (ret < 0) {
  343. dev_err(&chip->client->dev,
  344. "%s: failed to enable repeated byte protocol: %d\n",
  345. __func__, ret);
  346. return ret;
  347. }
  348. return le16_to_cpup((const __le16 *)&buf[0]);
  349. }
  350. /**
  351. * tsl2772_get_lux() - Reads and calculates current lux value.
  352. * @indio_dev: pointer to IIO device
  353. *
  354. * The raw ch0 and ch1 values of the ambient light sensed in the last
  355. * integration cycle are read from the device. The raw values are multiplied
  356. * by a device-specific scale factor, and divided by the integration time and
  357. * device gain. The code supports multiple lux equations through the lux table
  358. * coefficients. A lux gain trim is applied to each lux equation, and then the
  359. * maximum lux within the interval 0..65535 is selected.
  360. */
  361. static int tsl2772_get_lux(struct iio_dev *indio_dev)
  362. {
  363. struct tsl2772_chip *chip = iio_priv(indio_dev);
  364. struct tsl2772_lux *p;
  365. int max_lux, ret;
  366. bool overflow;
  367. mutex_lock(&chip->als_mutex);
  368. if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
  369. dev_err(&chip->client->dev, "%s: device is not enabled\n",
  370. __func__);
  371. ret = -EBUSY;
  372. goto out_unlock;
  373. }
  374. ret = tsl2772_read_status(chip);
  375. if (ret < 0)
  376. goto out_unlock;
  377. if (!(ret & TSL2772_STA_ADC_VALID)) {
  378. dev_err(&chip->client->dev,
  379. "%s: data not valid yet\n", __func__);
  380. ret = chip->als_cur_info.lux; /* return LAST VALUE */
  381. goto out_unlock;
  382. }
  383. ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
  384. TSL2772_ALS_CHAN0HI);
  385. if (ret < 0)
  386. goto out_unlock;
  387. chip->als_cur_info.als_ch0 = ret;
  388. ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
  389. TSL2772_ALS_CHAN1HI);
  390. if (ret < 0)
  391. goto out_unlock;
  392. chip->als_cur_info.als_ch1 = ret;
  393. if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
  394. max_lux = TSL2772_LUX_CALC_OVER_FLOW;
  395. goto update_struct_with_max_lux;
  396. }
  397. if (!chip->als_cur_info.als_ch0) {
  398. /* have no data, so return LAST VALUE */
  399. ret = chip->als_cur_info.lux;
  400. goto out_unlock;
  401. }
  402. max_lux = 0;
  403. overflow = false;
  404. for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
  405. p++) {
  406. int lux;
  407. lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
  408. (chip->als_cur_info.als_ch1 * p->ch1)) /
  409. chip->als_gain_time_scale;
  410. /*
  411. * The als_gain_trim can have a value within the range 250..4000
  412. * and is a multiplier for the lux. A trim of 1000 makes no
  413. * changes to the lux, less than 1000 scales it down, and
  414. * greater than 1000 scales it up.
  415. */
  416. lux = (lux * chip->settings.als_gain_trim) / 1000;
  417. if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
  418. overflow = true;
  419. continue;
  420. }
  421. max_lux = max(max_lux, lux);
  422. }
  423. if (overflow && max_lux == 0)
  424. max_lux = TSL2772_LUX_CALC_OVER_FLOW;
  425. update_struct_with_max_lux:
  426. chip->als_cur_info.lux = max_lux;
  427. ret = max_lux;
  428. out_unlock:
  429. mutex_unlock(&chip->als_mutex);
  430. return ret;
  431. }
  432. /**
  433. * tsl2772_get_prox() - Reads proximity data registers and updates
  434. * chip->prox_data.
  435. *
  436. * @indio_dev: pointer to IIO device
  437. */
  438. static int tsl2772_get_prox(struct iio_dev *indio_dev)
  439. {
  440. struct tsl2772_chip *chip = iio_priv(indio_dev);
  441. int ret;
  442. mutex_lock(&chip->prox_mutex);
  443. ret = tsl2772_read_status(chip);
  444. if (ret < 0)
  445. goto prox_poll_err;
  446. switch (chip->id) {
  447. case tsl2571:
  448. case tsl2671:
  449. case tmd2671:
  450. case tsl2771:
  451. case tmd2771:
  452. if (!(ret & TSL2772_STA_ADC_VALID)) {
  453. ret = -EINVAL;
  454. goto prox_poll_err;
  455. }
  456. break;
  457. case tsl2572:
  458. case tsl2672:
  459. case tmd2672:
  460. case tsl2772:
  461. case tmd2772:
  462. case apds9930:
  463. if (!(ret & TSL2772_STA_PRX_VALID)) {
  464. ret = -EINVAL;
  465. goto prox_poll_err;
  466. }
  467. break;
  468. }
  469. ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
  470. if (ret < 0)
  471. goto prox_poll_err;
  472. chip->prox_data = ret;
  473. prox_poll_err:
  474. mutex_unlock(&chip->prox_mutex);
  475. return ret;
  476. }
  477. static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip)
  478. {
  479. struct device *dev = &chip->client->dev;
  480. int ret, tmp, i;
  481. ret = device_property_read_u32(dev, "led-max-microamp", &tmp);
  482. if (ret < 0)
  483. return ret;
  484. for (i = 0; tsl2772_led_currents[i][0] != 0; i++) {
  485. if (tmp == tsl2772_led_currents[i][0]) {
  486. chip->settings.prox_power = tsl2772_led_currents[i][1];
  487. return 0;
  488. }
  489. }
  490. dev_err(dev, "Invalid value %d for led-max-microamp\n", tmp);
  491. return -EINVAL;
  492. }
  493. static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip)
  494. {
  495. struct device *dev = &chip->client->dev;
  496. int i, ret, num_leds, prox_diode_mask;
  497. u32 leds[TSL2772_MAX_PROX_LEDS];
  498. ret = device_property_count_u32(dev, "amstaos,proximity-diodes");
  499. if (ret < 0)
  500. return ret;
  501. num_leds = ret;
  502. if (num_leds > TSL2772_MAX_PROX_LEDS)
  503. num_leds = TSL2772_MAX_PROX_LEDS;
  504. ret = device_property_read_u32_array(dev, "amstaos,proximity-diodes", leds, num_leds);
  505. if (ret < 0) {
  506. dev_err(dev, "Invalid value for amstaos,proximity-diodes: %d.\n", ret);
  507. return ret;
  508. }
  509. prox_diode_mask = 0;
  510. for (i = 0; i < num_leds; i++) {
  511. if (leds[i] == 0)
  512. prox_diode_mask |= TSL2772_DIODE0;
  513. else if (leds[i] == 1)
  514. prox_diode_mask |= TSL2772_DIODE1;
  515. else {
  516. dev_err(dev, "Invalid value %d in amstaos,proximity-diodes.\n", leds[i]);
  517. return -EINVAL;
  518. }
  519. }
  520. chip->settings.prox_diode = prox_diode_mask;
  521. return 0;
  522. }
  523. static void tsl2772_parse_dt(struct tsl2772_chip *chip)
  524. {
  525. tsl2772_read_prox_led_current(chip);
  526. tsl2772_read_prox_diodes(chip);
  527. }
  528. /**
  529. * tsl2772_defaults() - Populates the device nominal operating parameters
  530. * with those provided by a 'platform' data struct or
  531. * with prefined defaults.
  532. *
  533. * @chip: pointer to device structure.
  534. */
  535. static void tsl2772_defaults(struct tsl2772_chip *chip)
  536. {
  537. /* If Operational settings defined elsewhere.. */
  538. if (chip->pdata && chip->pdata->platform_default_settings)
  539. memcpy(&chip->settings, chip->pdata->platform_default_settings,
  540. sizeof(tsl2772_default_settings));
  541. else
  542. memcpy(&chip->settings, &tsl2772_default_settings,
  543. sizeof(tsl2772_default_settings));
  544. /* Load up the proper lux table. */
  545. if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
  546. memcpy(chip->tsl2772_device_lux,
  547. chip->pdata->platform_lux_table,
  548. sizeof(chip->pdata->platform_lux_table));
  549. else
  550. memcpy(chip->tsl2772_device_lux,
  551. tsl2772_default_lux_table_group[chip->id],
  552. TSL2772_DEFAULT_TABLE_BYTES);
  553. tsl2772_parse_dt(chip);
  554. }
  555. /**
  556. * tsl2772_als_calibrate() - Obtain single reading and calculate
  557. * the als_gain_trim.
  558. *
  559. * @indio_dev: pointer to IIO device
  560. */
  561. static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
  562. {
  563. struct tsl2772_chip *chip = iio_priv(indio_dev);
  564. int ret, lux_val;
  565. ret = i2c_smbus_read_byte_data(chip->client,
  566. TSL2772_CMD_REG | TSL2772_CNTRL);
  567. if (ret < 0) {
  568. dev_err(&chip->client->dev,
  569. "%s: failed to read from the CNTRL register\n",
  570. __func__);
  571. return ret;
  572. }
  573. if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
  574. != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
  575. dev_err(&chip->client->dev,
  576. "%s: Device is not powered on and/or ADC is not enabled\n",
  577. __func__);
  578. return -EINVAL;
  579. } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
  580. dev_err(&chip->client->dev,
  581. "%s: The two ADC channels have not completed an integration cycle\n",
  582. __func__);
  583. return -ENODATA;
  584. }
  585. lux_val = tsl2772_get_lux(indio_dev);
  586. if (lux_val < 0) {
  587. dev_err(&chip->client->dev,
  588. "%s: failed to get lux\n", __func__);
  589. return lux_val;
  590. }
  591. if (lux_val == 0)
  592. return -ERANGE;
  593. ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
  594. lux_val;
  595. if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
  596. return -ERANGE;
  597. chip->settings.als_gain_trim = ret;
  598. return ret;
  599. }
  600. static void tsl2772_disable_regulators_action(void *_data)
  601. {
  602. struct tsl2772_chip *chip = _data;
  603. regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
  604. }
  605. static int tsl2772_chip_on(struct iio_dev *indio_dev)
  606. {
  607. struct tsl2772_chip *chip = iio_priv(indio_dev);
  608. int ret, i, als_count, als_time_us;
  609. u8 *dev_reg, reg_val;
  610. /* Non calculated parameters */
  611. chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
  612. chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
  613. chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
  614. chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
  615. chip->settings.als_prox_config;
  616. chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
  617. (chip->settings.als_thresh_low) & 0xFF;
  618. chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
  619. (chip->settings.als_thresh_low >> 8) & 0xFF;
  620. chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
  621. (chip->settings.als_thresh_high) & 0xFF;
  622. chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
  623. (chip->settings.als_thresh_high >> 8) & 0xFF;
  624. chip->tsl2772_config[TSL2772_PERSISTENCE] =
  625. (chip->settings.prox_persistence & 0xFF) << 4 |
  626. (chip->settings.als_persistence & 0xFF);
  627. chip->tsl2772_config[TSL2772_PRX_COUNT] =
  628. chip->settings.prox_pulse_count;
  629. chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
  630. (chip->settings.prox_thres_low) & 0xFF;
  631. chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
  632. (chip->settings.prox_thres_low >> 8) & 0xFF;
  633. chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
  634. (chip->settings.prox_thres_high) & 0xFF;
  635. chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
  636. (chip->settings.prox_thres_high >> 8) & 0xFF;
  637. /* and make sure we're not already on */
  638. if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
  639. /* if forcing a register update - turn off, then on */
  640. dev_info(&chip->client->dev, "device is already enabled\n");
  641. return -EINVAL;
  642. }
  643. /* Set the gain based on tsl2772_settings struct */
  644. chip->tsl2772_config[TSL2772_GAIN] =
  645. (chip->settings.als_gain & 0xFF) |
  646. ((chip->settings.prox_gain & 0xFF) << 2) |
  647. (chip->settings.prox_diode << 4) |
  648. (chip->settings.prox_power << 6);
  649. /* set chip time scaling and saturation */
  650. als_count = 256 - chip->settings.als_time;
  651. als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
  652. chip->als_saturation = als_count * 768; /* 75% of full scale */
  653. chip->als_gain_time_scale = als_time_us *
  654. tsl2772_als_gain[chip->settings.als_gain];
  655. /*
  656. * TSL2772 Specific power-on / adc enable sequence
  657. * Power on the device 1st.
  658. */
  659. ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
  660. if (ret < 0)
  661. return ret;
  662. /*
  663. * Use the following shadow copy for our delay before enabling ADC.
  664. * Write all the registers.
  665. */
  666. for (i = 0, dev_reg = chip->tsl2772_config;
  667. i < TSL2772_MAX_CONFIG_REG; i++) {
  668. int reg = TSL2772_CMD_REG + i;
  669. ret = i2c_smbus_write_byte_data(chip->client, reg,
  670. *dev_reg++);
  671. if (ret < 0) {
  672. dev_err(&chip->client->dev,
  673. "%s: failed to write to register %x: %d\n",
  674. __func__, reg, ret);
  675. return ret;
  676. }
  677. }
  678. /* Power-on settling time */
  679. usleep_range(3000, 3500);
  680. reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
  681. TSL2772_CNTL_PROX_DET_ENBL;
  682. if (chip->settings.als_interrupt_en)
  683. reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
  684. if (chip->settings.prox_interrupt_en)
  685. reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
  686. ret = tsl2772_write_control_reg(chip, reg_val);
  687. if (ret < 0)
  688. return ret;
  689. ret = i2c_smbus_write_byte(chip->client,
  690. TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
  691. TSL2772_CMD_PROXALS_INT_CLR);
  692. if (ret < 0) {
  693. dev_err(&chip->client->dev,
  694. "%s: failed to clear interrupt status: %d\n",
  695. __func__, ret);
  696. return ret;
  697. }
  698. chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
  699. return ret;
  700. }
  701. static int tsl2772_chip_off(struct iio_dev *indio_dev)
  702. {
  703. struct tsl2772_chip *chip = iio_priv(indio_dev);
  704. /* turn device off */
  705. chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
  706. return tsl2772_write_control_reg(chip, 0x00);
  707. }
  708. static void tsl2772_chip_off_action(void *data)
  709. {
  710. struct iio_dev *indio_dev = data;
  711. tsl2772_chip_off(indio_dev);
  712. }
  713. /**
  714. * tsl2772_invoke_change - power cycle the device to implement the user
  715. * parameters
  716. * @indio_dev: pointer to IIO device
  717. *
  718. * Obtain and lock both ALS and PROX resources, determine and save device state
  719. * (On/Off), cycle device to implement updated parameter, put device back into
  720. * proper state, and unlock resource.
  721. */
  722. static int tsl2772_invoke_change(struct iio_dev *indio_dev)
  723. {
  724. struct tsl2772_chip *chip = iio_priv(indio_dev);
  725. int device_status = chip->tsl2772_chip_status;
  726. int ret;
  727. mutex_lock(&chip->als_mutex);
  728. mutex_lock(&chip->prox_mutex);
  729. if (device_status == TSL2772_CHIP_WORKING) {
  730. ret = tsl2772_chip_off(indio_dev);
  731. if (ret < 0)
  732. goto unlock;
  733. }
  734. ret = tsl2772_chip_on(indio_dev);
  735. unlock:
  736. mutex_unlock(&chip->prox_mutex);
  737. mutex_unlock(&chip->als_mutex);
  738. return ret;
  739. }
  740. static int tsl2772_prox_cal(struct iio_dev *indio_dev)
  741. {
  742. struct tsl2772_chip *chip = iio_priv(indio_dev);
  743. int prox_history[MAX_SAMPLES_CAL + 1];
  744. int i, ret, mean, max, sample_sum;
  745. if (chip->settings.prox_max_samples_cal < 1 ||
  746. chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
  747. return -EINVAL;
  748. for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
  749. usleep_range(15000, 17500);
  750. ret = tsl2772_get_prox(indio_dev);
  751. if (ret < 0)
  752. return ret;
  753. prox_history[i] = chip->prox_data;
  754. }
  755. sample_sum = 0;
  756. max = INT_MIN;
  757. for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
  758. sample_sum += prox_history[i];
  759. max = max(max, prox_history[i]);
  760. }
  761. mean = sample_sum / chip->settings.prox_max_samples_cal;
  762. chip->settings.prox_thres_high = (max << 1) - mean;
  763. return tsl2772_invoke_change(indio_dev);
  764. }
  765. static int tsl2772_read_avail(struct iio_dev *indio_dev,
  766. struct iio_chan_spec const *chan,
  767. const int **vals, int *type, int *length,
  768. long mask)
  769. {
  770. struct tsl2772_chip *chip = iio_priv(indio_dev);
  771. switch (mask) {
  772. case IIO_CHAN_INFO_CALIBSCALE:
  773. if (chan->type == IIO_INTENSITY) {
  774. *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
  775. *vals = tsl2772_int_calibscale_avail;
  776. } else {
  777. *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
  778. *vals = tsl2772_prox_calibscale_avail;
  779. }
  780. *type = IIO_VAL_INT;
  781. return IIO_AVAIL_LIST;
  782. case IIO_CHAN_INFO_INT_TIME:
  783. *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
  784. *vals = tsl2772_int_time_avail[chip->id];
  785. *type = IIO_VAL_INT_PLUS_MICRO;
  786. return IIO_AVAIL_RANGE;
  787. }
  788. return -EINVAL;
  789. }
  790. static ssize_t in_illuminance0_target_input_show(struct device *dev,
  791. struct device_attribute *attr,
  792. char *buf)
  793. {
  794. struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
  795. return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
  796. }
  797. static ssize_t in_illuminance0_target_input_store(struct device *dev,
  798. struct device_attribute *attr,
  799. const char *buf, size_t len)
  800. {
  801. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  802. struct tsl2772_chip *chip = iio_priv(indio_dev);
  803. u16 value;
  804. int ret;
  805. if (kstrtou16(buf, 0, &value))
  806. return -EINVAL;
  807. chip->settings.als_cal_target = value;
  808. ret = tsl2772_invoke_change(indio_dev);
  809. if (ret < 0)
  810. return ret;
  811. return len;
  812. }
  813. static ssize_t in_illuminance0_calibrate_store(struct device *dev,
  814. struct device_attribute *attr,
  815. const char *buf, size_t len)
  816. {
  817. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  818. bool value;
  819. int ret;
  820. if (kstrtobool(buf, &value) || !value)
  821. return -EINVAL;
  822. ret = tsl2772_als_calibrate(indio_dev);
  823. if (ret < 0)
  824. return ret;
  825. ret = tsl2772_invoke_change(indio_dev);
  826. if (ret < 0)
  827. return ret;
  828. return len;
  829. }
  830. static ssize_t in_illuminance0_lux_table_show(struct device *dev,
  831. struct device_attribute *attr,
  832. char *buf)
  833. {
  834. struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
  835. int i = 0;
  836. int offset = 0;
  837. while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
  838. offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,",
  839. chip->tsl2772_device_lux[i].ch0,
  840. chip->tsl2772_device_lux[i].ch1);
  841. if (chip->tsl2772_device_lux[i].ch0 == 0) {
  842. /*
  843. * We just printed the first "0" entry.
  844. * Now get rid of the extra "," and break.
  845. */
  846. offset--;
  847. break;
  848. }
  849. i++;
  850. }
  851. offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n");
  852. return offset;
  853. }
  854. static ssize_t in_illuminance0_lux_table_store(struct device *dev,
  855. struct device_attribute *attr,
  856. const char *buf, size_t len)
  857. {
  858. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  859. struct tsl2772_chip *chip = iio_priv(indio_dev);
  860. int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
  861. int n, ret;
  862. get_options(buf, ARRAY_SIZE(value), value);
  863. /*
  864. * We now have an array of ints starting at value[1], and
  865. * enumerated by value[0].
  866. * We expect each group of two ints to be one table entry,
  867. * and the last table entry is all 0.
  868. */
  869. n = value[0];
  870. if ((n % 2) || n < 4 ||
  871. n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
  872. return -EINVAL;
  873. if ((value[(n - 1)] | value[n]) != 0)
  874. return -EINVAL;
  875. if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
  876. ret = tsl2772_chip_off(indio_dev);
  877. if (ret < 0)
  878. return ret;
  879. }
  880. /* Zero out the table */
  881. memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
  882. memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
  883. ret = tsl2772_invoke_change(indio_dev);
  884. if (ret < 0)
  885. return ret;
  886. return len;
  887. }
  888. static ssize_t in_proximity0_calibrate_store(struct device *dev,
  889. struct device_attribute *attr,
  890. const char *buf, size_t len)
  891. {
  892. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  893. bool value;
  894. int ret;
  895. if (kstrtobool(buf, &value) || !value)
  896. return -EINVAL;
  897. ret = tsl2772_prox_cal(indio_dev);
  898. if (ret < 0)
  899. return ret;
  900. ret = tsl2772_invoke_change(indio_dev);
  901. if (ret < 0)
  902. return ret;
  903. return len;
  904. }
  905. static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
  906. const struct iio_chan_spec *chan,
  907. enum iio_event_type type,
  908. enum iio_event_direction dir)
  909. {
  910. struct tsl2772_chip *chip = iio_priv(indio_dev);
  911. if (chan->type == IIO_INTENSITY)
  912. return chip->settings.als_interrupt_en;
  913. else
  914. return chip->settings.prox_interrupt_en;
  915. }
  916. static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
  917. const struct iio_chan_spec *chan,
  918. enum iio_event_type type,
  919. enum iio_event_direction dir,
  920. int val)
  921. {
  922. struct tsl2772_chip *chip = iio_priv(indio_dev);
  923. if (chan->type == IIO_INTENSITY)
  924. chip->settings.als_interrupt_en = val ? true : false;
  925. else
  926. chip->settings.prox_interrupt_en = val ? true : false;
  927. return tsl2772_invoke_change(indio_dev);
  928. }
  929. static int tsl2772_write_event_value(struct iio_dev *indio_dev,
  930. const struct iio_chan_spec *chan,
  931. enum iio_event_type type,
  932. enum iio_event_direction dir,
  933. enum iio_event_info info,
  934. int val, int val2)
  935. {
  936. struct tsl2772_chip *chip = iio_priv(indio_dev);
  937. int ret = -EINVAL, count, persistence;
  938. u8 time;
  939. switch (info) {
  940. case IIO_EV_INFO_VALUE:
  941. if (chan->type == IIO_INTENSITY) {
  942. switch (dir) {
  943. case IIO_EV_DIR_RISING:
  944. chip->settings.als_thresh_high = val;
  945. ret = 0;
  946. break;
  947. case IIO_EV_DIR_FALLING:
  948. chip->settings.als_thresh_low = val;
  949. ret = 0;
  950. break;
  951. default:
  952. break;
  953. }
  954. } else {
  955. switch (dir) {
  956. case IIO_EV_DIR_RISING:
  957. chip->settings.prox_thres_high = val;
  958. ret = 0;
  959. break;
  960. case IIO_EV_DIR_FALLING:
  961. chip->settings.prox_thres_low = val;
  962. ret = 0;
  963. break;
  964. default:
  965. break;
  966. }
  967. }
  968. break;
  969. case IIO_EV_INFO_PERIOD:
  970. if (chan->type == IIO_INTENSITY)
  971. time = chip->settings.als_time;
  972. else
  973. time = chip->settings.prox_time;
  974. count = 256 - time;
  975. persistence = ((val * 1000000) + val2) /
  976. (count * tsl2772_int_time_avail[chip->id][3]);
  977. if (chan->type == IIO_INTENSITY) {
  978. /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
  979. if (persistence > 3)
  980. persistence = (persistence / 5) + 3;
  981. chip->settings.als_persistence = persistence;
  982. } else {
  983. chip->settings.prox_persistence = persistence;
  984. }
  985. ret = 0;
  986. break;
  987. default:
  988. break;
  989. }
  990. if (ret < 0)
  991. return ret;
  992. return tsl2772_invoke_change(indio_dev);
  993. }
  994. static int tsl2772_read_event_value(struct iio_dev *indio_dev,
  995. const struct iio_chan_spec *chan,
  996. enum iio_event_type type,
  997. enum iio_event_direction dir,
  998. enum iio_event_info info,
  999. int *val, int *val2)
  1000. {
  1001. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1002. int filter_delay, persistence;
  1003. u8 time;
  1004. switch (info) {
  1005. case IIO_EV_INFO_VALUE:
  1006. if (chan->type == IIO_INTENSITY) {
  1007. switch (dir) {
  1008. case IIO_EV_DIR_RISING:
  1009. *val = chip->settings.als_thresh_high;
  1010. return IIO_VAL_INT;
  1011. case IIO_EV_DIR_FALLING:
  1012. *val = chip->settings.als_thresh_low;
  1013. return IIO_VAL_INT;
  1014. default:
  1015. return -EINVAL;
  1016. }
  1017. } else {
  1018. switch (dir) {
  1019. case IIO_EV_DIR_RISING:
  1020. *val = chip->settings.prox_thres_high;
  1021. return IIO_VAL_INT;
  1022. case IIO_EV_DIR_FALLING:
  1023. *val = chip->settings.prox_thres_low;
  1024. return IIO_VAL_INT;
  1025. default:
  1026. return -EINVAL;
  1027. }
  1028. }
  1029. break;
  1030. case IIO_EV_INFO_PERIOD:
  1031. if (chan->type == IIO_INTENSITY) {
  1032. time = chip->settings.als_time;
  1033. persistence = chip->settings.als_persistence;
  1034. /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
  1035. if (persistence > 3)
  1036. persistence = (persistence - 3) * 5;
  1037. } else {
  1038. time = chip->settings.prox_time;
  1039. persistence = chip->settings.prox_persistence;
  1040. }
  1041. filter_delay = persistence * (256 - time) *
  1042. tsl2772_int_time_avail[chip->id][3];
  1043. *val = filter_delay / 1000000;
  1044. *val2 = filter_delay % 1000000;
  1045. return IIO_VAL_INT_PLUS_MICRO;
  1046. default:
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. static int tsl2772_read_raw(struct iio_dev *indio_dev,
  1051. struct iio_chan_spec const *chan,
  1052. int *val,
  1053. int *val2,
  1054. long mask)
  1055. {
  1056. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1057. switch (mask) {
  1058. case IIO_CHAN_INFO_PROCESSED:
  1059. switch (chan->type) {
  1060. case IIO_LIGHT:
  1061. tsl2772_get_lux(indio_dev);
  1062. *val = chip->als_cur_info.lux;
  1063. return IIO_VAL_INT;
  1064. default:
  1065. return -EINVAL;
  1066. }
  1067. case IIO_CHAN_INFO_RAW:
  1068. switch (chan->type) {
  1069. case IIO_INTENSITY:
  1070. tsl2772_get_lux(indio_dev);
  1071. if (chan->channel == 0)
  1072. *val = chip->als_cur_info.als_ch0;
  1073. else
  1074. *val = chip->als_cur_info.als_ch1;
  1075. return IIO_VAL_INT;
  1076. case IIO_PROXIMITY:
  1077. tsl2772_get_prox(indio_dev);
  1078. *val = chip->prox_data;
  1079. return IIO_VAL_INT;
  1080. default:
  1081. return -EINVAL;
  1082. }
  1083. break;
  1084. case IIO_CHAN_INFO_CALIBSCALE:
  1085. if (chan->type == IIO_LIGHT)
  1086. *val = tsl2772_als_gain[chip->settings.als_gain];
  1087. else
  1088. *val = tsl2772_prox_gain[chip->settings.prox_gain];
  1089. return IIO_VAL_INT;
  1090. case IIO_CHAN_INFO_CALIBBIAS:
  1091. *val = chip->settings.als_gain_trim;
  1092. return IIO_VAL_INT;
  1093. case IIO_CHAN_INFO_INT_TIME:
  1094. *val = 0;
  1095. *val2 = (256 - chip->settings.als_time) *
  1096. tsl2772_int_time_avail[chip->id][3];
  1097. return IIO_VAL_INT_PLUS_MICRO;
  1098. default:
  1099. return -EINVAL;
  1100. }
  1101. }
  1102. static int tsl2772_write_raw(struct iio_dev *indio_dev,
  1103. struct iio_chan_spec const *chan,
  1104. int val,
  1105. int val2,
  1106. long mask)
  1107. {
  1108. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1109. switch (mask) {
  1110. case IIO_CHAN_INFO_CALIBSCALE:
  1111. if (chan->type == IIO_INTENSITY) {
  1112. switch (val) {
  1113. case 1:
  1114. chip->settings.als_gain = 0;
  1115. break;
  1116. case 8:
  1117. chip->settings.als_gain = 1;
  1118. break;
  1119. case 16:
  1120. chip->settings.als_gain = 2;
  1121. break;
  1122. case 120:
  1123. chip->settings.als_gain = 3;
  1124. break;
  1125. default:
  1126. return -EINVAL;
  1127. }
  1128. } else {
  1129. switch (val) {
  1130. case 1:
  1131. chip->settings.prox_gain = 0;
  1132. break;
  1133. case 2:
  1134. chip->settings.prox_gain = 1;
  1135. break;
  1136. case 4:
  1137. chip->settings.prox_gain = 2;
  1138. break;
  1139. case 8:
  1140. chip->settings.prox_gain = 3;
  1141. break;
  1142. default:
  1143. return -EINVAL;
  1144. }
  1145. }
  1146. break;
  1147. case IIO_CHAN_INFO_CALIBBIAS:
  1148. if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
  1149. val > TSL2772_ALS_GAIN_TRIM_MAX)
  1150. return -EINVAL;
  1151. chip->settings.als_gain_trim = val;
  1152. break;
  1153. case IIO_CHAN_INFO_INT_TIME:
  1154. if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
  1155. val2 > tsl2772_int_time_avail[chip->id][5])
  1156. return -EINVAL;
  1157. chip->settings.als_time = 256 -
  1158. (val2 / tsl2772_int_time_avail[chip->id][3]);
  1159. break;
  1160. default:
  1161. return -EINVAL;
  1162. }
  1163. return tsl2772_invoke_change(indio_dev);
  1164. }
  1165. static DEVICE_ATTR_RW(in_illuminance0_target_input);
  1166. static DEVICE_ATTR_WO(in_illuminance0_calibrate);
  1167. static DEVICE_ATTR_WO(in_proximity0_calibrate);
  1168. static DEVICE_ATTR_RW(in_illuminance0_lux_table);
  1169. /* Use the default register values to identify the Taos device */
  1170. static int tsl2772_device_id_verif(int id, int target)
  1171. {
  1172. switch (target) {
  1173. case tsl2571:
  1174. case tsl2671:
  1175. case tsl2771:
  1176. return (id & 0xf0) == TRITON_ID;
  1177. case tmd2671:
  1178. case tmd2771:
  1179. return (id & 0xf0) == HALIBUT_ID;
  1180. case tsl2572:
  1181. case tsl2672:
  1182. case tmd2672:
  1183. case tsl2772:
  1184. case tmd2772:
  1185. case apds9930:
  1186. return (id & 0xf0) == SWORDFISH_ID;
  1187. }
  1188. return -EINVAL;
  1189. }
  1190. static irqreturn_t tsl2772_event_handler(int irq, void *private)
  1191. {
  1192. struct iio_dev *indio_dev = private;
  1193. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1194. s64 timestamp = iio_get_time_ns(indio_dev);
  1195. int ret;
  1196. ret = tsl2772_read_status(chip);
  1197. if (ret < 0)
  1198. return IRQ_HANDLED;
  1199. /* What type of interrupt do we need to process */
  1200. if (ret & TSL2772_STA_PRX_INTR) {
  1201. iio_push_event(indio_dev,
  1202. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
  1203. 0,
  1204. IIO_EV_TYPE_THRESH,
  1205. IIO_EV_DIR_EITHER),
  1206. timestamp);
  1207. }
  1208. if (ret & TSL2772_STA_ALS_INTR) {
  1209. iio_push_event(indio_dev,
  1210. IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
  1211. 0,
  1212. IIO_EV_TYPE_THRESH,
  1213. IIO_EV_DIR_EITHER),
  1214. timestamp);
  1215. }
  1216. ret = i2c_smbus_write_byte(chip->client,
  1217. TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
  1218. TSL2772_CMD_PROXALS_INT_CLR);
  1219. if (ret < 0)
  1220. dev_err(&chip->client->dev,
  1221. "%s: failed to clear interrupt status: %d\n",
  1222. __func__, ret);
  1223. return IRQ_HANDLED;
  1224. }
  1225. static struct attribute *tsl2772_ALS_device_attrs[] = {
  1226. &dev_attr_in_illuminance0_target_input.attr,
  1227. &dev_attr_in_illuminance0_calibrate.attr,
  1228. &dev_attr_in_illuminance0_lux_table.attr,
  1229. NULL
  1230. };
  1231. static struct attribute *tsl2772_PRX_device_attrs[] = {
  1232. &dev_attr_in_proximity0_calibrate.attr,
  1233. NULL
  1234. };
  1235. static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
  1236. &dev_attr_in_illuminance0_target_input.attr,
  1237. &dev_attr_in_illuminance0_calibrate.attr,
  1238. &dev_attr_in_illuminance0_lux_table.attr,
  1239. NULL
  1240. };
  1241. static struct attribute *tsl2772_PRX2_device_attrs[] = {
  1242. &dev_attr_in_proximity0_calibrate.attr,
  1243. NULL
  1244. };
  1245. static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
  1246. &dev_attr_in_illuminance0_target_input.attr,
  1247. &dev_attr_in_illuminance0_calibrate.attr,
  1248. &dev_attr_in_illuminance0_lux_table.attr,
  1249. &dev_attr_in_proximity0_calibrate.attr,
  1250. NULL
  1251. };
  1252. static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
  1253. [ALS] = {
  1254. .attrs = tsl2772_ALS_device_attrs,
  1255. },
  1256. [PRX] = {
  1257. .attrs = tsl2772_PRX_device_attrs,
  1258. },
  1259. [ALSPRX] = {
  1260. .attrs = tsl2772_ALSPRX_device_attrs,
  1261. },
  1262. [PRX2] = {
  1263. .attrs = tsl2772_PRX2_device_attrs,
  1264. },
  1265. [ALSPRX2] = {
  1266. .attrs = tsl2772_ALSPRX2_device_attrs,
  1267. },
  1268. };
  1269. #define TSL2772_DEVICE_INFO(type)[type] = \
  1270. { \
  1271. .attrs = &tsl2772_device_attr_group_tbl[type], \
  1272. .read_raw = &tsl2772_read_raw, \
  1273. .read_avail = &tsl2772_read_avail, \
  1274. .write_raw = &tsl2772_write_raw, \
  1275. .read_event_value = &tsl2772_read_event_value, \
  1276. .write_event_value = &tsl2772_write_event_value, \
  1277. .read_event_config = &tsl2772_read_interrupt_config, \
  1278. .write_event_config = &tsl2772_write_interrupt_config, \
  1279. }
  1280. static const struct iio_info tsl2772_device_info[] = {
  1281. TSL2772_DEVICE_INFO(ALS),
  1282. TSL2772_DEVICE_INFO(PRX),
  1283. TSL2772_DEVICE_INFO(ALSPRX),
  1284. TSL2772_DEVICE_INFO(PRX2),
  1285. TSL2772_DEVICE_INFO(ALSPRX2),
  1286. };
  1287. static const struct iio_event_spec tsl2772_events[] = {
  1288. {
  1289. .type = IIO_EV_TYPE_THRESH,
  1290. .dir = IIO_EV_DIR_RISING,
  1291. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  1292. }, {
  1293. .type = IIO_EV_TYPE_THRESH,
  1294. .dir = IIO_EV_DIR_FALLING,
  1295. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  1296. }, {
  1297. .type = IIO_EV_TYPE_THRESH,
  1298. .dir = IIO_EV_DIR_EITHER,
  1299. .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
  1300. BIT(IIO_EV_INFO_ENABLE),
  1301. },
  1302. };
  1303. static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
  1304. [ALS] = {
  1305. .channel_with_events = {
  1306. {
  1307. .type = IIO_LIGHT,
  1308. .indexed = 1,
  1309. .channel = 0,
  1310. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1311. }, {
  1312. .type = IIO_INTENSITY,
  1313. .indexed = 1,
  1314. .channel = 0,
  1315. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1316. BIT(IIO_CHAN_INFO_INT_TIME) |
  1317. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1318. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1319. .info_mask_separate_available =
  1320. BIT(IIO_CHAN_INFO_INT_TIME) |
  1321. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1322. .event_spec = tsl2772_events,
  1323. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1324. }, {
  1325. .type = IIO_INTENSITY,
  1326. .indexed = 1,
  1327. .channel = 1,
  1328. },
  1329. },
  1330. .channel_without_events = {
  1331. {
  1332. .type = IIO_LIGHT,
  1333. .indexed = 1,
  1334. .channel = 0,
  1335. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1336. }, {
  1337. .type = IIO_INTENSITY,
  1338. .indexed = 1,
  1339. .channel = 0,
  1340. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1341. BIT(IIO_CHAN_INFO_INT_TIME) |
  1342. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1343. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1344. .info_mask_separate_available =
  1345. BIT(IIO_CHAN_INFO_INT_TIME) |
  1346. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1347. }, {
  1348. .type = IIO_INTENSITY,
  1349. .indexed = 1,
  1350. .channel = 1,
  1351. },
  1352. },
  1353. .chan_table_elements = 3,
  1354. .info = &tsl2772_device_info[ALS],
  1355. },
  1356. [PRX] = {
  1357. .channel_with_events = {
  1358. {
  1359. .type = IIO_PROXIMITY,
  1360. .indexed = 1,
  1361. .channel = 0,
  1362. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1363. .event_spec = tsl2772_events,
  1364. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1365. },
  1366. },
  1367. .channel_without_events = {
  1368. {
  1369. .type = IIO_PROXIMITY,
  1370. .indexed = 1,
  1371. .channel = 0,
  1372. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1373. },
  1374. },
  1375. .chan_table_elements = 1,
  1376. .info = &tsl2772_device_info[PRX],
  1377. },
  1378. [ALSPRX] = {
  1379. .channel_with_events = {
  1380. {
  1381. .type = IIO_LIGHT,
  1382. .indexed = 1,
  1383. .channel = 0,
  1384. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1385. }, {
  1386. .type = IIO_INTENSITY,
  1387. .indexed = 1,
  1388. .channel = 0,
  1389. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1390. BIT(IIO_CHAN_INFO_INT_TIME) |
  1391. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1392. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1393. .info_mask_separate_available =
  1394. BIT(IIO_CHAN_INFO_INT_TIME) |
  1395. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1396. .event_spec = tsl2772_events,
  1397. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1398. }, {
  1399. .type = IIO_INTENSITY,
  1400. .indexed = 1,
  1401. .channel = 1,
  1402. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1403. }, {
  1404. .type = IIO_PROXIMITY,
  1405. .indexed = 1,
  1406. .channel = 0,
  1407. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1408. .event_spec = tsl2772_events,
  1409. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1410. },
  1411. },
  1412. .channel_without_events = {
  1413. {
  1414. .type = IIO_LIGHT,
  1415. .indexed = 1,
  1416. .channel = 0,
  1417. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1418. }, {
  1419. .type = IIO_INTENSITY,
  1420. .indexed = 1,
  1421. .channel = 0,
  1422. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1423. BIT(IIO_CHAN_INFO_INT_TIME) |
  1424. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1425. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1426. .info_mask_separate_available =
  1427. BIT(IIO_CHAN_INFO_INT_TIME) |
  1428. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1429. }, {
  1430. .type = IIO_INTENSITY,
  1431. .indexed = 1,
  1432. .channel = 1,
  1433. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1434. }, {
  1435. .type = IIO_PROXIMITY,
  1436. .indexed = 1,
  1437. .channel = 0,
  1438. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1439. },
  1440. },
  1441. .chan_table_elements = 4,
  1442. .info = &tsl2772_device_info[ALSPRX],
  1443. },
  1444. [PRX2] = {
  1445. .channel_with_events = {
  1446. {
  1447. .type = IIO_PROXIMITY,
  1448. .indexed = 1,
  1449. .channel = 0,
  1450. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1451. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1452. .info_mask_separate_available =
  1453. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1454. .event_spec = tsl2772_events,
  1455. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1456. },
  1457. },
  1458. .channel_without_events = {
  1459. {
  1460. .type = IIO_PROXIMITY,
  1461. .indexed = 1,
  1462. .channel = 0,
  1463. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1464. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1465. .info_mask_separate_available =
  1466. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1467. },
  1468. },
  1469. .chan_table_elements = 1,
  1470. .info = &tsl2772_device_info[PRX2],
  1471. },
  1472. [ALSPRX2] = {
  1473. .channel_with_events = {
  1474. {
  1475. .type = IIO_LIGHT,
  1476. .indexed = 1,
  1477. .channel = 0,
  1478. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1479. }, {
  1480. .type = IIO_INTENSITY,
  1481. .indexed = 1,
  1482. .channel = 0,
  1483. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1484. BIT(IIO_CHAN_INFO_INT_TIME) |
  1485. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1486. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1487. .info_mask_separate_available =
  1488. BIT(IIO_CHAN_INFO_INT_TIME) |
  1489. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1490. .event_spec = tsl2772_events,
  1491. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1492. }, {
  1493. .type = IIO_INTENSITY,
  1494. .indexed = 1,
  1495. .channel = 1,
  1496. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1497. }, {
  1498. .type = IIO_PROXIMITY,
  1499. .indexed = 1,
  1500. .channel = 0,
  1501. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1502. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1503. .info_mask_separate_available =
  1504. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1505. .event_spec = tsl2772_events,
  1506. .num_event_specs = ARRAY_SIZE(tsl2772_events),
  1507. },
  1508. },
  1509. .channel_without_events = {
  1510. {
  1511. .type = IIO_LIGHT,
  1512. .indexed = 1,
  1513. .channel = 0,
  1514. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1515. }, {
  1516. .type = IIO_INTENSITY,
  1517. .indexed = 1,
  1518. .channel = 0,
  1519. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1520. BIT(IIO_CHAN_INFO_INT_TIME) |
  1521. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1522. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1523. .info_mask_separate_available =
  1524. BIT(IIO_CHAN_INFO_INT_TIME) |
  1525. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1526. }, {
  1527. .type = IIO_INTENSITY,
  1528. .indexed = 1,
  1529. .channel = 1,
  1530. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1531. }, {
  1532. .type = IIO_PROXIMITY,
  1533. .indexed = 1,
  1534. .channel = 0,
  1535. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1536. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1537. .info_mask_separate_available =
  1538. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1539. },
  1540. },
  1541. .chan_table_elements = 4,
  1542. .info = &tsl2772_device_info[ALSPRX2],
  1543. },
  1544. };
  1545. static int tsl2772_probe(struct i2c_client *clientp,
  1546. const struct i2c_device_id *id)
  1547. {
  1548. struct iio_dev *indio_dev;
  1549. struct tsl2772_chip *chip;
  1550. int ret;
  1551. indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
  1552. if (!indio_dev)
  1553. return -ENOMEM;
  1554. chip = iio_priv(indio_dev);
  1555. chip->client = clientp;
  1556. i2c_set_clientdata(clientp, indio_dev);
  1557. chip->supplies[TSL2772_SUPPLY_VDD].supply = "vdd";
  1558. chip->supplies[TSL2772_SUPPLY_VDDIO].supply = "vddio";
  1559. ret = devm_regulator_bulk_get(&clientp->dev,
  1560. ARRAY_SIZE(chip->supplies),
  1561. chip->supplies);
  1562. if (ret < 0)
  1563. return dev_err_probe(&clientp->dev, ret, "Failed to get regulators\n");
  1564. ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
  1565. if (ret < 0) {
  1566. dev_err(&clientp->dev, "Failed to enable regulators: %d\n",
  1567. ret);
  1568. return ret;
  1569. }
  1570. ret = devm_add_action_or_reset(&clientp->dev,
  1571. tsl2772_disable_regulators_action,
  1572. chip);
  1573. if (ret < 0) {
  1574. dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
  1575. ret);
  1576. return ret;
  1577. }
  1578. usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
  1579. ret = i2c_smbus_read_byte_data(chip->client,
  1580. TSL2772_CMD_REG | TSL2772_CHIPID);
  1581. if (ret < 0)
  1582. return ret;
  1583. if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
  1584. dev_info(&chip->client->dev,
  1585. "%s: i2c device found does not match expected id\n",
  1586. __func__);
  1587. return -EINVAL;
  1588. }
  1589. ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
  1590. if (ret < 0) {
  1591. dev_err(&clientp->dev,
  1592. "%s: Failed to write to CMD register: %d\n",
  1593. __func__, ret);
  1594. return ret;
  1595. }
  1596. mutex_init(&chip->als_mutex);
  1597. mutex_init(&chip->prox_mutex);
  1598. chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
  1599. chip->pdata = dev_get_platdata(&clientp->dev);
  1600. chip->id = id->driver_data;
  1601. chip->chip_info =
  1602. &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
  1603. indio_dev->info = chip->chip_info->info;
  1604. indio_dev->modes = INDIO_DIRECT_MODE;
  1605. indio_dev->name = chip->client->name;
  1606. indio_dev->num_channels = chip->chip_info->chan_table_elements;
  1607. if (clientp->irq) {
  1608. indio_dev->channels = chip->chip_info->channel_with_events;
  1609. ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
  1610. NULL,
  1611. &tsl2772_event_handler,
  1612. IRQF_TRIGGER_FALLING |
  1613. IRQF_ONESHOT,
  1614. "TSL2772_event",
  1615. indio_dev);
  1616. if (ret) {
  1617. dev_err(&clientp->dev,
  1618. "%s: irq request failed\n", __func__);
  1619. return ret;
  1620. }
  1621. } else {
  1622. indio_dev->channels = chip->chip_info->channel_without_events;
  1623. }
  1624. tsl2772_defaults(chip);
  1625. ret = tsl2772_chip_on(indio_dev);
  1626. if (ret < 0)
  1627. return ret;
  1628. ret = devm_add_action_or_reset(&clientp->dev,
  1629. tsl2772_chip_off_action,
  1630. indio_dev);
  1631. if (ret < 0)
  1632. return ret;
  1633. return devm_iio_device_register(&clientp->dev, indio_dev);
  1634. }
  1635. static int tsl2772_suspend(struct device *dev)
  1636. {
  1637. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1638. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1639. int ret;
  1640. ret = tsl2772_chip_off(indio_dev);
  1641. regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
  1642. return ret;
  1643. }
  1644. static int tsl2772_resume(struct device *dev)
  1645. {
  1646. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1647. struct tsl2772_chip *chip = iio_priv(indio_dev);
  1648. int ret;
  1649. ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
  1650. if (ret < 0)
  1651. return ret;
  1652. usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
  1653. return tsl2772_chip_on(indio_dev);
  1654. }
  1655. static const struct i2c_device_id tsl2772_idtable[] = {
  1656. { "tsl2571", tsl2571 },
  1657. { "tsl2671", tsl2671 },
  1658. { "tmd2671", tmd2671 },
  1659. { "tsl2771", tsl2771 },
  1660. { "tmd2771", tmd2771 },
  1661. { "tsl2572", tsl2572 },
  1662. { "tsl2672", tsl2672 },
  1663. { "tmd2672", tmd2672 },
  1664. { "tsl2772", tsl2772 },
  1665. { "tmd2772", tmd2772 },
  1666. { "apds9930", apds9930 },
  1667. {}
  1668. };
  1669. MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
  1670. static const struct of_device_id tsl2772_of_match[] = {
  1671. { .compatible = "amstaos,tsl2571" },
  1672. { .compatible = "amstaos,tsl2671" },
  1673. { .compatible = "amstaos,tmd2671" },
  1674. { .compatible = "amstaos,tsl2771" },
  1675. { .compatible = "amstaos,tmd2771" },
  1676. { .compatible = "amstaos,tsl2572" },
  1677. { .compatible = "amstaos,tsl2672" },
  1678. { .compatible = "amstaos,tmd2672" },
  1679. { .compatible = "amstaos,tsl2772" },
  1680. { .compatible = "amstaos,tmd2772" },
  1681. { .compatible = "avago,apds9930" },
  1682. {}
  1683. };
  1684. MODULE_DEVICE_TABLE(of, tsl2772_of_match);
  1685. static const struct dev_pm_ops tsl2772_pm_ops = {
  1686. .suspend = tsl2772_suspend,
  1687. .resume = tsl2772_resume,
  1688. };
  1689. static struct i2c_driver tsl2772_driver = {
  1690. .driver = {
  1691. .name = "tsl2772",
  1692. .of_match_table = tsl2772_of_match,
  1693. .pm = &tsl2772_pm_ops,
  1694. },
  1695. .id_table = tsl2772_idtable,
  1696. .probe = tsl2772_probe,
  1697. };
  1698. module_i2c_driver(tsl2772_driver);
  1699. MODULE_AUTHOR("J. August Brenner <[email protected]>");
  1700. MODULE_AUTHOR("Brian Masney <[email protected]>");
  1701. MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
  1702. MODULE_LICENSE("GPL");