tsl2772.c 44 KB

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