tsl2x7x_core.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  1. /*
  2. * Device driver for monitoring ambient light intensity in (lux)
  3. * and proximity detection (prox) within the TAOS TSL2X7X family of devices.
  4. *
  5. * Copyright (c) 2012, TAOS Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/i2c.h>
  23. #include <linux/errno.h>
  24. #include <linux/delay.h>
  25. #include <linux/mutex.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/iio/events.h>
  30. #include <linux/iio/iio.h>
  31. #include <linux/iio/sysfs.h>
  32. #include "tsl2x7x.h"
  33. /* Cal defs*/
  34. #define PROX_STAT_CAL 0
  35. #define PROX_STAT_SAMP 1
  36. #define MAX_SAMPLES_CAL 200
  37. /* TSL2X7X Device ID */
  38. #define TRITON_ID 0x00
  39. #define SWORDFISH_ID 0x30
  40. #define HALIBUT_ID 0x20
  41. /* Lux calculation constants */
  42. #define TSL2X7X_LUX_CALC_OVER_FLOW 65535
  43. /* TAOS Register definitions - note:
  44. * depending on device, some of these register are not used and the
  45. * register address is benign.
  46. */
  47. /* 2X7X register offsets */
  48. #define TSL2X7X_MAX_CONFIG_REG 16
  49. /* Device Registers and Masks */
  50. #define TSL2X7X_CNTRL 0x00
  51. #define TSL2X7X_ALS_TIME 0X01
  52. #define TSL2X7X_PRX_TIME 0x02
  53. #define TSL2X7X_WAIT_TIME 0x03
  54. #define TSL2X7X_ALS_MINTHRESHLO 0X04
  55. #define TSL2X7X_ALS_MINTHRESHHI 0X05
  56. #define TSL2X7X_ALS_MAXTHRESHLO 0X06
  57. #define TSL2X7X_ALS_MAXTHRESHHI 0X07
  58. #define TSL2X7X_PRX_MINTHRESHLO 0X08
  59. #define TSL2X7X_PRX_MINTHRESHHI 0X09
  60. #define TSL2X7X_PRX_MAXTHRESHLO 0X0A
  61. #define TSL2X7X_PRX_MAXTHRESHHI 0X0B
  62. #define TSL2X7X_PERSISTENCE 0x0C
  63. #define TSL2X7X_PRX_CONFIG 0x0D
  64. #define TSL2X7X_PRX_COUNT 0x0E
  65. #define TSL2X7X_GAIN 0x0F
  66. #define TSL2X7X_NOTUSED 0x10
  67. #define TSL2X7X_REVID 0x11
  68. #define TSL2X7X_CHIPID 0x12
  69. #define TSL2X7X_STATUS 0x13
  70. #define TSL2X7X_ALS_CHAN0LO 0x14
  71. #define TSL2X7X_ALS_CHAN0HI 0x15
  72. #define TSL2X7X_ALS_CHAN1LO 0x16
  73. #define TSL2X7X_ALS_CHAN1HI 0x17
  74. #define TSL2X7X_PRX_LO 0x18
  75. #define TSL2X7X_PRX_HI 0x19
  76. /* tsl2X7X cmd reg masks */
  77. #define TSL2X7X_CMD_REG 0x80
  78. #define TSL2X7X_CMD_SPL_FN 0x60
  79. #define TSL2X7X_CMD_PROX_INT_CLR 0X05
  80. #define TSL2X7X_CMD_ALS_INT_CLR 0x06
  81. #define TSL2X7X_CMD_PROXALS_INT_CLR 0X07
  82. /* tsl2X7X cntrl reg masks */
  83. #define TSL2X7X_CNTL_ADC_ENBL 0x02
  84. #define TSL2X7X_CNTL_PWR_ON 0x01
  85. /* tsl2X7X status reg masks */
  86. #define TSL2X7X_STA_ADC_VALID 0x01
  87. #define TSL2X7X_STA_PRX_VALID 0x02
  88. #define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\
  89. TSL2X7X_STA_PRX_VALID)
  90. #define TSL2X7X_STA_ALS_INTR 0x10
  91. #define TSL2X7X_STA_PRX_INTR 0x20
  92. /* tsl2X7X cntrl reg masks */
  93. #define TSL2X7X_CNTL_REG_CLEAR 0x00
  94. #define TSL2X7X_CNTL_PROX_INT_ENBL 0X20
  95. #define TSL2X7X_CNTL_ALS_INT_ENBL 0X10
  96. #define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08
  97. #define TSL2X7X_CNTL_PROX_DET_ENBL 0X04
  98. #define TSL2X7X_CNTL_PWRON 0x01
  99. #define TSL2X7X_CNTL_ALSPON_ENBL 0x03
  100. #define TSL2X7X_CNTL_INTALSPON_ENBL 0x13
  101. #define TSL2X7X_CNTL_PROXPON_ENBL 0x0F
  102. #define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F
  103. /*Prox diode to use */
  104. #define TSL2X7X_DIODE0 0x10
  105. #define TSL2X7X_DIODE1 0x20
  106. #define TSL2X7X_DIODE_BOTH 0x30
  107. /* LED Power */
  108. #define TSL2X7X_mA100 0x00
  109. #define TSL2X7X_mA50 0x40
  110. #define TSL2X7X_mA25 0x80
  111. #define TSL2X7X_mA13 0xD0
  112. #define TSL2X7X_MAX_TIMER_CNT (0xFF)
  113. #define TSL2X7X_MIN_ITIME 3
  114. /* TAOS txx2x7x Device family members */
  115. enum {
  116. tsl2571,
  117. tsl2671,
  118. tmd2671,
  119. tsl2771,
  120. tmd2771,
  121. tsl2572,
  122. tsl2672,
  123. tmd2672,
  124. tsl2772,
  125. tmd2772
  126. };
  127. enum {
  128. TSL2X7X_CHIP_UNKNOWN = 0,
  129. TSL2X7X_CHIP_WORKING = 1,
  130. TSL2X7X_CHIP_SUSPENDED = 2
  131. };
  132. struct tsl2x7x_parse_result {
  133. int integer;
  134. int fract;
  135. };
  136. /* Per-device data */
  137. struct tsl2x7x_als_info {
  138. u16 als_ch0;
  139. u16 als_ch1;
  140. u16 lux;
  141. };
  142. struct tsl2x7x_prox_stat {
  143. int min;
  144. int max;
  145. int mean;
  146. unsigned long stddev;
  147. };
  148. struct tsl2x7x_chip_info {
  149. int chan_table_elements;
  150. struct iio_chan_spec channel[4];
  151. const struct iio_info *info;
  152. };
  153. struct tsl2X7X_chip {
  154. kernel_ulong_t id;
  155. struct mutex prox_mutex;
  156. struct mutex als_mutex;
  157. struct i2c_client *client;
  158. u16 prox_data;
  159. struct tsl2x7x_als_info als_cur_info;
  160. struct tsl2x7x_settings tsl2x7x_settings;
  161. struct tsl2X7X_platform_data *pdata;
  162. int als_time_scale;
  163. int als_saturation;
  164. int tsl2x7x_chip_status;
  165. u8 tsl2x7x_config[TSL2X7X_MAX_CONFIG_REG];
  166. const struct tsl2x7x_chip_info *chip_info;
  167. const struct iio_info *info;
  168. s64 event_timestamp;
  169. /* This structure is intentionally large to accommodate
  170. * updates via sysfs. */
  171. /* Sized to 9 = max 8 segments + 1 termination segment */
  172. struct tsl2x7x_lux tsl2x7x_device_lux[TSL2X7X_MAX_LUX_TABLE_SIZE];
  173. };
  174. /* Different devices require different coefficents */
  175. static const struct tsl2x7x_lux tsl2x71_lux_table[] = {
  176. { 14461, 611, 1211 },
  177. { 18540, 352, 623 },
  178. { 0, 0, 0 },
  179. };
  180. static const struct tsl2x7x_lux tmd2x71_lux_table[] = {
  181. { 11635, 115, 256 },
  182. { 15536, 87, 179 },
  183. { 0, 0, 0 },
  184. };
  185. static const struct tsl2x7x_lux tsl2x72_lux_table[] = {
  186. { 14013, 466, 917 },
  187. { 18222, 310, 552 },
  188. { 0, 0, 0 },
  189. };
  190. static const struct tsl2x7x_lux tmd2x72_lux_table[] = {
  191. { 13218, 130, 262 },
  192. { 17592, 92, 169 },
  193. { 0, 0, 0 },
  194. };
  195. static const struct tsl2x7x_lux *tsl2x7x_default_lux_table_group[] = {
  196. [tsl2571] = tsl2x71_lux_table,
  197. [tsl2671] = tsl2x71_lux_table,
  198. [tmd2671] = tmd2x71_lux_table,
  199. [tsl2771] = tsl2x71_lux_table,
  200. [tmd2771] = tmd2x71_lux_table,
  201. [tsl2572] = tsl2x72_lux_table,
  202. [tsl2672] = tsl2x72_lux_table,
  203. [tmd2672] = tmd2x72_lux_table,
  204. [tsl2772] = tsl2x72_lux_table,
  205. [tmd2772] = tmd2x72_lux_table,
  206. };
  207. static const struct tsl2x7x_settings tsl2x7x_default_settings = {
  208. .als_time = 219, /* 101 ms */
  209. .als_gain = 0,
  210. .prx_time = 254, /* 5.4 ms */
  211. .prox_gain = 1,
  212. .wait_time = 245,
  213. .prox_config = 0,
  214. .als_gain_trim = 1000,
  215. .als_cal_target = 150,
  216. .als_thresh_low = 200,
  217. .als_thresh_high = 256,
  218. .persistence = 255,
  219. .interrupts_en = 0,
  220. .prox_thres_low = 0,
  221. .prox_thres_high = 512,
  222. .prox_max_samples_cal = 30,
  223. .prox_pulse_count = 8
  224. };
  225. static const s16 tsl2X7X_als_gainadj[] = {
  226. 1,
  227. 8,
  228. 16,
  229. 120
  230. };
  231. static const s16 tsl2X7X_prx_gainadj[] = {
  232. 1,
  233. 2,
  234. 4,
  235. 8
  236. };
  237. /* Channel variations */
  238. enum {
  239. ALS,
  240. PRX,
  241. ALSPRX,
  242. PRX2,
  243. ALSPRX2,
  244. };
  245. static const u8 device_channel_config[] = {
  246. ALS,
  247. PRX,
  248. PRX,
  249. ALSPRX,
  250. ALSPRX,
  251. ALS,
  252. PRX2,
  253. PRX2,
  254. ALSPRX2,
  255. ALSPRX2
  256. };
  257. /**
  258. * tsl2x7x_i2c_read() - Read a byte from a register.
  259. * @client: i2c client
  260. * @reg: device register to read from
  261. * @*val: pointer to location to store register contents.
  262. *
  263. */
  264. static int
  265. tsl2x7x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
  266. {
  267. int ret = 0;
  268. /* select register to write */
  269. ret = i2c_smbus_write_byte(client, (TSL2X7X_CMD_REG | reg));
  270. if (ret < 0) {
  271. dev_err(&client->dev, "failed to write register %x\n", reg);
  272. return ret;
  273. }
  274. /* read the data */
  275. ret = i2c_smbus_read_byte(client);
  276. if (ret >= 0)
  277. *val = (u8)ret;
  278. else
  279. dev_err(&client->dev, "failed to read register %x\n", reg);
  280. return ret;
  281. }
  282. /**
  283. * tsl2x7x_get_lux() - Reads and calculates current lux value.
  284. * @indio_dev: pointer to IIO device
  285. *
  286. * The raw ch0 and ch1 values of the ambient light sensed in the last
  287. * integration cycle are read from the device.
  288. * Time scale factor array values are adjusted based on the integration time.
  289. * The raw values are multiplied by a scale factor, and device gain is obtained
  290. * using gain index. Limit checks are done next, then the ratio of a multiple
  291. * of ch1 value, to the ch0 value, is calculated. Array tsl2x7x_device_lux[]
  292. * is then scanned to find the first ratio value that is just above the ratio
  293. * we just calculated. The ch0 and ch1 multiplier constants in the array are
  294. * then used along with the time scale factor array values, to calculate the
  295. * lux.
  296. */
  297. static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
  298. {
  299. u16 ch0, ch1; /* separated ch0/ch1 data from device */
  300. u32 lux; /* raw lux calculated from device data */
  301. u64 lux64;
  302. u32 ratio;
  303. u8 buf[4];
  304. struct tsl2x7x_lux *p;
  305. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  306. int i, ret;
  307. u32 ch0lux = 0;
  308. u32 ch1lux = 0;
  309. if (mutex_trylock(&chip->als_mutex) == 0)
  310. return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
  311. if (chip->tsl2x7x_chip_status != TSL2X7X_CHIP_WORKING) {
  312. /* device is not enabled */
  313. dev_err(&chip->client->dev, "%s: device is not enabled\n",
  314. __func__);
  315. ret = -EBUSY;
  316. goto out_unlock;
  317. }
  318. ret = tsl2x7x_i2c_read(chip->client,
  319. (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &buf[0]);
  320. if (ret < 0) {
  321. dev_err(&chip->client->dev,
  322. "%s: Failed to read STATUS Reg\n", __func__);
  323. goto out_unlock;
  324. }
  325. /* is data new & valid */
  326. if (!(buf[0] & TSL2X7X_STA_ADC_VALID)) {
  327. dev_err(&chip->client->dev,
  328. "%s: data not valid yet\n", __func__);
  329. ret = chip->als_cur_info.lux; /* return LAST VALUE */
  330. goto out_unlock;
  331. }
  332. for (i = 0; i < 4; i++) {
  333. ret = tsl2x7x_i2c_read(chip->client,
  334. (TSL2X7X_CMD_REG | (TSL2X7X_ALS_CHAN0LO + i)),
  335. &buf[i]);
  336. if (ret < 0) {
  337. dev_err(&chip->client->dev,
  338. "failed to read. err=%x\n", ret);
  339. goto out_unlock;
  340. }
  341. }
  342. /* clear any existing interrupt status */
  343. ret = i2c_smbus_write_byte(chip->client,
  344. (TSL2X7X_CMD_REG |
  345. TSL2X7X_CMD_SPL_FN |
  346. TSL2X7X_CMD_ALS_INT_CLR));
  347. if (ret < 0) {
  348. dev_err(&chip->client->dev,
  349. "i2c_write_command failed - err = %d\n", ret);
  350. goto out_unlock; /* have no data, so return failure */
  351. }
  352. /* extract ALS/lux data */
  353. ch0 = le16_to_cpup((const __le16 *)&buf[0]);
  354. ch1 = le16_to_cpup((const __le16 *)&buf[2]);
  355. chip->als_cur_info.als_ch0 = ch0;
  356. chip->als_cur_info.als_ch1 = ch1;
  357. if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) {
  358. lux = TSL2X7X_LUX_CALC_OVER_FLOW;
  359. goto return_max;
  360. }
  361. if (!ch0) {
  362. /* have no data, so return LAST VALUE */
  363. ret = chip->als_cur_info.lux;
  364. goto out_unlock;
  365. }
  366. /* calculate ratio */
  367. ratio = (ch1 << 15) / ch0;
  368. /* convert to unscaled lux using the pointer to the table */
  369. p = (struct tsl2x7x_lux *) chip->tsl2x7x_device_lux;
  370. while (p->ratio != 0 && p->ratio < ratio)
  371. p++;
  372. if (p->ratio == 0) {
  373. lux = 0;
  374. } else {
  375. ch0lux = DIV_ROUND_UP(ch0 * p->ch0,
  376. tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
  377. ch1lux = DIV_ROUND_UP(ch1 * p->ch1,
  378. tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
  379. lux = ch0lux - ch1lux;
  380. }
  381. /* note: lux is 31 bit max at this point */
  382. if (ch1lux > ch0lux) {
  383. dev_dbg(&chip->client->dev, "ch1lux > ch0lux-return last value\n");
  384. ret = chip->als_cur_info.lux;
  385. goto out_unlock;
  386. }
  387. /* adjust for active time scale */
  388. if (chip->als_time_scale == 0)
  389. lux = 0;
  390. else
  391. lux = (lux + (chip->als_time_scale >> 1)) /
  392. chip->als_time_scale;
  393. /* adjust for active gain scale
  394. * The tsl2x7x_device_lux tables have a factor of 256 built-in.
  395. * User-specified gain provides a multiplier.
  396. * Apply user-specified gain before shifting right to retain precision.
  397. * Use 64 bits to avoid overflow on multiplication.
  398. * Then go back to 32 bits before division to avoid using div_u64().
  399. */
  400. lux64 = lux;
  401. lux64 = lux64 * chip->tsl2x7x_settings.als_gain_trim;
  402. lux64 >>= 8;
  403. lux = lux64;
  404. lux = (lux + 500) / 1000;
  405. if (lux > TSL2X7X_LUX_CALC_OVER_FLOW) /* check for overflow */
  406. lux = TSL2X7X_LUX_CALC_OVER_FLOW;
  407. /* Update the structure with the latest lux. */
  408. return_max:
  409. chip->als_cur_info.lux = lux;
  410. ret = lux;
  411. out_unlock:
  412. mutex_unlock(&chip->als_mutex);
  413. return ret;
  414. }
  415. /**
  416. * tsl2x7x_get_prox() - Reads proximity data registers and updates
  417. * chip->prox_data.
  418. *
  419. * @indio_dev: pointer to IIO device
  420. */
  421. static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
  422. {
  423. int i;
  424. int ret;
  425. u8 status;
  426. u8 chdata[2];
  427. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  428. if (mutex_trylock(&chip->prox_mutex) == 0) {
  429. dev_err(&chip->client->dev,
  430. "%s: Can't get prox mutex\n", __func__);
  431. return -EBUSY;
  432. }
  433. ret = tsl2x7x_i2c_read(chip->client,
  434. (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &status);
  435. if (ret < 0) {
  436. dev_err(&chip->client->dev, "i2c err=%d\n", ret);
  437. goto prox_poll_err;
  438. }
  439. switch (chip->id) {
  440. case tsl2571:
  441. case tsl2671:
  442. case tmd2671:
  443. case tsl2771:
  444. case tmd2771:
  445. if (!(status & TSL2X7X_STA_ADC_VALID))
  446. goto prox_poll_err;
  447. break;
  448. case tsl2572:
  449. case tsl2672:
  450. case tmd2672:
  451. case tsl2772:
  452. case tmd2772:
  453. if (!(status & TSL2X7X_STA_PRX_VALID))
  454. goto prox_poll_err;
  455. break;
  456. }
  457. for (i = 0; i < 2; i++) {
  458. ret = tsl2x7x_i2c_read(chip->client,
  459. (TSL2X7X_CMD_REG |
  460. (TSL2X7X_PRX_LO + i)), &chdata[i]);
  461. if (ret < 0)
  462. goto prox_poll_err;
  463. }
  464. chip->prox_data =
  465. le16_to_cpup((const __le16 *)&chdata[0]);
  466. prox_poll_err:
  467. mutex_unlock(&chip->prox_mutex);
  468. return chip->prox_data;
  469. }
  470. /**
  471. * tsl2x7x_defaults() - Populates the device nominal operating parameters
  472. * with those provided by a 'platform' data struct or
  473. * with prefined defaults.
  474. *
  475. * @chip: pointer to device structure.
  476. */
  477. static void tsl2x7x_defaults(struct tsl2X7X_chip *chip)
  478. {
  479. /* If Operational settings defined elsewhere.. */
  480. if (chip->pdata && chip->pdata->platform_default_settings)
  481. memcpy(&(chip->tsl2x7x_settings),
  482. chip->pdata->platform_default_settings,
  483. sizeof(tsl2x7x_default_settings));
  484. else
  485. memcpy(&(chip->tsl2x7x_settings),
  486. &tsl2x7x_default_settings,
  487. sizeof(tsl2x7x_default_settings));
  488. /* Load up the proper lux table. */
  489. if (chip->pdata && chip->pdata->platform_lux_table[0].ratio != 0)
  490. memcpy(chip->tsl2x7x_device_lux,
  491. chip->pdata->platform_lux_table,
  492. sizeof(chip->pdata->platform_lux_table));
  493. else
  494. memcpy(chip->tsl2x7x_device_lux,
  495. (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id],
  496. MAX_DEFAULT_TABLE_BYTES);
  497. }
  498. /**
  499. * tsl2x7x_als_calibrate() - Obtain single reading and calculate
  500. * the als_gain_trim.
  501. *
  502. * @indio_dev: pointer to IIO device
  503. */
  504. static int tsl2x7x_als_calibrate(struct iio_dev *indio_dev)
  505. {
  506. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  507. u8 reg_val;
  508. int gain_trim_val;
  509. int ret;
  510. int lux_val;
  511. ret = i2c_smbus_write_byte(chip->client,
  512. (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
  513. if (ret < 0) {
  514. dev_err(&chip->client->dev,
  515. "failed to write CNTRL register, ret=%d\n", ret);
  516. return ret;
  517. }
  518. reg_val = i2c_smbus_read_byte(chip->client);
  519. if ((reg_val & (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON))
  520. != (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON)) {
  521. dev_err(&chip->client->dev,
  522. "%s: failed: ADC not enabled\n", __func__);
  523. return -1;
  524. }
  525. ret = i2c_smbus_write_byte(chip->client,
  526. (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
  527. if (ret < 0) {
  528. dev_err(&chip->client->dev,
  529. "failed to write ctrl reg: ret=%d\n", ret);
  530. return ret;
  531. }
  532. reg_val = i2c_smbus_read_byte(chip->client);
  533. if ((reg_val & TSL2X7X_STA_ADC_VALID) != TSL2X7X_STA_ADC_VALID) {
  534. dev_err(&chip->client->dev,
  535. "%s: failed: STATUS - ADC not valid.\n", __func__);
  536. return -ENODATA;
  537. }
  538. lux_val = tsl2x7x_get_lux(indio_dev);
  539. if (lux_val < 0) {
  540. dev_err(&chip->client->dev,
  541. "%s: failed to get lux\n", __func__);
  542. return lux_val;
  543. }
  544. gain_trim_val = ((chip->tsl2x7x_settings.als_cal_target)
  545. * chip->tsl2x7x_settings.als_gain_trim) / lux_val;
  546. if ((gain_trim_val < 250) || (gain_trim_val > 4000))
  547. return -ERANGE;
  548. chip->tsl2x7x_settings.als_gain_trim = gain_trim_val;
  549. dev_info(&chip->client->dev,
  550. "%s als_calibrate completed\n", chip->client->name);
  551. return (int) gain_trim_val;
  552. }
  553. static int tsl2x7x_chip_on(struct iio_dev *indio_dev)
  554. {
  555. int i;
  556. int ret = 0;
  557. u8 *dev_reg;
  558. u8 utmp;
  559. int als_count;
  560. int als_time;
  561. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  562. u8 reg_val = 0;
  563. if (chip->pdata && chip->pdata->power_on)
  564. chip->pdata->power_on(indio_dev);
  565. /* Non calculated parameters */
  566. chip->tsl2x7x_config[TSL2X7X_PRX_TIME] =
  567. chip->tsl2x7x_settings.prx_time;
  568. chip->tsl2x7x_config[TSL2X7X_WAIT_TIME] =
  569. chip->tsl2x7x_settings.wait_time;
  570. chip->tsl2x7x_config[TSL2X7X_PRX_CONFIG] =
  571. chip->tsl2x7x_settings.prox_config;
  572. chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHLO] =
  573. (chip->tsl2x7x_settings.als_thresh_low) & 0xFF;
  574. chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHHI] =
  575. (chip->tsl2x7x_settings.als_thresh_low >> 8) & 0xFF;
  576. chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHLO] =
  577. (chip->tsl2x7x_settings.als_thresh_high) & 0xFF;
  578. chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHHI] =
  579. (chip->tsl2x7x_settings.als_thresh_high >> 8) & 0xFF;
  580. chip->tsl2x7x_config[TSL2X7X_PERSISTENCE] =
  581. chip->tsl2x7x_settings.persistence;
  582. chip->tsl2x7x_config[TSL2X7X_PRX_COUNT] =
  583. chip->tsl2x7x_settings.prox_pulse_count;
  584. chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHLO] =
  585. (chip->tsl2x7x_settings.prox_thres_low) & 0xFF;
  586. chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHHI] =
  587. (chip->tsl2x7x_settings.prox_thres_low >> 8) & 0xFF;
  588. chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHLO] =
  589. (chip->tsl2x7x_settings.prox_thres_high) & 0xFF;
  590. chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHHI] =
  591. (chip->tsl2x7x_settings.prox_thres_high >> 8) & 0xFF;
  592. /* and make sure we're not already on */
  593. if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
  594. /* if forcing a register update - turn off, then on */
  595. dev_info(&chip->client->dev, "device is already enabled\n");
  596. return -EINVAL;
  597. }
  598. /* determine als integration register */
  599. als_count = (chip->tsl2x7x_settings.als_time * 100 + 135) / 270;
  600. if (!als_count)
  601. als_count = 1; /* ensure at least one cycle */
  602. /* convert back to time (encompasses overrides) */
  603. als_time = (als_count * 27 + 5) / 10;
  604. chip->tsl2x7x_config[TSL2X7X_ALS_TIME] = 256 - als_count;
  605. /* Set the gain based on tsl2x7x_settings struct */
  606. chip->tsl2x7x_config[TSL2X7X_GAIN] =
  607. (chip->tsl2x7x_settings.als_gain |
  608. (TSL2X7X_mA100 | TSL2X7X_DIODE1)
  609. | ((chip->tsl2x7x_settings.prox_gain) << 2));
  610. /* set chip struct re scaling and saturation */
  611. chip->als_saturation = als_count * 922; /* 90% of full scale */
  612. chip->als_time_scale = (als_time + 25) / 50;
  613. /* TSL2X7X Specific power-on / adc enable sequence
  614. * Power on the device 1st. */
  615. utmp = TSL2X7X_CNTL_PWR_ON;
  616. ret = i2c_smbus_write_byte_data(chip->client,
  617. TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
  618. if (ret < 0) {
  619. dev_err(&chip->client->dev,
  620. "%s: failed on CNTRL reg.\n", __func__);
  621. return ret;
  622. }
  623. /* Use the following shadow copy for our delay before enabling ADC.
  624. * Write all the registers. */
  625. for (i = 0, dev_reg = chip->tsl2x7x_config;
  626. i < TSL2X7X_MAX_CONFIG_REG; i++) {
  627. ret = i2c_smbus_write_byte_data(chip->client,
  628. TSL2X7X_CMD_REG + i, *dev_reg++);
  629. if (ret < 0) {
  630. dev_err(&chip->client->dev,
  631. "failed on write to reg %d.\n", i);
  632. return ret;
  633. }
  634. }
  635. mdelay(3); /* Power-on settling time */
  636. /* NOW enable the ADC
  637. * initialize the desired mode of operation */
  638. utmp = TSL2X7X_CNTL_PWR_ON |
  639. TSL2X7X_CNTL_ADC_ENBL |
  640. TSL2X7X_CNTL_PROX_DET_ENBL;
  641. ret = i2c_smbus_write_byte_data(chip->client,
  642. TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
  643. if (ret < 0) {
  644. dev_err(&chip->client->dev,
  645. "%s: failed on 2nd CTRL reg.\n", __func__);
  646. return ret;
  647. }
  648. chip->tsl2x7x_chip_status = TSL2X7X_CHIP_WORKING;
  649. if (chip->tsl2x7x_settings.interrupts_en != 0) {
  650. dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n");
  651. reg_val = TSL2X7X_CNTL_PWR_ON | TSL2X7X_CNTL_ADC_ENBL;
  652. if ((chip->tsl2x7x_settings.interrupts_en == 0x20) ||
  653. (chip->tsl2x7x_settings.interrupts_en == 0x30))
  654. reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL;
  655. reg_val |= chip->tsl2x7x_settings.interrupts_en;
  656. ret = i2c_smbus_write_byte_data(chip->client,
  657. (TSL2X7X_CMD_REG | TSL2X7X_CNTRL), reg_val);
  658. if (ret < 0)
  659. dev_err(&chip->client->dev,
  660. "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
  661. __func__);
  662. /* Clear out any initial interrupts */
  663. ret = i2c_smbus_write_byte(chip->client,
  664. TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
  665. TSL2X7X_CMD_PROXALS_INT_CLR);
  666. if (ret < 0) {
  667. dev_err(&chip->client->dev,
  668. "%s: Failed to clear Int status\n",
  669. __func__);
  670. return ret;
  671. }
  672. }
  673. return ret;
  674. }
  675. static int tsl2x7x_chip_off(struct iio_dev *indio_dev)
  676. {
  677. int ret;
  678. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  679. /* turn device off */
  680. chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
  681. ret = i2c_smbus_write_byte_data(chip->client,
  682. TSL2X7X_CMD_REG | TSL2X7X_CNTRL, 0x00);
  683. if (chip->pdata && chip->pdata->power_off)
  684. chip->pdata->power_off(chip->client);
  685. return ret;
  686. }
  687. /**
  688. * tsl2x7x_invoke_change
  689. * @indio_dev: pointer to IIO device
  690. *
  691. * Obtain and lock both ALS and PROX resources,
  692. * determine and save device state (On/Off),
  693. * cycle device to implement updated parameter,
  694. * put device back into proper state, and unlock
  695. * resource.
  696. */
  697. static
  698. int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
  699. {
  700. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  701. int device_status = chip->tsl2x7x_chip_status;
  702. mutex_lock(&chip->als_mutex);
  703. mutex_lock(&chip->prox_mutex);
  704. if (device_status == TSL2X7X_CHIP_WORKING)
  705. tsl2x7x_chip_off(indio_dev);
  706. tsl2x7x_chip_on(indio_dev);
  707. if (device_status != TSL2X7X_CHIP_WORKING)
  708. tsl2x7x_chip_off(indio_dev);
  709. mutex_unlock(&chip->prox_mutex);
  710. mutex_unlock(&chip->als_mutex);
  711. return 0;
  712. }
  713. static
  714. void tsl2x7x_prox_calculate(int *data, int length,
  715. struct tsl2x7x_prox_stat *statP)
  716. {
  717. int i;
  718. int sample_sum;
  719. int tmp;
  720. if (!length)
  721. length = 1;
  722. sample_sum = 0;
  723. statP->min = INT_MAX;
  724. statP->max = INT_MIN;
  725. for (i = 0; i < length; i++) {
  726. sample_sum += data[i];
  727. statP->min = min(statP->min, data[i]);
  728. statP->max = max(statP->max, data[i]);
  729. }
  730. statP->mean = sample_sum / length;
  731. sample_sum = 0;
  732. for (i = 0; i < length; i++) {
  733. tmp = data[i] - statP->mean;
  734. sample_sum += tmp * tmp;
  735. }
  736. statP->stddev = int_sqrt((long)sample_sum)/length;
  737. }
  738. /**
  739. * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
  740. * @indio_dev: pointer to IIO device
  741. *
  742. * Calculates a standard deviation based on the samples,
  743. * and sets the threshold accordingly.
  744. */
  745. static void tsl2x7x_prox_cal(struct iio_dev *indio_dev)
  746. {
  747. int prox_history[MAX_SAMPLES_CAL + 1];
  748. int i;
  749. struct tsl2x7x_prox_stat prox_stat_data[2];
  750. struct tsl2x7x_prox_stat *calP;
  751. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  752. u8 tmp_irq_settings;
  753. u8 current_state = chip->tsl2x7x_chip_status;
  754. if (chip->tsl2x7x_settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
  755. dev_err(&chip->client->dev,
  756. "max prox samples cal is too big: %d\n",
  757. chip->tsl2x7x_settings.prox_max_samples_cal);
  758. chip->tsl2x7x_settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
  759. }
  760. /* have to stop to change settings */
  761. tsl2x7x_chip_off(indio_dev);
  762. /* Enable proximity detection save just in case prox not wanted yet*/
  763. tmp_irq_settings = chip->tsl2x7x_settings.interrupts_en;
  764. chip->tsl2x7x_settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
  765. /*turn on device if not already on*/
  766. tsl2x7x_chip_on(indio_dev);
  767. /*gather the samples*/
  768. for (i = 0; i < chip->tsl2x7x_settings.prox_max_samples_cal; i++) {
  769. mdelay(15);
  770. tsl2x7x_get_prox(indio_dev);
  771. prox_history[i] = chip->prox_data;
  772. dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
  773. i, chip->prox_data);
  774. }
  775. tsl2x7x_chip_off(indio_dev);
  776. calP = &prox_stat_data[PROX_STAT_CAL];
  777. tsl2x7x_prox_calculate(prox_history,
  778. chip->tsl2x7x_settings.prox_max_samples_cal, calP);
  779. chip->tsl2x7x_settings.prox_thres_high = (calP->max << 1) - calP->mean;
  780. dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
  781. calP->min, calP->mean, calP->max);
  782. dev_info(&chip->client->dev,
  783. "%s proximity threshold set to %d\n",
  784. chip->client->name, chip->tsl2x7x_settings.prox_thres_high);
  785. /* back to the way they were */
  786. chip->tsl2x7x_settings.interrupts_en = tmp_irq_settings;
  787. if (current_state == TSL2X7X_CHIP_WORKING)
  788. tsl2x7x_chip_on(indio_dev);
  789. }
  790. static ssize_t tsl2x7x_power_state_show(struct device *dev,
  791. struct device_attribute *attr, char *buf)
  792. {
  793. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  794. return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
  795. }
  796. static ssize_t tsl2x7x_power_state_store(struct device *dev,
  797. struct device_attribute *attr, const char *buf, size_t len)
  798. {
  799. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  800. bool value;
  801. if (strtobool(buf, &value))
  802. return -EINVAL;
  803. if (value)
  804. tsl2x7x_chip_on(indio_dev);
  805. else
  806. tsl2x7x_chip_off(indio_dev);
  807. return len;
  808. }
  809. static ssize_t tsl2x7x_gain_available_show(struct device *dev,
  810. struct device_attribute *attr, char *buf)
  811. {
  812. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  813. switch (chip->id) {
  814. case tsl2571:
  815. case tsl2671:
  816. case tmd2671:
  817. case tsl2771:
  818. case tmd2771:
  819. return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
  820. }
  821. return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
  822. }
  823. static ssize_t tsl2x7x_prox_gain_available_show(struct device *dev,
  824. struct device_attribute *attr, char *buf)
  825. {
  826. return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
  827. }
  828. static ssize_t tsl2x7x_als_time_show(struct device *dev,
  829. struct device_attribute *attr, char *buf)
  830. {
  831. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  832. int y, z;
  833. y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
  834. z = y * TSL2X7X_MIN_ITIME;
  835. y /= 1000;
  836. z %= 1000;
  837. return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
  838. }
  839. static ssize_t tsl2x7x_als_time_store(struct device *dev,
  840. struct device_attribute *attr, const char *buf, size_t len)
  841. {
  842. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  843. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  844. struct tsl2x7x_parse_result result;
  845. int ret;
  846. ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
  847. if (ret)
  848. return ret;
  849. result.fract /= 3;
  850. chip->tsl2x7x_settings.als_time =
  851. (TSL2X7X_MAX_TIMER_CNT - (u8)result.fract);
  852. dev_info(&chip->client->dev, "%s: als time = %d",
  853. __func__, chip->tsl2x7x_settings.als_time);
  854. tsl2x7x_invoke_change(indio_dev);
  855. return IIO_VAL_INT_PLUS_MICRO;
  856. }
  857. static IIO_CONST_ATTR(in_illuminance0_integration_time_available,
  858. ".00272 - .696");
  859. static ssize_t tsl2x7x_als_cal_target_show(struct device *dev,
  860. struct device_attribute *attr, char *buf)
  861. {
  862. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  863. return snprintf(buf, PAGE_SIZE, "%d\n",
  864. chip->tsl2x7x_settings.als_cal_target);
  865. }
  866. static ssize_t tsl2x7x_als_cal_target_store(struct device *dev,
  867. struct device_attribute *attr, const char *buf, size_t len)
  868. {
  869. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  870. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  871. unsigned long value;
  872. if (kstrtoul(buf, 0, &value))
  873. return -EINVAL;
  874. if (value)
  875. chip->tsl2x7x_settings.als_cal_target = value;
  876. tsl2x7x_invoke_change(indio_dev);
  877. return len;
  878. }
  879. /* persistence settings */
  880. static ssize_t tsl2x7x_als_persistence_show(struct device *dev,
  881. struct device_attribute *attr, char *buf)
  882. {
  883. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  884. int y, z, filter_delay;
  885. /* Determine integration time */
  886. y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
  887. z = y * TSL2X7X_MIN_ITIME;
  888. filter_delay = z * (chip->tsl2x7x_settings.persistence & 0x0F);
  889. y = filter_delay / 1000;
  890. z = filter_delay % 1000;
  891. return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
  892. }
  893. static ssize_t tsl2x7x_als_persistence_store(struct device *dev,
  894. struct device_attribute *attr, const char *buf, size_t len)
  895. {
  896. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  897. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  898. struct tsl2x7x_parse_result result;
  899. int y, z, filter_delay;
  900. int ret;
  901. ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
  902. if (ret)
  903. return ret;
  904. y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
  905. z = y * TSL2X7X_MIN_ITIME;
  906. filter_delay =
  907. DIV_ROUND_UP((result.integer * 1000) + result.fract, z);
  908. chip->tsl2x7x_settings.persistence &= 0xF0;
  909. chip->tsl2x7x_settings.persistence |= (filter_delay & 0x0F);
  910. dev_info(&chip->client->dev, "%s: als persistence = %d",
  911. __func__, filter_delay);
  912. tsl2x7x_invoke_change(indio_dev);
  913. return IIO_VAL_INT_PLUS_MICRO;
  914. }
  915. static ssize_t tsl2x7x_prox_persistence_show(struct device *dev,
  916. struct device_attribute *attr, char *buf)
  917. {
  918. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  919. int y, z, filter_delay;
  920. /* Determine integration time */
  921. y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
  922. z = y * TSL2X7X_MIN_ITIME;
  923. filter_delay = z * ((chip->tsl2x7x_settings.persistence & 0xF0) >> 4);
  924. y = filter_delay / 1000;
  925. z = filter_delay % 1000;
  926. return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
  927. }
  928. static ssize_t tsl2x7x_prox_persistence_store(struct device *dev,
  929. struct device_attribute *attr, const char *buf, size_t len)
  930. {
  931. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  932. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  933. struct tsl2x7x_parse_result result;
  934. int y, z, filter_delay;
  935. int ret;
  936. ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
  937. if (ret)
  938. return ret;
  939. y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
  940. z = y * TSL2X7X_MIN_ITIME;
  941. filter_delay =
  942. DIV_ROUND_UP((result.integer * 1000) + result.fract, z);
  943. chip->tsl2x7x_settings.persistence &= 0x0F;
  944. chip->tsl2x7x_settings.persistence |= ((filter_delay << 4) & 0xF0);
  945. dev_info(&chip->client->dev, "%s: prox persistence = %d",
  946. __func__, filter_delay);
  947. tsl2x7x_invoke_change(indio_dev);
  948. return IIO_VAL_INT_PLUS_MICRO;
  949. }
  950. static ssize_t tsl2x7x_do_calibrate(struct device *dev,
  951. struct device_attribute *attr, const char *buf, size_t len)
  952. {
  953. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  954. bool value;
  955. if (strtobool(buf, &value))
  956. return -EINVAL;
  957. if (value)
  958. tsl2x7x_als_calibrate(indio_dev);
  959. tsl2x7x_invoke_change(indio_dev);
  960. return len;
  961. }
  962. static ssize_t tsl2x7x_luxtable_show(struct device *dev,
  963. struct device_attribute *attr, char *buf)
  964. {
  965. struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
  966. int i = 0;
  967. int offset = 0;
  968. while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
  969. offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,%u,",
  970. chip->tsl2x7x_device_lux[i].ratio,
  971. chip->tsl2x7x_device_lux[i].ch0,
  972. chip->tsl2x7x_device_lux[i].ch1);
  973. if (chip->tsl2x7x_device_lux[i].ratio == 0) {
  974. /* We just printed the first "0" entry.
  975. * Now get rid of the extra "," and break. */
  976. offset--;
  977. break;
  978. }
  979. i++;
  980. }
  981. offset += snprintf(buf + offset, PAGE_SIZE, "\n");
  982. return offset;
  983. }
  984. static ssize_t tsl2x7x_luxtable_store(struct device *dev,
  985. struct device_attribute *attr, const char *buf, size_t len)
  986. {
  987. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  988. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  989. int value[ARRAY_SIZE(chip->tsl2x7x_device_lux)*3 + 1];
  990. int n;
  991. get_options(buf, ARRAY_SIZE(value), value);
  992. /* We now have an array of ints starting at value[1], and
  993. * enumerated by value[0].
  994. * We expect each group of three ints is one table entry,
  995. * and the last table entry is all 0.
  996. */
  997. n = value[0];
  998. if ((n % 3) || n < 6 ||
  999. n > ((ARRAY_SIZE(chip->tsl2x7x_device_lux) - 1) * 3)) {
  1000. dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
  1001. return -EINVAL;
  1002. }
  1003. if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
  1004. dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
  1005. return -EINVAL;
  1006. }
  1007. if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING)
  1008. tsl2x7x_chip_off(indio_dev);
  1009. /* Zero out the table */
  1010. memset(chip->tsl2x7x_device_lux, 0, sizeof(chip->tsl2x7x_device_lux));
  1011. memcpy(chip->tsl2x7x_device_lux, &value[1], (value[0] * 4));
  1012. tsl2x7x_invoke_change(indio_dev);
  1013. return len;
  1014. }
  1015. static ssize_t tsl2x7x_do_prox_calibrate(struct device *dev,
  1016. struct device_attribute *attr, const char *buf, size_t len)
  1017. {
  1018. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1019. bool value;
  1020. if (strtobool(buf, &value))
  1021. return -EINVAL;
  1022. if (value)
  1023. tsl2x7x_prox_cal(indio_dev);
  1024. tsl2x7x_invoke_change(indio_dev);
  1025. return len;
  1026. }
  1027. static int tsl2x7x_read_interrupt_config(struct iio_dev *indio_dev,
  1028. const struct iio_chan_spec *chan,
  1029. enum iio_event_type type,
  1030. enum iio_event_direction dir)
  1031. {
  1032. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1033. int ret;
  1034. if (chan->type == IIO_INTENSITY)
  1035. ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x10);
  1036. else
  1037. ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x20);
  1038. return ret;
  1039. }
  1040. static int tsl2x7x_write_interrupt_config(struct iio_dev *indio_dev,
  1041. const struct iio_chan_spec *chan,
  1042. enum iio_event_type type,
  1043. enum iio_event_direction dir,
  1044. int val)
  1045. {
  1046. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1047. if (chan->type == IIO_INTENSITY) {
  1048. if (val)
  1049. chip->tsl2x7x_settings.interrupts_en |= 0x10;
  1050. else
  1051. chip->tsl2x7x_settings.interrupts_en &= 0x20;
  1052. } else {
  1053. if (val)
  1054. chip->tsl2x7x_settings.interrupts_en |= 0x20;
  1055. else
  1056. chip->tsl2x7x_settings.interrupts_en &= 0x10;
  1057. }
  1058. tsl2x7x_invoke_change(indio_dev);
  1059. return 0;
  1060. }
  1061. static int tsl2x7x_write_thresh(struct iio_dev *indio_dev,
  1062. const struct iio_chan_spec *chan,
  1063. enum iio_event_type type,
  1064. enum iio_event_direction dir,
  1065. enum iio_event_info info,
  1066. int val, int val2)
  1067. {
  1068. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1069. if (chan->type == IIO_INTENSITY) {
  1070. switch (dir) {
  1071. case IIO_EV_DIR_RISING:
  1072. chip->tsl2x7x_settings.als_thresh_high = val;
  1073. break;
  1074. case IIO_EV_DIR_FALLING:
  1075. chip->tsl2x7x_settings.als_thresh_low = val;
  1076. break;
  1077. default:
  1078. return -EINVAL;
  1079. }
  1080. } else {
  1081. switch (dir) {
  1082. case IIO_EV_DIR_RISING:
  1083. chip->tsl2x7x_settings.prox_thres_high = val;
  1084. break;
  1085. case IIO_EV_DIR_FALLING:
  1086. chip->tsl2x7x_settings.prox_thres_low = val;
  1087. break;
  1088. default:
  1089. return -EINVAL;
  1090. }
  1091. }
  1092. tsl2x7x_invoke_change(indio_dev);
  1093. return 0;
  1094. }
  1095. static int tsl2x7x_read_thresh(struct iio_dev *indio_dev,
  1096. const struct iio_chan_spec *chan,
  1097. enum iio_event_type type,
  1098. enum iio_event_direction dir,
  1099. enum iio_event_info info,
  1100. int *val, int *val2)
  1101. {
  1102. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1103. if (chan->type == IIO_INTENSITY) {
  1104. switch (dir) {
  1105. case IIO_EV_DIR_RISING:
  1106. *val = chip->tsl2x7x_settings.als_thresh_high;
  1107. break;
  1108. case IIO_EV_DIR_FALLING:
  1109. *val = chip->tsl2x7x_settings.als_thresh_low;
  1110. break;
  1111. default:
  1112. return -EINVAL;
  1113. }
  1114. } else {
  1115. switch (dir) {
  1116. case IIO_EV_DIR_RISING:
  1117. *val = chip->tsl2x7x_settings.prox_thres_high;
  1118. break;
  1119. case IIO_EV_DIR_FALLING:
  1120. *val = chip->tsl2x7x_settings.prox_thres_low;
  1121. break;
  1122. default:
  1123. return -EINVAL;
  1124. }
  1125. }
  1126. return IIO_VAL_INT;
  1127. }
  1128. static int tsl2x7x_read_raw(struct iio_dev *indio_dev,
  1129. struct iio_chan_spec const *chan,
  1130. int *val,
  1131. int *val2,
  1132. long mask)
  1133. {
  1134. int ret = -EINVAL;
  1135. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1136. switch (mask) {
  1137. case IIO_CHAN_INFO_PROCESSED:
  1138. switch (chan->type) {
  1139. case IIO_LIGHT:
  1140. tsl2x7x_get_lux(indio_dev);
  1141. *val = chip->als_cur_info.lux;
  1142. ret = IIO_VAL_INT;
  1143. break;
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. break;
  1148. case IIO_CHAN_INFO_RAW:
  1149. switch (chan->type) {
  1150. case IIO_INTENSITY:
  1151. tsl2x7x_get_lux(indio_dev);
  1152. if (chan->channel == 0)
  1153. *val = chip->als_cur_info.als_ch0;
  1154. else
  1155. *val = chip->als_cur_info.als_ch1;
  1156. ret = IIO_VAL_INT;
  1157. break;
  1158. case IIO_PROXIMITY:
  1159. tsl2x7x_get_prox(indio_dev);
  1160. *val = chip->prox_data;
  1161. ret = IIO_VAL_INT;
  1162. break;
  1163. default:
  1164. return -EINVAL;
  1165. }
  1166. break;
  1167. case IIO_CHAN_INFO_CALIBSCALE:
  1168. if (chan->type == IIO_LIGHT)
  1169. *val =
  1170. tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain];
  1171. else
  1172. *val =
  1173. tsl2X7X_prx_gainadj[chip->tsl2x7x_settings.prox_gain];
  1174. ret = IIO_VAL_INT;
  1175. break;
  1176. case IIO_CHAN_INFO_CALIBBIAS:
  1177. *val = chip->tsl2x7x_settings.als_gain_trim;
  1178. ret = IIO_VAL_INT;
  1179. break;
  1180. default:
  1181. ret = -EINVAL;
  1182. }
  1183. return ret;
  1184. }
  1185. static int tsl2x7x_write_raw(struct iio_dev *indio_dev,
  1186. struct iio_chan_spec const *chan,
  1187. int val,
  1188. int val2,
  1189. long mask)
  1190. {
  1191. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1192. switch (mask) {
  1193. case IIO_CHAN_INFO_CALIBSCALE:
  1194. if (chan->type == IIO_INTENSITY) {
  1195. switch (val) {
  1196. case 1:
  1197. chip->tsl2x7x_settings.als_gain = 0;
  1198. break;
  1199. case 8:
  1200. chip->tsl2x7x_settings.als_gain = 1;
  1201. break;
  1202. case 16:
  1203. chip->tsl2x7x_settings.als_gain = 2;
  1204. break;
  1205. case 120:
  1206. switch (chip->id) {
  1207. case tsl2572:
  1208. case tsl2672:
  1209. case tmd2672:
  1210. case tsl2772:
  1211. case tmd2772:
  1212. return -EINVAL;
  1213. }
  1214. chip->tsl2x7x_settings.als_gain = 3;
  1215. break;
  1216. case 128:
  1217. switch (chip->id) {
  1218. case tsl2571:
  1219. case tsl2671:
  1220. case tmd2671:
  1221. case tsl2771:
  1222. case tmd2771:
  1223. return -EINVAL;
  1224. }
  1225. chip->tsl2x7x_settings.als_gain = 3;
  1226. break;
  1227. default:
  1228. return -EINVAL;
  1229. }
  1230. } else {
  1231. switch (val) {
  1232. case 1:
  1233. chip->tsl2x7x_settings.prox_gain = 0;
  1234. break;
  1235. case 2:
  1236. chip->tsl2x7x_settings.prox_gain = 1;
  1237. break;
  1238. case 4:
  1239. chip->tsl2x7x_settings.prox_gain = 2;
  1240. break;
  1241. case 8:
  1242. chip->tsl2x7x_settings.prox_gain = 3;
  1243. break;
  1244. default:
  1245. return -EINVAL;
  1246. }
  1247. }
  1248. break;
  1249. case IIO_CHAN_INFO_CALIBBIAS:
  1250. chip->tsl2x7x_settings.als_gain_trim = val;
  1251. break;
  1252. default:
  1253. return -EINVAL;
  1254. }
  1255. tsl2x7x_invoke_change(indio_dev);
  1256. return 0;
  1257. }
  1258. static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
  1259. tsl2x7x_power_state_show, tsl2x7x_power_state_store);
  1260. static DEVICE_ATTR(in_proximity0_calibscale_available, S_IRUGO,
  1261. tsl2x7x_prox_gain_available_show, NULL);
  1262. static DEVICE_ATTR(in_illuminance0_calibscale_available, S_IRUGO,
  1263. tsl2x7x_gain_available_show, NULL);
  1264. static DEVICE_ATTR(in_illuminance0_integration_time, S_IRUGO | S_IWUSR,
  1265. tsl2x7x_als_time_show, tsl2x7x_als_time_store);
  1266. static DEVICE_ATTR(in_illuminance0_target_input, S_IRUGO | S_IWUSR,
  1267. tsl2x7x_als_cal_target_show, tsl2x7x_als_cal_target_store);
  1268. static DEVICE_ATTR(in_illuminance0_calibrate, S_IWUSR, NULL,
  1269. tsl2x7x_do_calibrate);
  1270. static DEVICE_ATTR(in_proximity0_calibrate, S_IWUSR, NULL,
  1271. tsl2x7x_do_prox_calibrate);
  1272. static DEVICE_ATTR(in_illuminance0_lux_table, S_IRUGO | S_IWUSR,
  1273. tsl2x7x_luxtable_show, tsl2x7x_luxtable_store);
  1274. static DEVICE_ATTR(in_intensity0_thresh_period, S_IRUGO | S_IWUSR,
  1275. tsl2x7x_als_persistence_show, tsl2x7x_als_persistence_store);
  1276. static DEVICE_ATTR(in_proximity0_thresh_period, S_IRUGO | S_IWUSR,
  1277. tsl2x7x_prox_persistence_show, tsl2x7x_prox_persistence_store);
  1278. /* Use the default register values to identify the Taos device */
  1279. static int tsl2x7x_device_id(unsigned char *id, int target)
  1280. {
  1281. switch (target) {
  1282. case tsl2571:
  1283. case tsl2671:
  1284. case tsl2771:
  1285. return (*id & 0xf0) == TRITON_ID;
  1286. case tmd2671:
  1287. case tmd2771:
  1288. return (*id & 0xf0) == HALIBUT_ID;
  1289. case tsl2572:
  1290. case tsl2672:
  1291. case tmd2672:
  1292. case tsl2772:
  1293. case tmd2772:
  1294. return (*id & 0xf0) == SWORDFISH_ID;
  1295. }
  1296. return -EINVAL;
  1297. }
  1298. static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
  1299. {
  1300. struct iio_dev *indio_dev = private;
  1301. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1302. s64 timestamp = iio_get_time_ns();
  1303. int ret;
  1304. u8 value;
  1305. value = i2c_smbus_read_byte_data(chip->client,
  1306. TSL2X7X_CMD_REG | TSL2X7X_STATUS);
  1307. /* What type of interrupt do we need to process */
  1308. if (value & TSL2X7X_STA_PRX_INTR) {
  1309. tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
  1310. iio_push_event(indio_dev,
  1311. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
  1312. 0,
  1313. IIO_EV_TYPE_THRESH,
  1314. IIO_EV_DIR_EITHER),
  1315. timestamp);
  1316. }
  1317. if (value & TSL2X7X_STA_ALS_INTR) {
  1318. tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
  1319. iio_push_event(indio_dev,
  1320. IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
  1321. 0,
  1322. IIO_EV_TYPE_THRESH,
  1323. IIO_EV_DIR_EITHER),
  1324. timestamp);
  1325. }
  1326. /* Clear interrupt now that we have handled it. */
  1327. ret = i2c_smbus_write_byte(chip->client,
  1328. TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
  1329. TSL2X7X_CMD_PROXALS_INT_CLR);
  1330. if (ret < 0)
  1331. dev_err(&chip->client->dev,
  1332. "Failed to clear irq from event handler. err = %d\n",
  1333. ret);
  1334. return IRQ_HANDLED;
  1335. }
  1336. static struct attribute *tsl2x7x_ALS_device_attrs[] = {
  1337. &dev_attr_power_state.attr,
  1338. &dev_attr_in_illuminance0_calibscale_available.attr,
  1339. &dev_attr_in_illuminance0_integration_time.attr,
  1340. &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
  1341. &dev_attr_in_illuminance0_target_input.attr,
  1342. &dev_attr_in_illuminance0_calibrate.attr,
  1343. &dev_attr_in_illuminance0_lux_table.attr,
  1344. NULL
  1345. };
  1346. static struct attribute *tsl2x7x_PRX_device_attrs[] = {
  1347. &dev_attr_power_state.attr,
  1348. &dev_attr_in_proximity0_calibrate.attr,
  1349. NULL
  1350. };
  1351. static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = {
  1352. &dev_attr_power_state.attr,
  1353. &dev_attr_in_illuminance0_calibscale_available.attr,
  1354. &dev_attr_in_illuminance0_integration_time.attr,
  1355. &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
  1356. &dev_attr_in_illuminance0_target_input.attr,
  1357. &dev_attr_in_illuminance0_calibrate.attr,
  1358. &dev_attr_in_illuminance0_lux_table.attr,
  1359. &dev_attr_in_proximity0_calibrate.attr,
  1360. NULL
  1361. };
  1362. static struct attribute *tsl2x7x_PRX2_device_attrs[] = {
  1363. &dev_attr_power_state.attr,
  1364. &dev_attr_in_proximity0_calibrate.attr,
  1365. &dev_attr_in_proximity0_calibscale_available.attr,
  1366. NULL
  1367. };
  1368. static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = {
  1369. &dev_attr_power_state.attr,
  1370. &dev_attr_in_illuminance0_calibscale_available.attr,
  1371. &dev_attr_in_illuminance0_integration_time.attr,
  1372. &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
  1373. &dev_attr_in_illuminance0_target_input.attr,
  1374. &dev_attr_in_illuminance0_calibrate.attr,
  1375. &dev_attr_in_illuminance0_lux_table.attr,
  1376. &dev_attr_in_proximity0_calibrate.attr,
  1377. &dev_attr_in_proximity0_calibscale_available.attr,
  1378. NULL
  1379. };
  1380. static struct attribute *tsl2X7X_ALS_event_attrs[] = {
  1381. &dev_attr_in_intensity0_thresh_period.attr,
  1382. NULL,
  1383. };
  1384. static struct attribute *tsl2X7X_PRX_event_attrs[] = {
  1385. &dev_attr_in_proximity0_thresh_period.attr,
  1386. NULL,
  1387. };
  1388. static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = {
  1389. &dev_attr_in_intensity0_thresh_period.attr,
  1390. &dev_attr_in_proximity0_thresh_period.attr,
  1391. NULL,
  1392. };
  1393. static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = {
  1394. [ALS] = {
  1395. .attrs = tsl2x7x_ALS_device_attrs,
  1396. },
  1397. [PRX] = {
  1398. .attrs = tsl2x7x_PRX_device_attrs,
  1399. },
  1400. [ALSPRX] = {
  1401. .attrs = tsl2x7x_ALSPRX_device_attrs,
  1402. },
  1403. [PRX2] = {
  1404. .attrs = tsl2x7x_PRX2_device_attrs,
  1405. },
  1406. [ALSPRX2] = {
  1407. .attrs = tsl2x7x_ALSPRX2_device_attrs,
  1408. },
  1409. };
  1410. static struct attribute_group tsl2X7X_event_attr_group_tbl[] = {
  1411. [ALS] = {
  1412. .attrs = tsl2X7X_ALS_event_attrs,
  1413. .name = "events",
  1414. },
  1415. [PRX] = {
  1416. .attrs = tsl2X7X_PRX_event_attrs,
  1417. .name = "events",
  1418. },
  1419. [ALSPRX] = {
  1420. .attrs = tsl2X7X_ALSPRX_event_attrs,
  1421. .name = "events",
  1422. },
  1423. };
  1424. static const struct iio_info tsl2X7X_device_info[] = {
  1425. [ALS] = {
  1426. .attrs = &tsl2X7X_device_attr_group_tbl[ALS],
  1427. .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS],
  1428. .driver_module = THIS_MODULE,
  1429. .read_raw = &tsl2x7x_read_raw,
  1430. .write_raw = &tsl2x7x_write_raw,
  1431. .read_event_value = &tsl2x7x_read_thresh,
  1432. .write_event_value = &tsl2x7x_write_thresh,
  1433. .read_event_config = &tsl2x7x_read_interrupt_config,
  1434. .write_event_config = &tsl2x7x_write_interrupt_config,
  1435. },
  1436. [PRX] = {
  1437. .attrs = &tsl2X7X_device_attr_group_tbl[PRX],
  1438. .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
  1439. .driver_module = THIS_MODULE,
  1440. .read_raw = &tsl2x7x_read_raw,
  1441. .write_raw = &tsl2x7x_write_raw,
  1442. .read_event_value = &tsl2x7x_read_thresh,
  1443. .write_event_value = &tsl2x7x_write_thresh,
  1444. .read_event_config = &tsl2x7x_read_interrupt_config,
  1445. .write_event_config = &tsl2x7x_write_interrupt_config,
  1446. },
  1447. [ALSPRX] = {
  1448. .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX],
  1449. .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
  1450. .driver_module = THIS_MODULE,
  1451. .read_raw = &tsl2x7x_read_raw,
  1452. .write_raw = &tsl2x7x_write_raw,
  1453. .read_event_value = &tsl2x7x_read_thresh,
  1454. .write_event_value = &tsl2x7x_write_thresh,
  1455. .read_event_config = &tsl2x7x_read_interrupt_config,
  1456. .write_event_config = &tsl2x7x_write_interrupt_config,
  1457. },
  1458. [PRX2] = {
  1459. .attrs = &tsl2X7X_device_attr_group_tbl[PRX2],
  1460. .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
  1461. .driver_module = THIS_MODULE,
  1462. .read_raw = &tsl2x7x_read_raw,
  1463. .write_raw = &tsl2x7x_write_raw,
  1464. .read_event_value = &tsl2x7x_read_thresh,
  1465. .write_event_value = &tsl2x7x_write_thresh,
  1466. .read_event_config = &tsl2x7x_read_interrupt_config,
  1467. .write_event_config = &tsl2x7x_write_interrupt_config,
  1468. },
  1469. [ALSPRX2] = {
  1470. .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2],
  1471. .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
  1472. .driver_module = THIS_MODULE,
  1473. .read_raw = &tsl2x7x_read_raw,
  1474. .write_raw = &tsl2x7x_write_raw,
  1475. .read_event_value = &tsl2x7x_read_thresh,
  1476. .write_event_value = &tsl2x7x_write_thresh,
  1477. .read_event_config = &tsl2x7x_read_interrupt_config,
  1478. .write_event_config = &tsl2x7x_write_interrupt_config,
  1479. },
  1480. };
  1481. static const struct iio_event_spec tsl2x7x_events[] = {
  1482. {
  1483. .type = IIO_EV_TYPE_THRESH,
  1484. .dir = IIO_EV_DIR_RISING,
  1485. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  1486. BIT(IIO_EV_INFO_ENABLE),
  1487. }, {
  1488. .type = IIO_EV_TYPE_THRESH,
  1489. .dir = IIO_EV_DIR_FALLING,
  1490. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  1491. BIT(IIO_EV_INFO_ENABLE),
  1492. },
  1493. };
  1494. static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl[] = {
  1495. [ALS] = {
  1496. .channel = {
  1497. {
  1498. .type = IIO_LIGHT,
  1499. .indexed = 1,
  1500. .channel = 0,
  1501. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1502. }, {
  1503. .type = IIO_INTENSITY,
  1504. .indexed = 1,
  1505. .channel = 0,
  1506. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1507. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1508. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1509. .event_spec = tsl2x7x_events,
  1510. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1511. }, {
  1512. .type = IIO_INTENSITY,
  1513. .indexed = 1,
  1514. .channel = 1,
  1515. },
  1516. },
  1517. .chan_table_elements = 3,
  1518. .info = &tsl2X7X_device_info[ALS],
  1519. },
  1520. [PRX] = {
  1521. .channel = {
  1522. {
  1523. .type = IIO_PROXIMITY,
  1524. .indexed = 1,
  1525. .channel = 0,
  1526. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1527. .event_spec = tsl2x7x_events,
  1528. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1529. },
  1530. },
  1531. .chan_table_elements = 1,
  1532. .info = &tsl2X7X_device_info[PRX],
  1533. },
  1534. [ALSPRX] = {
  1535. .channel = {
  1536. {
  1537. .type = IIO_LIGHT,
  1538. .indexed = 1,
  1539. .channel = 0,
  1540. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED)
  1541. }, {
  1542. .type = IIO_INTENSITY,
  1543. .indexed = 1,
  1544. .channel = 0,
  1545. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1546. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1547. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1548. .event_spec = tsl2x7x_events,
  1549. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1550. }, {
  1551. .type = IIO_INTENSITY,
  1552. .indexed = 1,
  1553. .channel = 1,
  1554. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1555. }, {
  1556. .type = IIO_PROXIMITY,
  1557. .indexed = 1,
  1558. .channel = 0,
  1559. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1560. .event_spec = tsl2x7x_events,
  1561. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1562. },
  1563. },
  1564. .chan_table_elements = 4,
  1565. .info = &tsl2X7X_device_info[ALSPRX],
  1566. },
  1567. [PRX2] = {
  1568. .channel = {
  1569. {
  1570. .type = IIO_PROXIMITY,
  1571. .indexed = 1,
  1572. .channel = 0,
  1573. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1574. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1575. .event_spec = tsl2x7x_events,
  1576. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1577. },
  1578. },
  1579. .chan_table_elements = 1,
  1580. .info = &tsl2X7X_device_info[PRX2],
  1581. },
  1582. [ALSPRX2] = {
  1583. .channel = {
  1584. {
  1585. .type = IIO_LIGHT,
  1586. .indexed = 1,
  1587. .channel = 0,
  1588. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  1589. }, {
  1590. .type = IIO_INTENSITY,
  1591. .indexed = 1,
  1592. .channel = 0,
  1593. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1594. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  1595. BIT(IIO_CHAN_INFO_CALIBBIAS),
  1596. .event_spec = tsl2x7x_events,
  1597. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1598. }, {
  1599. .type = IIO_INTENSITY,
  1600. .indexed = 1,
  1601. .channel = 1,
  1602. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  1603. }, {
  1604. .type = IIO_PROXIMITY,
  1605. .indexed = 1,
  1606. .channel = 0,
  1607. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1608. BIT(IIO_CHAN_INFO_CALIBSCALE),
  1609. .event_spec = tsl2x7x_events,
  1610. .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
  1611. },
  1612. },
  1613. .chan_table_elements = 4,
  1614. .info = &tsl2X7X_device_info[ALSPRX2],
  1615. },
  1616. };
  1617. static int tsl2x7x_probe(struct i2c_client *clientp,
  1618. const struct i2c_device_id *id)
  1619. {
  1620. int ret;
  1621. unsigned char device_id;
  1622. struct iio_dev *indio_dev;
  1623. struct tsl2X7X_chip *chip;
  1624. indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
  1625. if (!indio_dev)
  1626. return -ENOMEM;
  1627. chip = iio_priv(indio_dev);
  1628. chip->client = clientp;
  1629. i2c_set_clientdata(clientp, indio_dev);
  1630. ret = tsl2x7x_i2c_read(chip->client,
  1631. TSL2X7X_CHIPID, &device_id);
  1632. if (ret < 0)
  1633. return ret;
  1634. if ((!tsl2x7x_device_id(&device_id, id->driver_data)) ||
  1635. (tsl2x7x_device_id(&device_id, id->driver_data) == -EINVAL)) {
  1636. dev_info(&chip->client->dev,
  1637. "%s: i2c device found does not match expected id\n",
  1638. __func__);
  1639. return -EINVAL;
  1640. }
  1641. ret = i2c_smbus_write_byte(clientp, (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
  1642. if (ret < 0) {
  1643. dev_err(&clientp->dev, "write to cmd reg failed. err = %d\n",
  1644. ret);
  1645. return ret;
  1646. }
  1647. /* ALS and PROX functions can be invoked via user space poll
  1648. * or H/W interrupt. If busy return last sample. */
  1649. mutex_init(&chip->als_mutex);
  1650. mutex_init(&chip->prox_mutex);
  1651. chip->tsl2x7x_chip_status = TSL2X7X_CHIP_UNKNOWN;
  1652. chip->pdata = clientp->dev.platform_data;
  1653. chip->id = id->driver_data;
  1654. chip->chip_info =
  1655. &tsl2x7x_chip_info_tbl[device_channel_config[id->driver_data]];
  1656. indio_dev->info = chip->chip_info->info;
  1657. indio_dev->dev.parent = &clientp->dev;
  1658. indio_dev->modes = INDIO_DIRECT_MODE;
  1659. indio_dev->name = chip->client->name;
  1660. indio_dev->channels = chip->chip_info->channel;
  1661. indio_dev->num_channels = chip->chip_info->chan_table_elements;
  1662. if (clientp->irq) {
  1663. ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
  1664. NULL,
  1665. &tsl2x7x_event_handler,
  1666. IRQF_TRIGGER_RISING |
  1667. IRQF_ONESHOT,
  1668. "TSL2X7X_event",
  1669. indio_dev);
  1670. if (ret) {
  1671. dev_err(&clientp->dev,
  1672. "%s: irq request failed", __func__);
  1673. return ret;
  1674. }
  1675. }
  1676. /* Load up the defaults */
  1677. tsl2x7x_defaults(chip);
  1678. /* Make sure the chip is on */
  1679. tsl2x7x_chip_on(indio_dev);
  1680. ret = iio_device_register(indio_dev);
  1681. if (ret) {
  1682. dev_err(&clientp->dev,
  1683. "%s: iio registration failed\n", __func__);
  1684. return ret;
  1685. }
  1686. dev_info(&clientp->dev, "%s Light sensor found.\n", id->name);
  1687. return 0;
  1688. }
  1689. static int tsl2x7x_suspend(struct device *dev)
  1690. {
  1691. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1692. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1693. int ret = 0;
  1694. if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
  1695. ret = tsl2x7x_chip_off(indio_dev);
  1696. chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
  1697. }
  1698. if (chip->pdata && chip->pdata->platform_power) {
  1699. pm_message_t pmm = {PM_EVENT_SUSPEND};
  1700. chip->pdata->platform_power(dev, pmm);
  1701. }
  1702. return ret;
  1703. }
  1704. static int tsl2x7x_resume(struct device *dev)
  1705. {
  1706. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1707. struct tsl2X7X_chip *chip = iio_priv(indio_dev);
  1708. int ret = 0;
  1709. if (chip->pdata && chip->pdata->platform_power) {
  1710. pm_message_t pmm = {PM_EVENT_RESUME};
  1711. chip->pdata->platform_power(dev, pmm);
  1712. }
  1713. if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_SUSPENDED)
  1714. ret = tsl2x7x_chip_on(indio_dev);
  1715. return ret;
  1716. }
  1717. static int tsl2x7x_remove(struct i2c_client *client)
  1718. {
  1719. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1720. tsl2x7x_chip_off(indio_dev);
  1721. iio_device_unregister(indio_dev);
  1722. return 0;
  1723. }
  1724. static struct i2c_device_id tsl2x7x_idtable[] = {
  1725. { "tsl2571", tsl2571 },
  1726. { "tsl2671", tsl2671 },
  1727. { "tmd2671", tmd2671 },
  1728. { "tsl2771", tsl2771 },
  1729. { "tmd2771", tmd2771 },
  1730. { "tsl2572", tsl2572 },
  1731. { "tsl2672", tsl2672 },
  1732. { "tmd2672", tmd2672 },
  1733. { "tsl2772", tsl2772 },
  1734. { "tmd2772", tmd2772 },
  1735. {}
  1736. };
  1737. MODULE_DEVICE_TABLE(i2c, tsl2x7x_idtable);
  1738. static const struct dev_pm_ops tsl2x7x_pm_ops = {
  1739. .suspend = tsl2x7x_suspend,
  1740. .resume = tsl2x7x_resume,
  1741. };
  1742. /* Driver definition */
  1743. static struct i2c_driver tsl2x7x_driver = {
  1744. .driver = {
  1745. .name = "tsl2x7x",
  1746. .pm = &tsl2x7x_pm_ops,
  1747. },
  1748. .id_table = tsl2x7x_idtable,
  1749. .probe = tsl2x7x_probe,
  1750. .remove = tsl2x7x_remove,
  1751. };
  1752. module_i2c_driver(tsl2x7x_driver);
  1753. MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
  1754. MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
  1755. MODULE_LICENSE("GPL");