ltr501.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*
  2. * ltr501.c - Support for Lite-On LTR501 ambient light and proximity sensor
  3. *
  4. * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * 7-bit I2C slave address 0x23
  11. *
  12. * TODO: IR LED characteristics
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/regmap.h>
  19. #include <linux/acpi.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/events.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/iio/buffer.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #define LTR501_DRV_NAME "ltr501"
  27. #define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */
  28. #define LTR501_PS_CONTR 0x81 /* PS operation mode */
  29. #define LTR501_PS_MEAS_RATE 0x84 /* measurement rate*/
  30. #define LTR501_ALS_MEAS_RATE 0x85 /* ALS integ time, measurement rate*/
  31. #define LTR501_PART_ID 0x86
  32. #define LTR501_MANUFAC_ID 0x87
  33. #define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */
  34. #define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */
  35. #define LTR501_ALS_PS_STATUS 0x8c
  36. #define LTR501_PS_DATA 0x8d /* 16-bit, little endian */
  37. #define LTR501_INTR 0x8f /* output mode, polarity, mode */
  38. #define LTR501_PS_THRESH_UP 0x90 /* 11 bit, ps upper threshold */
  39. #define LTR501_PS_THRESH_LOW 0x92 /* 11 bit, ps lower threshold */
  40. #define LTR501_ALS_THRESH_UP 0x97 /* 16 bit, ALS upper threshold */
  41. #define LTR501_ALS_THRESH_LOW 0x99 /* 16 bit, ALS lower threshold */
  42. #define LTR501_INTR_PRST 0x9e /* ps thresh, als thresh */
  43. #define LTR501_MAX_REG 0x9f
  44. #define LTR501_ALS_CONTR_SW_RESET BIT(2)
  45. #define LTR501_CONTR_PS_GAIN_MASK (BIT(3) | BIT(2))
  46. #define LTR501_CONTR_PS_GAIN_SHIFT 2
  47. #define LTR501_CONTR_ALS_GAIN_MASK BIT(3)
  48. #define LTR501_CONTR_ACTIVE BIT(1)
  49. #define LTR501_STATUS_ALS_INTR BIT(3)
  50. #define LTR501_STATUS_ALS_RDY BIT(2)
  51. #define LTR501_STATUS_PS_INTR BIT(1)
  52. #define LTR501_STATUS_PS_RDY BIT(0)
  53. #define LTR501_PS_DATA_MASK 0x7ff
  54. #define LTR501_PS_THRESH_MASK 0x7ff
  55. #define LTR501_ALS_THRESH_MASK 0xffff
  56. #define LTR501_ALS_DEF_PERIOD 500000
  57. #define LTR501_PS_DEF_PERIOD 100000
  58. #define LTR501_REGMAP_NAME "ltr501_regmap"
  59. #define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
  60. ((vis_coeff * vis_data) - (ir_coeff * ir_data))
  61. static const int int_time_mapping[] = {100000, 50000, 200000, 400000};
  62. static const struct reg_field reg_field_it =
  63. REG_FIELD(LTR501_ALS_MEAS_RATE, 3, 4);
  64. static const struct reg_field reg_field_als_intr =
  65. REG_FIELD(LTR501_INTR, 1, 1);
  66. static const struct reg_field reg_field_ps_intr =
  67. REG_FIELD(LTR501_INTR, 0, 0);
  68. static const struct reg_field reg_field_als_rate =
  69. REG_FIELD(LTR501_ALS_MEAS_RATE, 0, 2);
  70. static const struct reg_field reg_field_ps_rate =
  71. REG_FIELD(LTR501_PS_MEAS_RATE, 0, 3);
  72. static const struct reg_field reg_field_als_prst =
  73. REG_FIELD(LTR501_INTR_PRST, 0, 3);
  74. static const struct reg_field reg_field_ps_prst =
  75. REG_FIELD(LTR501_INTR_PRST, 4, 7);
  76. struct ltr501_samp_table {
  77. int freq_val; /* repetition frequency in micro HZ*/
  78. int time_val; /* repetition rate in micro seconds */
  79. };
  80. #define LTR501_RESERVED_GAIN -1
  81. enum {
  82. ltr501 = 0,
  83. ltr559,
  84. ltr301,
  85. };
  86. struct ltr501_gain {
  87. int scale;
  88. int uscale;
  89. };
  90. static struct ltr501_gain ltr501_als_gain_tbl[] = {
  91. {1, 0},
  92. {0, 5000},
  93. };
  94. static struct ltr501_gain ltr559_als_gain_tbl[] = {
  95. {1, 0},
  96. {0, 500000},
  97. {0, 250000},
  98. {0, 125000},
  99. {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN},
  100. {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN},
  101. {0, 20000},
  102. {0, 10000},
  103. };
  104. static struct ltr501_gain ltr501_ps_gain_tbl[] = {
  105. {1, 0},
  106. {0, 250000},
  107. {0, 125000},
  108. {0, 62500},
  109. };
  110. static struct ltr501_gain ltr559_ps_gain_tbl[] = {
  111. {0, 62500}, /* x16 gain */
  112. {0, 31250}, /* x32 gain */
  113. {0, 15625}, /* bits X1 are for x64 gain */
  114. {0, 15624},
  115. };
  116. struct ltr501_chip_info {
  117. u8 partid;
  118. struct ltr501_gain *als_gain;
  119. int als_gain_tbl_size;
  120. struct ltr501_gain *ps_gain;
  121. int ps_gain_tbl_size;
  122. u8 als_mode_active;
  123. u8 als_gain_mask;
  124. u8 als_gain_shift;
  125. struct iio_chan_spec const *channels;
  126. const int no_channels;
  127. const struct iio_info *info;
  128. const struct iio_info *info_no_irq;
  129. };
  130. struct ltr501_data {
  131. struct i2c_client *client;
  132. struct mutex lock_als, lock_ps;
  133. struct ltr501_chip_info *chip_info;
  134. u8 als_contr, ps_contr;
  135. int als_period, ps_period; /* period in micro seconds */
  136. struct regmap *regmap;
  137. struct regmap_field *reg_it;
  138. struct regmap_field *reg_als_intr;
  139. struct regmap_field *reg_ps_intr;
  140. struct regmap_field *reg_als_rate;
  141. struct regmap_field *reg_ps_rate;
  142. struct regmap_field *reg_als_prst;
  143. struct regmap_field *reg_ps_prst;
  144. };
  145. static const struct ltr501_samp_table ltr501_als_samp_table[] = {
  146. {20000000, 50000}, {10000000, 100000},
  147. {5000000, 200000}, {2000000, 500000},
  148. {1000000, 1000000}, {500000, 2000000},
  149. {500000, 2000000}, {500000, 2000000}
  150. };
  151. static const struct ltr501_samp_table ltr501_ps_samp_table[] = {
  152. {20000000, 50000}, {14285714, 70000},
  153. {10000000, 100000}, {5000000, 200000},
  154. {2000000, 500000}, {1000000, 1000000},
  155. {500000, 2000000}, {500000, 2000000},
  156. {500000, 2000000}
  157. };
  158. static int ltr501_match_samp_freq(const struct ltr501_samp_table *tab,
  159. int len, int val, int val2)
  160. {
  161. int i, freq;
  162. freq = val * 1000000 + val2;
  163. for (i = 0; i < len; i++) {
  164. if (tab[i].freq_val == freq)
  165. return i;
  166. }
  167. return -EINVAL;
  168. }
  169. static int ltr501_als_read_samp_freq(struct ltr501_data *data,
  170. int *val, int *val2)
  171. {
  172. int ret, i;
  173. ret = regmap_field_read(data->reg_als_rate, &i);
  174. if (ret < 0)
  175. return ret;
  176. if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table))
  177. return -EINVAL;
  178. *val = ltr501_als_samp_table[i].freq_val / 1000000;
  179. *val2 = ltr501_als_samp_table[i].freq_val % 1000000;
  180. return IIO_VAL_INT_PLUS_MICRO;
  181. }
  182. static int ltr501_ps_read_samp_freq(struct ltr501_data *data,
  183. int *val, int *val2)
  184. {
  185. int ret, i;
  186. ret = regmap_field_read(data->reg_ps_rate, &i);
  187. if (ret < 0)
  188. return ret;
  189. if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table))
  190. return -EINVAL;
  191. *val = ltr501_ps_samp_table[i].freq_val / 1000000;
  192. *val2 = ltr501_ps_samp_table[i].freq_val % 1000000;
  193. return IIO_VAL_INT_PLUS_MICRO;
  194. }
  195. static int ltr501_als_write_samp_freq(struct ltr501_data *data,
  196. int val, int val2)
  197. {
  198. int i, ret;
  199. i = ltr501_match_samp_freq(ltr501_als_samp_table,
  200. ARRAY_SIZE(ltr501_als_samp_table),
  201. val, val2);
  202. if (i < 0)
  203. return i;
  204. mutex_lock(&data->lock_als);
  205. ret = regmap_field_write(data->reg_als_rate, i);
  206. mutex_unlock(&data->lock_als);
  207. return ret;
  208. }
  209. static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
  210. int val, int val2)
  211. {
  212. int i, ret;
  213. i = ltr501_match_samp_freq(ltr501_ps_samp_table,
  214. ARRAY_SIZE(ltr501_ps_samp_table),
  215. val, val2);
  216. if (i < 0)
  217. return i;
  218. mutex_lock(&data->lock_ps);
  219. ret = regmap_field_write(data->reg_ps_rate, i);
  220. mutex_unlock(&data->lock_ps);
  221. return ret;
  222. }
  223. static int ltr501_als_read_samp_period(struct ltr501_data *data, int *val)
  224. {
  225. int ret, i;
  226. ret = regmap_field_read(data->reg_als_rate, &i);
  227. if (ret < 0)
  228. return ret;
  229. if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table))
  230. return -EINVAL;
  231. *val = ltr501_als_samp_table[i].time_val;
  232. return IIO_VAL_INT;
  233. }
  234. static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val)
  235. {
  236. int ret, i;
  237. ret = regmap_field_read(data->reg_ps_rate, &i);
  238. if (ret < 0)
  239. return ret;
  240. if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table))
  241. return -EINVAL;
  242. *val = ltr501_ps_samp_table[i].time_val;
  243. return IIO_VAL_INT;
  244. }
  245. /* IR and visible spectrum coeff's are given in data sheet */
  246. static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data)
  247. {
  248. unsigned long ratio, lux;
  249. if (vis_data == 0)
  250. return 0;
  251. /* multiply numerator by 100 to avoid handling ratio < 1 */
  252. ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data);
  253. if (ratio < 45)
  254. lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data);
  255. else if (ratio >= 45 && ratio < 64)
  256. lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data);
  257. else if (ratio >= 64 && ratio < 85)
  258. lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data);
  259. else
  260. lux = 0;
  261. return lux / 1000;
  262. }
  263. static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask)
  264. {
  265. int tries = 100;
  266. int ret, status;
  267. while (tries--) {
  268. ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status);
  269. if (ret < 0)
  270. return ret;
  271. if ((status & drdy_mask) == drdy_mask)
  272. return 0;
  273. msleep(25);
  274. }
  275. dev_err(&data->client->dev, "ltr501_drdy() failed, data not ready\n");
  276. return -EIO;
  277. }
  278. static int ltr501_set_it_time(struct ltr501_data *data, int it)
  279. {
  280. int ret, i, index = -1, status;
  281. for (i = 0; i < ARRAY_SIZE(int_time_mapping); i++) {
  282. if (int_time_mapping[i] == it) {
  283. index = i;
  284. break;
  285. }
  286. }
  287. /* Make sure integ time index is valid */
  288. if (index < 0)
  289. return -EINVAL;
  290. ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status);
  291. if (ret < 0)
  292. return ret;
  293. if (status & LTR501_CONTR_ALS_GAIN_MASK) {
  294. /*
  295. * 200 ms and 400 ms integ time can only be
  296. * used in dynamic range 1
  297. */
  298. if (index > 1)
  299. return -EINVAL;
  300. } else
  301. /* 50 ms integ time can only be used in dynamic range 2 */
  302. if (index == 1)
  303. return -EINVAL;
  304. return regmap_field_write(data->reg_it, index);
  305. }
  306. /* read int time in micro seconds */
  307. static int ltr501_read_it_time(struct ltr501_data *data, int *val, int *val2)
  308. {
  309. int ret, index;
  310. ret = regmap_field_read(data->reg_it, &index);
  311. if (ret < 0)
  312. return ret;
  313. /* Make sure integ time index is valid */
  314. if (index < 0 || index >= ARRAY_SIZE(int_time_mapping))
  315. return -EINVAL;
  316. *val2 = int_time_mapping[index];
  317. *val = 0;
  318. return IIO_VAL_INT_PLUS_MICRO;
  319. }
  320. static int ltr501_read_als(struct ltr501_data *data, __le16 buf[2])
  321. {
  322. int ret;
  323. ret = ltr501_drdy(data, LTR501_STATUS_ALS_RDY);
  324. if (ret < 0)
  325. return ret;
  326. /* always read both ALS channels in given order */
  327. return regmap_bulk_read(data->regmap, LTR501_ALS_DATA1,
  328. buf, 2 * sizeof(__le16));
  329. }
  330. static int ltr501_read_ps(struct ltr501_data *data)
  331. {
  332. int ret, status;
  333. ret = ltr501_drdy(data, LTR501_STATUS_PS_RDY);
  334. if (ret < 0)
  335. return ret;
  336. ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA,
  337. &status, 2);
  338. if (ret < 0)
  339. return ret;
  340. return status;
  341. }
  342. static int ltr501_read_intr_prst(struct ltr501_data *data,
  343. enum iio_chan_type type,
  344. int *val2)
  345. {
  346. int ret, samp_period, prst;
  347. switch (type) {
  348. case IIO_INTENSITY:
  349. ret = regmap_field_read(data->reg_als_prst, &prst);
  350. if (ret < 0)
  351. return ret;
  352. ret = ltr501_als_read_samp_period(data, &samp_period);
  353. if (ret < 0)
  354. return ret;
  355. *val2 = samp_period * prst;
  356. return IIO_VAL_INT_PLUS_MICRO;
  357. case IIO_PROXIMITY:
  358. ret = regmap_field_read(data->reg_ps_prst, &prst);
  359. if (ret < 0)
  360. return ret;
  361. ret = ltr501_ps_read_samp_period(data, &samp_period);
  362. if (ret < 0)
  363. return ret;
  364. *val2 = samp_period * prst;
  365. return IIO_VAL_INT_PLUS_MICRO;
  366. default:
  367. return -EINVAL;
  368. }
  369. return -EINVAL;
  370. }
  371. static int ltr501_write_intr_prst(struct ltr501_data *data,
  372. enum iio_chan_type type,
  373. int val, int val2)
  374. {
  375. int ret, samp_period, new_val;
  376. unsigned long period;
  377. if (val < 0 || val2 < 0)
  378. return -EINVAL;
  379. /* period in microseconds */
  380. period = ((val * 1000000) + val2);
  381. switch (type) {
  382. case IIO_INTENSITY:
  383. ret = ltr501_als_read_samp_period(data, &samp_period);
  384. if (ret < 0)
  385. return ret;
  386. /* period should be atleast equal to sampling period */
  387. if (period < samp_period)
  388. return -EINVAL;
  389. new_val = DIV_ROUND_UP(period, samp_period);
  390. if (new_val < 0 || new_val > 0x0f)
  391. return -EINVAL;
  392. mutex_lock(&data->lock_als);
  393. ret = regmap_field_write(data->reg_als_prst, new_val);
  394. mutex_unlock(&data->lock_als);
  395. if (ret >= 0)
  396. data->als_period = period;
  397. return ret;
  398. case IIO_PROXIMITY:
  399. ret = ltr501_ps_read_samp_period(data, &samp_period);
  400. if (ret < 0)
  401. return ret;
  402. /* period should be atleast equal to rate */
  403. if (period < samp_period)
  404. return -EINVAL;
  405. new_val = DIV_ROUND_UP(period, samp_period);
  406. if (new_val < 0 || new_val > 0x0f)
  407. return -EINVAL;
  408. mutex_lock(&data->lock_ps);
  409. ret = regmap_field_write(data->reg_ps_prst, new_val);
  410. mutex_unlock(&data->lock_ps);
  411. if (ret >= 0)
  412. data->ps_period = period;
  413. return ret;
  414. default:
  415. return -EINVAL;
  416. }
  417. return -EINVAL;
  418. }
  419. static const struct iio_event_spec ltr501_als_event_spec[] = {
  420. {
  421. .type = IIO_EV_TYPE_THRESH,
  422. .dir = IIO_EV_DIR_RISING,
  423. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  424. }, {
  425. .type = IIO_EV_TYPE_THRESH,
  426. .dir = IIO_EV_DIR_FALLING,
  427. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  428. }, {
  429. .type = IIO_EV_TYPE_THRESH,
  430. .dir = IIO_EV_DIR_EITHER,
  431. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  432. BIT(IIO_EV_INFO_PERIOD),
  433. },
  434. };
  435. static const struct iio_event_spec ltr501_pxs_event_spec[] = {
  436. {
  437. .type = IIO_EV_TYPE_THRESH,
  438. .dir = IIO_EV_DIR_RISING,
  439. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  440. }, {
  441. .type = IIO_EV_TYPE_THRESH,
  442. .dir = IIO_EV_DIR_FALLING,
  443. .mask_separate = BIT(IIO_EV_INFO_VALUE),
  444. }, {
  445. .type = IIO_EV_TYPE_THRESH,
  446. .dir = IIO_EV_DIR_EITHER,
  447. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  448. BIT(IIO_EV_INFO_PERIOD),
  449. },
  450. };
  451. #define LTR501_INTENSITY_CHANNEL(_idx, _addr, _mod, _shared, \
  452. _evspec, _evsize) { \
  453. .type = IIO_INTENSITY, \
  454. .modified = 1, \
  455. .address = (_addr), \
  456. .channel2 = (_mod), \
  457. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  458. .info_mask_shared_by_type = (_shared), \
  459. .scan_index = (_idx), \
  460. .scan_type = { \
  461. .sign = 'u', \
  462. .realbits = 16, \
  463. .storagebits = 16, \
  464. .endianness = IIO_CPU, \
  465. }, \
  466. .event_spec = _evspec,\
  467. .num_event_specs = _evsize,\
  468. }
  469. #define LTR501_LIGHT_CHANNEL() { \
  470. .type = IIO_LIGHT, \
  471. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  472. .scan_index = -1, \
  473. }
  474. static const struct iio_chan_spec ltr501_channels[] = {
  475. LTR501_LIGHT_CHANNEL(),
  476. LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
  477. ltr501_als_event_spec,
  478. ARRAY_SIZE(ltr501_als_event_spec)),
  479. LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR,
  480. BIT(IIO_CHAN_INFO_SCALE) |
  481. BIT(IIO_CHAN_INFO_INT_TIME) |
  482. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  483. NULL, 0),
  484. {
  485. .type = IIO_PROXIMITY,
  486. .address = LTR501_PS_DATA,
  487. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  488. BIT(IIO_CHAN_INFO_SCALE),
  489. .scan_index = 2,
  490. .scan_type = {
  491. .sign = 'u',
  492. .realbits = 11,
  493. .storagebits = 16,
  494. .endianness = IIO_CPU,
  495. },
  496. .event_spec = ltr501_pxs_event_spec,
  497. .num_event_specs = ARRAY_SIZE(ltr501_pxs_event_spec),
  498. },
  499. IIO_CHAN_SOFT_TIMESTAMP(3),
  500. };
  501. static const struct iio_chan_spec ltr301_channels[] = {
  502. LTR501_LIGHT_CHANNEL(),
  503. LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0,
  504. ltr501_als_event_spec,
  505. ARRAY_SIZE(ltr501_als_event_spec)),
  506. LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR,
  507. BIT(IIO_CHAN_INFO_SCALE) |
  508. BIT(IIO_CHAN_INFO_INT_TIME) |
  509. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  510. NULL, 0),
  511. IIO_CHAN_SOFT_TIMESTAMP(2),
  512. };
  513. static int ltr501_read_raw(struct iio_dev *indio_dev,
  514. struct iio_chan_spec const *chan,
  515. int *val, int *val2, long mask)
  516. {
  517. struct ltr501_data *data = iio_priv(indio_dev);
  518. __le16 buf[2];
  519. int ret, i;
  520. switch (mask) {
  521. case IIO_CHAN_INFO_PROCESSED:
  522. if (iio_buffer_enabled(indio_dev))
  523. return -EBUSY;
  524. switch (chan->type) {
  525. case IIO_LIGHT:
  526. mutex_lock(&data->lock_als);
  527. ret = ltr501_read_als(data, buf);
  528. mutex_unlock(&data->lock_als);
  529. if (ret < 0)
  530. return ret;
  531. *val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
  532. le16_to_cpu(buf[0]));
  533. return IIO_VAL_INT;
  534. default:
  535. return -EINVAL;
  536. }
  537. case IIO_CHAN_INFO_RAW:
  538. if (iio_buffer_enabled(indio_dev))
  539. return -EBUSY;
  540. switch (chan->type) {
  541. case IIO_INTENSITY:
  542. mutex_lock(&data->lock_als);
  543. ret = ltr501_read_als(data, buf);
  544. mutex_unlock(&data->lock_als);
  545. if (ret < 0)
  546. return ret;
  547. *val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
  548. buf[0] : buf[1]);
  549. return IIO_VAL_INT;
  550. case IIO_PROXIMITY:
  551. mutex_lock(&data->lock_ps);
  552. ret = ltr501_read_ps(data);
  553. mutex_unlock(&data->lock_ps);
  554. if (ret < 0)
  555. return ret;
  556. *val = ret & LTR501_PS_DATA_MASK;
  557. return IIO_VAL_INT;
  558. default:
  559. return -EINVAL;
  560. }
  561. case IIO_CHAN_INFO_SCALE:
  562. switch (chan->type) {
  563. case IIO_INTENSITY:
  564. i = (data->als_contr & data->chip_info->als_gain_mask)
  565. >> data->chip_info->als_gain_shift;
  566. *val = data->chip_info->als_gain[i].scale;
  567. *val2 = data->chip_info->als_gain[i].uscale;
  568. return IIO_VAL_INT_PLUS_MICRO;
  569. case IIO_PROXIMITY:
  570. i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >>
  571. LTR501_CONTR_PS_GAIN_SHIFT;
  572. *val = data->chip_info->ps_gain[i].scale;
  573. *val2 = data->chip_info->ps_gain[i].uscale;
  574. return IIO_VAL_INT_PLUS_MICRO;
  575. default:
  576. return -EINVAL;
  577. }
  578. case IIO_CHAN_INFO_INT_TIME:
  579. switch (chan->type) {
  580. case IIO_INTENSITY:
  581. return ltr501_read_it_time(data, val, val2);
  582. default:
  583. return -EINVAL;
  584. }
  585. case IIO_CHAN_INFO_SAMP_FREQ:
  586. switch (chan->type) {
  587. case IIO_INTENSITY:
  588. return ltr501_als_read_samp_freq(data, val, val2);
  589. case IIO_PROXIMITY:
  590. return ltr501_ps_read_samp_freq(data, val, val2);
  591. default:
  592. return -EINVAL;
  593. }
  594. }
  595. return -EINVAL;
  596. }
  597. static int ltr501_get_gain_index(struct ltr501_gain *gain, int size,
  598. int val, int val2)
  599. {
  600. int i;
  601. for (i = 0; i < size; i++)
  602. if (val == gain[i].scale && val2 == gain[i].uscale)
  603. return i;
  604. return -1;
  605. }
  606. static int ltr501_write_raw(struct iio_dev *indio_dev,
  607. struct iio_chan_spec const *chan,
  608. int val, int val2, long mask)
  609. {
  610. struct ltr501_data *data = iio_priv(indio_dev);
  611. int i, ret, freq_val, freq_val2;
  612. struct ltr501_chip_info *info = data->chip_info;
  613. if (iio_buffer_enabled(indio_dev))
  614. return -EBUSY;
  615. switch (mask) {
  616. case IIO_CHAN_INFO_SCALE:
  617. switch (chan->type) {
  618. case IIO_INTENSITY:
  619. i = ltr501_get_gain_index(info->als_gain,
  620. info->als_gain_tbl_size,
  621. val, val2);
  622. if (i < 0)
  623. return -EINVAL;
  624. data->als_contr &= ~info->als_gain_mask;
  625. data->als_contr |= i << info->als_gain_shift;
  626. return regmap_write(data->regmap, LTR501_ALS_CONTR,
  627. data->als_contr);
  628. case IIO_PROXIMITY:
  629. i = ltr501_get_gain_index(info->ps_gain,
  630. info->ps_gain_tbl_size,
  631. val, val2);
  632. if (i < 0)
  633. return -EINVAL;
  634. data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK;
  635. data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT;
  636. return regmap_write(data->regmap, LTR501_PS_CONTR,
  637. data->ps_contr);
  638. default:
  639. return -EINVAL;
  640. }
  641. case IIO_CHAN_INFO_INT_TIME:
  642. switch (chan->type) {
  643. case IIO_INTENSITY:
  644. if (val != 0)
  645. return -EINVAL;
  646. mutex_lock(&data->lock_als);
  647. i = ltr501_set_it_time(data, val2);
  648. mutex_unlock(&data->lock_als);
  649. return i;
  650. default:
  651. return -EINVAL;
  652. }
  653. case IIO_CHAN_INFO_SAMP_FREQ:
  654. switch (chan->type) {
  655. case IIO_INTENSITY:
  656. ret = ltr501_als_read_samp_freq(data, &freq_val,
  657. &freq_val2);
  658. if (ret < 0)
  659. return ret;
  660. ret = ltr501_als_write_samp_freq(data, val, val2);
  661. if (ret < 0)
  662. return ret;
  663. /* update persistence count when changing frequency */
  664. ret = ltr501_write_intr_prst(data, chan->type,
  665. 0, data->als_period);
  666. if (ret < 0)
  667. return ltr501_als_write_samp_freq(data,
  668. freq_val,
  669. freq_val2);
  670. return ret;
  671. case IIO_PROXIMITY:
  672. ret = ltr501_ps_read_samp_freq(data, &freq_val,
  673. &freq_val2);
  674. if (ret < 0)
  675. return ret;
  676. ret = ltr501_ps_write_samp_freq(data, val, val2);
  677. if (ret < 0)
  678. return ret;
  679. /* update persistence count when changing frequency */
  680. ret = ltr501_write_intr_prst(data, chan->type,
  681. 0, data->ps_period);
  682. if (ret < 0)
  683. return ltr501_ps_write_samp_freq(data,
  684. freq_val,
  685. freq_val2);
  686. return ret;
  687. default:
  688. return -EINVAL;
  689. }
  690. }
  691. return -EINVAL;
  692. }
  693. static int ltr501_read_thresh(struct iio_dev *indio_dev,
  694. const struct iio_chan_spec *chan,
  695. enum iio_event_type type,
  696. enum iio_event_direction dir,
  697. enum iio_event_info info,
  698. int *val, int *val2)
  699. {
  700. struct ltr501_data *data = iio_priv(indio_dev);
  701. int ret, thresh_data;
  702. switch (chan->type) {
  703. case IIO_INTENSITY:
  704. switch (dir) {
  705. case IIO_EV_DIR_RISING:
  706. ret = regmap_bulk_read(data->regmap,
  707. LTR501_ALS_THRESH_UP,
  708. &thresh_data, 2);
  709. if (ret < 0)
  710. return ret;
  711. *val = thresh_data & LTR501_ALS_THRESH_MASK;
  712. return IIO_VAL_INT;
  713. case IIO_EV_DIR_FALLING:
  714. ret = regmap_bulk_read(data->regmap,
  715. LTR501_ALS_THRESH_LOW,
  716. &thresh_data, 2);
  717. if (ret < 0)
  718. return ret;
  719. *val = thresh_data & LTR501_ALS_THRESH_MASK;
  720. return IIO_VAL_INT;
  721. default:
  722. return -EINVAL;
  723. }
  724. case IIO_PROXIMITY:
  725. switch (dir) {
  726. case IIO_EV_DIR_RISING:
  727. ret = regmap_bulk_read(data->regmap,
  728. LTR501_PS_THRESH_UP,
  729. &thresh_data, 2);
  730. if (ret < 0)
  731. return ret;
  732. *val = thresh_data & LTR501_PS_THRESH_MASK;
  733. return IIO_VAL_INT;
  734. case IIO_EV_DIR_FALLING:
  735. ret = regmap_bulk_read(data->regmap,
  736. LTR501_PS_THRESH_LOW,
  737. &thresh_data, 2);
  738. if (ret < 0)
  739. return ret;
  740. *val = thresh_data & LTR501_PS_THRESH_MASK;
  741. return IIO_VAL_INT;
  742. default:
  743. return -EINVAL;
  744. }
  745. default:
  746. return -EINVAL;
  747. }
  748. return -EINVAL;
  749. }
  750. static int ltr501_write_thresh(struct iio_dev *indio_dev,
  751. const struct iio_chan_spec *chan,
  752. enum iio_event_type type,
  753. enum iio_event_direction dir,
  754. enum iio_event_info info,
  755. int val, int val2)
  756. {
  757. struct ltr501_data *data = iio_priv(indio_dev);
  758. int ret;
  759. if (val < 0)
  760. return -EINVAL;
  761. switch (chan->type) {
  762. case IIO_INTENSITY:
  763. if (val > LTR501_ALS_THRESH_MASK)
  764. return -EINVAL;
  765. switch (dir) {
  766. case IIO_EV_DIR_RISING:
  767. mutex_lock(&data->lock_als);
  768. ret = regmap_bulk_write(data->regmap,
  769. LTR501_ALS_THRESH_UP,
  770. &val, 2);
  771. mutex_unlock(&data->lock_als);
  772. return ret;
  773. case IIO_EV_DIR_FALLING:
  774. mutex_lock(&data->lock_als);
  775. ret = regmap_bulk_write(data->regmap,
  776. LTR501_ALS_THRESH_LOW,
  777. &val, 2);
  778. mutex_unlock(&data->lock_als);
  779. return ret;
  780. default:
  781. return -EINVAL;
  782. }
  783. case IIO_PROXIMITY:
  784. if (val > LTR501_PS_THRESH_MASK)
  785. return -EINVAL;
  786. switch (dir) {
  787. case IIO_EV_DIR_RISING:
  788. mutex_lock(&data->lock_ps);
  789. ret = regmap_bulk_write(data->regmap,
  790. LTR501_PS_THRESH_UP,
  791. &val, 2);
  792. mutex_unlock(&data->lock_ps);
  793. return ret;
  794. case IIO_EV_DIR_FALLING:
  795. mutex_lock(&data->lock_ps);
  796. ret = regmap_bulk_write(data->regmap,
  797. LTR501_PS_THRESH_LOW,
  798. &val, 2);
  799. mutex_unlock(&data->lock_ps);
  800. return ret;
  801. default:
  802. return -EINVAL;
  803. }
  804. default:
  805. return -EINVAL;
  806. }
  807. return -EINVAL;
  808. }
  809. static int ltr501_read_event(struct iio_dev *indio_dev,
  810. const struct iio_chan_spec *chan,
  811. enum iio_event_type type,
  812. enum iio_event_direction dir,
  813. enum iio_event_info info,
  814. int *val, int *val2)
  815. {
  816. int ret;
  817. switch (info) {
  818. case IIO_EV_INFO_VALUE:
  819. return ltr501_read_thresh(indio_dev, chan, type, dir,
  820. info, val, val2);
  821. case IIO_EV_INFO_PERIOD:
  822. ret = ltr501_read_intr_prst(iio_priv(indio_dev),
  823. chan->type, val2);
  824. *val = *val2 / 1000000;
  825. *val2 = *val2 % 1000000;
  826. return ret;
  827. default:
  828. return -EINVAL;
  829. }
  830. return -EINVAL;
  831. }
  832. static int ltr501_write_event(struct iio_dev *indio_dev,
  833. const struct iio_chan_spec *chan,
  834. enum iio_event_type type,
  835. enum iio_event_direction dir,
  836. enum iio_event_info info,
  837. int val, int val2)
  838. {
  839. switch (info) {
  840. case IIO_EV_INFO_VALUE:
  841. if (val2 != 0)
  842. return -EINVAL;
  843. return ltr501_write_thresh(indio_dev, chan, type, dir,
  844. info, val, val2);
  845. case IIO_EV_INFO_PERIOD:
  846. return ltr501_write_intr_prst(iio_priv(indio_dev), chan->type,
  847. val, val2);
  848. default:
  849. return -EINVAL;
  850. }
  851. return -EINVAL;
  852. }
  853. static int ltr501_read_event_config(struct iio_dev *indio_dev,
  854. const struct iio_chan_spec *chan,
  855. enum iio_event_type type,
  856. enum iio_event_direction dir)
  857. {
  858. struct ltr501_data *data = iio_priv(indio_dev);
  859. int ret, status;
  860. switch (chan->type) {
  861. case IIO_INTENSITY:
  862. ret = regmap_field_read(data->reg_als_intr, &status);
  863. if (ret < 0)
  864. return ret;
  865. return status;
  866. case IIO_PROXIMITY:
  867. ret = regmap_field_read(data->reg_ps_intr, &status);
  868. if (ret < 0)
  869. return ret;
  870. return status;
  871. default:
  872. return -EINVAL;
  873. }
  874. return -EINVAL;
  875. }
  876. static int ltr501_write_event_config(struct iio_dev *indio_dev,
  877. const struct iio_chan_spec *chan,
  878. enum iio_event_type type,
  879. enum iio_event_direction dir, int state)
  880. {
  881. struct ltr501_data *data = iio_priv(indio_dev);
  882. int ret;
  883. /* only 1 and 0 are valid inputs */
  884. if (state != 1 && state != 0)
  885. return -EINVAL;
  886. switch (chan->type) {
  887. case IIO_INTENSITY:
  888. mutex_lock(&data->lock_als);
  889. ret = regmap_field_write(data->reg_als_intr, state);
  890. mutex_unlock(&data->lock_als);
  891. return ret;
  892. case IIO_PROXIMITY:
  893. mutex_lock(&data->lock_ps);
  894. ret = regmap_field_write(data->reg_ps_intr, state);
  895. mutex_unlock(&data->lock_ps);
  896. return ret;
  897. default:
  898. return -EINVAL;
  899. }
  900. return -EINVAL;
  901. }
  902. static ssize_t ltr501_show_proximity_scale_avail(struct device *dev,
  903. struct device_attribute *attr,
  904. char *buf)
  905. {
  906. struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev));
  907. struct ltr501_chip_info *info = data->chip_info;
  908. ssize_t len = 0;
  909. int i;
  910. for (i = 0; i < info->ps_gain_tbl_size; i++) {
  911. if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN)
  912. continue;
  913. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
  914. info->ps_gain[i].scale,
  915. info->ps_gain[i].uscale);
  916. }
  917. buf[len - 1] = '\n';
  918. return len;
  919. }
  920. static ssize_t ltr501_show_intensity_scale_avail(struct device *dev,
  921. struct device_attribute *attr,
  922. char *buf)
  923. {
  924. struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev));
  925. struct ltr501_chip_info *info = data->chip_info;
  926. ssize_t len = 0;
  927. int i;
  928. for (i = 0; i < info->als_gain_tbl_size; i++) {
  929. if (info->als_gain[i].scale == LTR501_RESERVED_GAIN)
  930. continue;
  931. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
  932. info->als_gain[i].scale,
  933. info->als_gain[i].uscale);
  934. }
  935. buf[len - 1] = '\n';
  936. return len;
  937. }
  938. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4");
  939. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5");
  940. static IIO_DEVICE_ATTR(in_proximity_scale_available, S_IRUGO,
  941. ltr501_show_proximity_scale_avail, NULL, 0);
  942. static IIO_DEVICE_ATTR(in_intensity_scale_available, S_IRUGO,
  943. ltr501_show_intensity_scale_avail, NULL, 0);
  944. static struct attribute *ltr501_attributes[] = {
  945. &iio_dev_attr_in_proximity_scale_available.dev_attr.attr,
  946. &iio_dev_attr_in_intensity_scale_available.dev_attr.attr,
  947. &iio_const_attr_integration_time_available.dev_attr.attr,
  948. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  949. NULL
  950. };
  951. static struct attribute *ltr301_attributes[] = {
  952. &iio_dev_attr_in_intensity_scale_available.dev_attr.attr,
  953. &iio_const_attr_integration_time_available.dev_attr.attr,
  954. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  955. NULL
  956. };
  957. static const struct attribute_group ltr501_attribute_group = {
  958. .attrs = ltr501_attributes,
  959. };
  960. static const struct attribute_group ltr301_attribute_group = {
  961. .attrs = ltr301_attributes,
  962. };
  963. static const struct iio_info ltr501_info_no_irq = {
  964. .read_raw = ltr501_read_raw,
  965. .write_raw = ltr501_write_raw,
  966. .attrs = &ltr501_attribute_group,
  967. .driver_module = THIS_MODULE,
  968. };
  969. static const struct iio_info ltr501_info = {
  970. .read_raw = ltr501_read_raw,
  971. .write_raw = ltr501_write_raw,
  972. .attrs = &ltr501_attribute_group,
  973. .read_event_value = &ltr501_read_event,
  974. .write_event_value = &ltr501_write_event,
  975. .read_event_config = &ltr501_read_event_config,
  976. .write_event_config = &ltr501_write_event_config,
  977. .driver_module = THIS_MODULE,
  978. };
  979. static const struct iio_info ltr301_info_no_irq = {
  980. .read_raw = ltr501_read_raw,
  981. .write_raw = ltr501_write_raw,
  982. .attrs = &ltr301_attribute_group,
  983. .driver_module = THIS_MODULE,
  984. };
  985. static const struct iio_info ltr301_info = {
  986. .read_raw = ltr501_read_raw,
  987. .write_raw = ltr501_write_raw,
  988. .attrs = &ltr301_attribute_group,
  989. .read_event_value = &ltr501_read_event,
  990. .write_event_value = &ltr501_write_event,
  991. .read_event_config = &ltr501_read_event_config,
  992. .write_event_config = &ltr501_write_event_config,
  993. .driver_module = THIS_MODULE,
  994. };
  995. static struct ltr501_chip_info ltr501_chip_info_tbl[] = {
  996. [ltr501] = {
  997. .partid = 0x08,
  998. .als_gain = ltr501_als_gain_tbl,
  999. .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl),
  1000. .ps_gain = ltr501_ps_gain_tbl,
  1001. .ps_gain_tbl_size = ARRAY_SIZE(ltr501_ps_gain_tbl),
  1002. .als_mode_active = BIT(0) | BIT(1),
  1003. .als_gain_mask = BIT(3),
  1004. .als_gain_shift = 3,
  1005. .info = &ltr501_info,
  1006. .info_no_irq = &ltr501_info_no_irq,
  1007. .channels = ltr501_channels,
  1008. .no_channels = ARRAY_SIZE(ltr501_channels),
  1009. },
  1010. [ltr559] = {
  1011. .partid = 0x09,
  1012. .als_gain = ltr559_als_gain_tbl,
  1013. .als_gain_tbl_size = ARRAY_SIZE(ltr559_als_gain_tbl),
  1014. .ps_gain = ltr559_ps_gain_tbl,
  1015. .ps_gain_tbl_size = ARRAY_SIZE(ltr559_ps_gain_tbl),
  1016. .als_mode_active = BIT(1),
  1017. .als_gain_mask = BIT(2) | BIT(3) | BIT(4),
  1018. .als_gain_shift = 2,
  1019. .info = &ltr501_info,
  1020. .info_no_irq = &ltr501_info_no_irq,
  1021. .channels = ltr501_channels,
  1022. .no_channels = ARRAY_SIZE(ltr501_channels),
  1023. },
  1024. [ltr301] = {
  1025. .partid = 0x08,
  1026. .als_gain = ltr501_als_gain_tbl,
  1027. .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl),
  1028. .als_mode_active = BIT(0) | BIT(1),
  1029. .als_gain_mask = BIT(3),
  1030. .als_gain_shift = 3,
  1031. .info = &ltr301_info,
  1032. .info_no_irq = &ltr301_info_no_irq,
  1033. .channels = ltr301_channels,
  1034. .no_channels = ARRAY_SIZE(ltr301_channels),
  1035. },
  1036. };
  1037. static int ltr501_write_contr(struct ltr501_data *data, u8 als_val, u8 ps_val)
  1038. {
  1039. int ret;
  1040. ret = regmap_write(data->regmap, LTR501_ALS_CONTR, als_val);
  1041. if (ret < 0)
  1042. return ret;
  1043. return regmap_write(data->regmap, LTR501_PS_CONTR, ps_val);
  1044. }
  1045. static irqreturn_t ltr501_trigger_handler(int irq, void *p)
  1046. {
  1047. struct iio_poll_func *pf = p;
  1048. struct iio_dev *indio_dev = pf->indio_dev;
  1049. struct ltr501_data *data = iio_priv(indio_dev);
  1050. u16 buf[8];
  1051. __le16 als_buf[2];
  1052. u8 mask = 0;
  1053. int j = 0;
  1054. int ret, psdata;
  1055. memset(buf, 0, sizeof(buf));
  1056. /* figure out which data needs to be ready */
  1057. if (test_bit(0, indio_dev->active_scan_mask) ||
  1058. test_bit(1, indio_dev->active_scan_mask))
  1059. mask |= LTR501_STATUS_ALS_RDY;
  1060. if (test_bit(2, indio_dev->active_scan_mask))
  1061. mask |= LTR501_STATUS_PS_RDY;
  1062. ret = ltr501_drdy(data, mask);
  1063. if (ret < 0)
  1064. goto done;
  1065. if (mask & LTR501_STATUS_ALS_RDY) {
  1066. ret = regmap_bulk_read(data->regmap, LTR501_ALS_DATA1,
  1067. (u8 *)als_buf, sizeof(als_buf));
  1068. if (ret < 0)
  1069. return ret;
  1070. if (test_bit(0, indio_dev->active_scan_mask))
  1071. buf[j++] = le16_to_cpu(als_buf[1]);
  1072. if (test_bit(1, indio_dev->active_scan_mask))
  1073. buf[j++] = le16_to_cpu(als_buf[0]);
  1074. }
  1075. if (mask & LTR501_STATUS_PS_RDY) {
  1076. ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA,
  1077. &psdata, 2);
  1078. if (ret < 0)
  1079. goto done;
  1080. buf[j++] = psdata & LTR501_PS_DATA_MASK;
  1081. }
  1082. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  1083. iio_get_time_ns(indio_dev));
  1084. done:
  1085. iio_trigger_notify_done(indio_dev->trig);
  1086. return IRQ_HANDLED;
  1087. }
  1088. static irqreturn_t ltr501_interrupt_handler(int irq, void *private)
  1089. {
  1090. struct iio_dev *indio_dev = private;
  1091. struct ltr501_data *data = iio_priv(indio_dev);
  1092. int ret, status;
  1093. ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status);
  1094. if (ret < 0) {
  1095. dev_err(&data->client->dev,
  1096. "irq read int reg failed\n");
  1097. return IRQ_HANDLED;
  1098. }
  1099. if (status & LTR501_STATUS_ALS_INTR)
  1100. iio_push_event(indio_dev,
  1101. IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
  1102. IIO_EV_TYPE_THRESH,
  1103. IIO_EV_DIR_EITHER),
  1104. iio_get_time_ns(indio_dev));
  1105. if (status & LTR501_STATUS_PS_INTR)
  1106. iio_push_event(indio_dev,
  1107. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
  1108. IIO_EV_TYPE_THRESH,
  1109. IIO_EV_DIR_EITHER),
  1110. iio_get_time_ns(indio_dev));
  1111. return IRQ_HANDLED;
  1112. }
  1113. static int ltr501_init(struct ltr501_data *data)
  1114. {
  1115. int ret, status;
  1116. ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status);
  1117. if (ret < 0)
  1118. return ret;
  1119. data->als_contr = status | data->chip_info->als_mode_active;
  1120. ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status);
  1121. if (ret < 0)
  1122. return ret;
  1123. data->ps_contr = status | LTR501_CONTR_ACTIVE;
  1124. ret = ltr501_read_intr_prst(data, IIO_INTENSITY, &data->als_period);
  1125. if (ret < 0)
  1126. return ret;
  1127. ret = ltr501_read_intr_prst(data, IIO_PROXIMITY, &data->ps_period);
  1128. if (ret < 0)
  1129. return ret;
  1130. return ltr501_write_contr(data, data->als_contr, data->ps_contr);
  1131. }
  1132. static bool ltr501_is_volatile_reg(struct device *dev, unsigned int reg)
  1133. {
  1134. switch (reg) {
  1135. case LTR501_ALS_DATA1:
  1136. case LTR501_ALS_DATA0:
  1137. case LTR501_ALS_PS_STATUS:
  1138. case LTR501_PS_DATA:
  1139. return true;
  1140. default:
  1141. return false;
  1142. }
  1143. }
  1144. static struct regmap_config ltr501_regmap_config = {
  1145. .name = LTR501_REGMAP_NAME,
  1146. .reg_bits = 8,
  1147. .val_bits = 8,
  1148. .max_register = LTR501_MAX_REG,
  1149. .cache_type = REGCACHE_RBTREE,
  1150. .volatile_reg = ltr501_is_volatile_reg,
  1151. };
  1152. static int ltr501_powerdown(struct ltr501_data *data)
  1153. {
  1154. return ltr501_write_contr(data, data->als_contr &
  1155. ~data->chip_info->als_mode_active,
  1156. data->ps_contr & ~LTR501_CONTR_ACTIVE);
  1157. }
  1158. static const char *ltr501_match_acpi_device(struct device *dev, int *chip_idx)
  1159. {
  1160. const struct acpi_device_id *id;
  1161. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  1162. if (!id)
  1163. return NULL;
  1164. *chip_idx = id->driver_data;
  1165. return dev_name(dev);
  1166. }
  1167. static int ltr501_probe(struct i2c_client *client,
  1168. const struct i2c_device_id *id)
  1169. {
  1170. struct ltr501_data *data;
  1171. struct iio_dev *indio_dev;
  1172. struct regmap *regmap;
  1173. int ret, partid, chip_idx = 0;
  1174. const char *name = NULL;
  1175. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  1176. if (!indio_dev)
  1177. return -ENOMEM;
  1178. regmap = devm_regmap_init_i2c(client, &ltr501_regmap_config);
  1179. if (IS_ERR(regmap)) {
  1180. dev_err(&client->dev, "Regmap initialization failed.\n");
  1181. return PTR_ERR(regmap);
  1182. }
  1183. data = iio_priv(indio_dev);
  1184. i2c_set_clientdata(client, indio_dev);
  1185. data->client = client;
  1186. data->regmap = regmap;
  1187. mutex_init(&data->lock_als);
  1188. mutex_init(&data->lock_ps);
  1189. data->reg_it = devm_regmap_field_alloc(&client->dev, regmap,
  1190. reg_field_it);
  1191. if (IS_ERR(data->reg_it)) {
  1192. dev_err(&client->dev, "Integ time reg field init failed.\n");
  1193. return PTR_ERR(data->reg_it);
  1194. }
  1195. data->reg_als_intr = devm_regmap_field_alloc(&client->dev, regmap,
  1196. reg_field_als_intr);
  1197. if (IS_ERR(data->reg_als_intr)) {
  1198. dev_err(&client->dev, "ALS intr mode reg field init failed\n");
  1199. return PTR_ERR(data->reg_als_intr);
  1200. }
  1201. data->reg_ps_intr = devm_regmap_field_alloc(&client->dev, regmap,
  1202. reg_field_ps_intr);
  1203. if (IS_ERR(data->reg_ps_intr)) {
  1204. dev_err(&client->dev, "PS intr mode reg field init failed.\n");
  1205. return PTR_ERR(data->reg_ps_intr);
  1206. }
  1207. data->reg_als_rate = devm_regmap_field_alloc(&client->dev, regmap,
  1208. reg_field_als_rate);
  1209. if (IS_ERR(data->reg_als_rate)) {
  1210. dev_err(&client->dev, "ALS samp rate field init failed.\n");
  1211. return PTR_ERR(data->reg_als_rate);
  1212. }
  1213. data->reg_ps_rate = devm_regmap_field_alloc(&client->dev, regmap,
  1214. reg_field_ps_rate);
  1215. if (IS_ERR(data->reg_ps_rate)) {
  1216. dev_err(&client->dev, "PS samp rate field init failed.\n");
  1217. return PTR_ERR(data->reg_ps_rate);
  1218. }
  1219. data->reg_als_prst = devm_regmap_field_alloc(&client->dev, regmap,
  1220. reg_field_als_prst);
  1221. if (IS_ERR(data->reg_als_prst)) {
  1222. dev_err(&client->dev, "ALS prst reg field init failed\n");
  1223. return PTR_ERR(data->reg_als_prst);
  1224. }
  1225. data->reg_ps_prst = devm_regmap_field_alloc(&client->dev, regmap,
  1226. reg_field_ps_prst);
  1227. if (IS_ERR(data->reg_ps_prst)) {
  1228. dev_err(&client->dev, "PS prst reg field init failed.\n");
  1229. return PTR_ERR(data->reg_ps_prst);
  1230. }
  1231. ret = regmap_read(data->regmap, LTR501_PART_ID, &partid);
  1232. if (ret < 0)
  1233. return ret;
  1234. if (id) {
  1235. name = id->name;
  1236. chip_idx = id->driver_data;
  1237. } else if (ACPI_HANDLE(&client->dev)) {
  1238. name = ltr501_match_acpi_device(&client->dev, &chip_idx);
  1239. } else {
  1240. return -ENODEV;
  1241. }
  1242. data->chip_info = &ltr501_chip_info_tbl[chip_idx];
  1243. if ((partid >> 4) != data->chip_info->partid)
  1244. return -ENODEV;
  1245. indio_dev->dev.parent = &client->dev;
  1246. indio_dev->info = data->chip_info->info;
  1247. indio_dev->channels = data->chip_info->channels;
  1248. indio_dev->num_channels = data->chip_info->no_channels;
  1249. indio_dev->name = name;
  1250. indio_dev->modes = INDIO_DIRECT_MODE;
  1251. ret = ltr501_init(data);
  1252. if (ret < 0)
  1253. return ret;
  1254. if (client->irq > 0) {
  1255. ret = devm_request_threaded_irq(&client->dev, client->irq,
  1256. NULL, ltr501_interrupt_handler,
  1257. IRQF_TRIGGER_FALLING |
  1258. IRQF_ONESHOT,
  1259. "ltr501_thresh_event",
  1260. indio_dev);
  1261. if (ret) {
  1262. dev_err(&client->dev, "request irq (%d) failed\n",
  1263. client->irq);
  1264. return ret;
  1265. }
  1266. } else {
  1267. indio_dev->info = data->chip_info->info_no_irq;
  1268. }
  1269. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  1270. ltr501_trigger_handler, NULL);
  1271. if (ret)
  1272. goto powerdown_on_error;
  1273. ret = iio_device_register(indio_dev);
  1274. if (ret)
  1275. goto error_unreg_buffer;
  1276. return 0;
  1277. error_unreg_buffer:
  1278. iio_triggered_buffer_cleanup(indio_dev);
  1279. powerdown_on_error:
  1280. ltr501_powerdown(data);
  1281. return ret;
  1282. }
  1283. static int ltr501_remove(struct i2c_client *client)
  1284. {
  1285. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1286. iio_device_unregister(indio_dev);
  1287. iio_triggered_buffer_cleanup(indio_dev);
  1288. ltr501_powerdown(iio_priv(indio_dev));
  1289. return 0;
  1290. }
  1291. #ifdef CONFIG_PM_SLEEP
  1292. static int ltr501_suspend(struct device *dev)
  1293. {
  1294. struct ltr501_data *data = iio_priv(i2c_get_clientdata(
  1295. to_i2c_client(dev)));
  1296. return ltr501_powerdown(data);
  1297. }
  1298. static int ltr501_resume(struct device *dev)
  1299. {
  1300. struct ltr501_data *data = iio_priv(i2c_get_clientdata(
  1301. to_i2c_client(dev)));
  1302. return ltr501_write_contr(data, data->als_contr,
  1303. data->ps_contr);
  1304. }
  1305. #endif
  1306. static SIMPLE_DEV_PM_OPS(ltr501_pm_ops, ltr501_suspend, ltr501_resume);
  1307. static const struct acpi_device_id ltr_acpi_match[] = {
  1308. {"LTER0501", ltr501},
  1309. {"LTER0559", ltr559},
  1310. {"LTER0301", ltr301},
  1311. { },
  1312. };
  1313. MODULE_DEVICE_TABLE(acpi, ltr_acpi_match);
  1314. static const struct i2c_device_id ltr501_id[] = {
  1315. { "ltr501", ltr501},
  1316. { "ltr559", ltr559},
  1317. { "ltr301", ltr301},
  1318. { }
  1319. };
  1320. MODULE_DEVICE_TABLE(i2c, ltr501_id);
  1321. static struct i2c_driver ltr501_driver = {
  1322. .driver = {
  1323. .name = LTR501_DRV_NAME,
  1324. .pm = &ltr501_pm_ops,
  1325. .acpi_match_table = ACPI_PTR(ltr_acpi_match),
  1326. },
  1327. .probe = ltr501_probe,
  1328. .remove = ltr501_remove,
  1329. .id_table = ltr501_id,
  1330. };
  1331. module_i2c_driver(ltr501_driver);
  1332. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  1333. MODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver");
  1334. MODULE_LICENSE("GPL");