lv0104cs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * lv0104cs.c: LV0104CS Ambient Light Sensor Driver
  4. *
  5. * Copyright (C) 2018
  6. * Author: Jeff LaBundy <jeff@labundy.com>
  7. *
  8. * 7-bit I2C slave address: 0x13
  9. *
  10. * Link to data sheet: http://www.onsemi.com/pub/Collateral/LV0104CS-D.PDF
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #define LV0104CS_REGVAL_MEASURE 0xE0
  21. #define LV0104CS_REGVAL_SLEEP 0x00
  22. #define LV0104CS_SCALE_0_25X 0
  23. #define LV0104CS_SCALE_1X 1
  24. #define LV0104CS_SCALE_2X 2
  25. #define LV0104CS_SCALE_8X 3
  26. #define LV0104CS_SCALE_SHIFT 3
  27. #define LV0104CS_INTEG_12_5MS 0
  28. #define LV0104CS_INTEG_100MS 1
  29. #define LV0104CS_INTEG_200MS 2
  30. #define LV0104CS_INTEG_SHIFT 1
  31. #define LV0104CS_CALIBSCALE_UNITY 31
  32. struct lv0104cs_private {
  33. struct i2c_client *client;
  34. struct mutex lock;
  35. u8 calibscale;
  36. u8 scale;
  37. u8 int_time;
  38. };
  39. struct lv0104cs_mapping {
  40. int val;
  41. int val2;
  42. u8 regval;
  43. };
  44. static const struct lv0104cs_mapping lv0104cs_calibscales[] = {
  45. { 0, 666666, 0x81 },
  46. { 0, 800000, 0x82 },
  47. { 0, 857142, 0x83 },
  48. { 0, 888888, 0x84 },
  49. { 0, 909090, 0x85 },
  50. { 0, 923076, 0x86 },
  51. { 0, 933333, 0x87 },
  52. { 0, 941176, 0x88 },
  53. { 0, 947368, 0x89 },
  54. { 0, 952380, 0x8A },
  55. { 0, 956521, 0x8B },
  56. { 0, 960000, 0x8C },
  57. { 0, 962962, 0x8D },
  58. { 0, 965517, 0x8E },
  59. { 0, 967741, 0x8F },
  60. { 0, 969696, 0x90 },
  61. { 0, 971428, 0x91 },
  62. { 0, 972972, 0x92 },
  63. { 0, 974358, 0x93 },
  64. { 0, 975609, 0x94 },
  65. { 0, 976744, 0x95 },
  66. { 0, 977777, 0x96 },
  67. { 0, 978723, 0x97 },
  68. { 0, 979591, 0x98 },
  69. { 0, 980392, 0x99 },
  70. { 0, 981132, 0x9A },
  71. { 0, 981818, 0x9B },
  72. { 0, 982456, 0x9C },
  73. { 0, 983050, 0x9D },
  74. { 0, 983606, 0x9E },
  75. { 0, 984126, 0x9F },
  76. { 1, 0, 0x80 },
  77. { 1, 16129, 0xBF },
  78. { 1, 16666, 0xBE },
  79. { 1, 17241, 0xBD },
  80. { 1, 17857, 0xBC },
  81. { 1, 18518, 0xBB },
  82. { 1, 19230, 0xBA },
  83. { 1, 20000, 0xB9 },
  84. { 1, 20833, 0xB8 },
  85. { 1, 21739, 0xB7 },
  86. { 1, 22727, 0xB6 },
  87. { 1, 23809, 0xB5 },
  88. { 1, 24999, 0xB4 },
  89. { 1, 26315, 0xB3 },
  90. { 1, 27777, 0xB2 },
  91. { 1, 29411, 0xB1 },
  92. { 1, 31250, 0xB0 },
  93. { 1, 33333, 0xAF },
  94. { 1, 35714, 0xAE },
  95. { 1, 38461, 0xAD },
  96. { 1, 41666, 0xAC },
  97. { 1, 45454, 0xAB },
  98. { 1, 50000, 0xAA },
  99. { 1, 55555, 0xA9 },
  100. { 1, 62500, 0xA8 },
  101. { 1, 71428, 0xA7 },
  102. { 1, 83333, 0xA6 },
  103. { 1, 100000, 0xA5 },
  104. { 1, 125000, 0xA4 },
  105. { 1, 166666, 0xA3 },
  106. { 1, 250000, 0xA2 },
  107. { 1, 500000, 0xA1 },
  108. };
  109. static const struct lv0104cs_mapping lv0104cs_scales[] = {
  110. { 0, 250000, LV0104CS_SCALE_0_25X << LV0104CS_SCALE_SHIFT },
  111. { 1, 0, LV0104CS_SCALE_1X << LV0104CS_SCALE_SHIFT },
  112. { 2, 0, LV0104CS_SCALE_2X << LV0104CS_SCALE_SHIFT },
  113. { 8, 0, LV0104CS_SCALE_8X << LV0104CS_SCALE_SHIFT },
  114. };
  115. static const struct lv0104cs_mapping lv0104cs_int_times[] = {
  116. { 0, 12500, LV0104CS_INTEG_12_5MS << LV0104CS_INTEG_SHIFT },
  117. { 0, 100000, LV0104CS_INTEG_100MS << LV0104CS_INTEG_SHIFT },
  118. { 0, 200000, LV0104CS_INTEG_200MS << LV0104CS_INTEG_SHIFT },
  119. };
  120. static int lv0104cs_write_reg(struct i2c_client *client, u8 regval)
  121. {
  122. int ret;
  123. ret = i2c_master_send(client, (char *)&regval, sizeof(regval));
  124. if (ret < 0)
  125. return ret;
  126. if (ret != sizeof(regval))
  127. return -EIO;
  128. return 0;
  129. }
  130. static int lv0104cs_read_adc(struct i2c_client *client, u16 *adc_output)
  131. {
  132. __be16 regval;
  133. int ret;
  134. ret = i2c_master_recv(client, (char *)&regval, sizeof(regval));
  135. if (ret < 0)
  136. return ret;
  137. if (ret != sizeof(regval))
  138. return -EIO;
  139. *adc_output = be16_to_cpu(regval);
  140. return 0;
  141. }
  142. static int lv0104cs_get_lux(struct lv0104cs_private *lv0104cs,
  143. int *val, int *val2)
  144. {
  145. u8 regval = LV0104CS_REGVAL_MEASURE;
  146. u16 adc_output;
  147. int ret;
  148. regval |= lv0104cs_scales[lv0104cs->scale].regval;
  149. regval |= lv0104cs_int_times[lv0104cs->int_time].regval;
  150. ret = lv0104cs_write_reg(lv0104cs->client, regval);
  151. if (ret)
  152. return ret;
  153. /* wait for integration time to pass (with margin) */
  154. switch (lv0104cs->int_time) {
  155. case LV0104CS_INTEG_12_5MS:
  156. msleep(50);
  157. break;
  158. case LV0104CS_INTEG_100MS:
  159. msleep(150);
  160. break;
  161. case LV0104CS_INTEG_200MS:
  162. msleep(250);
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. ret = lv0104cs_read_adc(lv0104cs->client, &adc_output);
  168. if (ret)
  169. return ret;
  170. ret = lv0104cs_write_reg(lv0104cs->client, LV0104CS_REGVAL_SLEEP);
  171. if (ret)
  172. return ret;
  173. /* convert ADC output to lux */
  174. switch (lv0104cs->scale) {
  175. case LV0104CS_SCALE_0_25X:
  176. *val = adc_output * 4;
  177. *val2 = 0;
  178. return 0;
  179. case LV0104CS_SCALE_1X:
  180. *val = adc_output;
  181. *val2 = 0;
  182. return 0;
  183. case LV0104CS_SCALE_2X:
  184. *val = adc_output / 2;
  185. *val2 = (adc_output % 2) * 500000;
  186. return 0;
  187. case LV0104CS_SCALE_8X:
  188. *val = adc_output / 8;
  189. *val2 = (adc_output % 8) * 125000;
  190. return 0;
  191. default:
  192. return -EINVAL;
  193. }
  194. }
  195. static int lv0104cs_read_raw(struct iio_dev *indio_dev,
  196. struct iio_chan_spec const *chan,
  197. int *val, int *val2, long mask)
  198. {
  199. struct lv0104cs_private *lv0104cs = iio_priv(indio_dev);
  200. int ret;
  201. if (chan->type != IIO_LIGHT)
  202. return -EINVAL;
  203. mutex_lock(&lv0104cs->lock);
  204. switch (mask) {
  205. case IIO_CHAN_INFO_PROCESSED:
  206. ret = lv0104cs_get_lux(lv0104cs, val, val2);
  207. if (ret)
  208. goto err_mutex;
  209. ret = IIO_VAL_INT_PLUS_MICRO;
  210. break;
  211. case IIO_CHAN_INFO_CALIBSCALE:
  212. *val = lv0104cs_calibscales[lv0104cs->calibscale].val;
  213. *val2 = lv0104cs_calibscales[lv0104cs->calibscale].val2;
  214. ret = IIO_VAL_INT_PLUS_MICRO;
  215. break;
  216. case IIO_CHAN_INFO_SCALE:
  217. *val = lv0104cs_scales[lv0104cs->scale].val;
  218. *val2 = lv0104cs_scales[lv0104cs->scale].val2;
  219. ret = IIO_VAL_INT_PLUS_MICRO;
  220. break;
  221. case IIO_CHAN_INFO_INT_TIME:
  222. *val = lv0104cs_int_times[lv0104cs->int_time].val;
  223. *val2 = lv0104cs_int_times[lv0104cs->int_time].val2;
  224. ret = IIO_VAL_INT_PLUS_MICRO;
  225. break;
  226. default:
  227. ret = -EINVAL;
  228. }
  229. err_mutex:
  230. mutex_unlock(&lv0104cs->lock);
  231. return ret;
  232. }
  233. static int lv0104cs_set_calibscale(struct lv0104cs_private *lv0104cs,
  234. int val, int val2)
  235. {
  236. int calibscale = val * 1000000 + val2;
  237. int floor, ceil, mid;
  238. int ret, i, index;
  239. /* round to nearest quantized calibscale (sensitivity) */
  240. for (i = 0; i < ARRAY_SIZE(lv0104cs_calibscales) - 1; i++) {
  241. floor = lv0104cs_calibscales[i].val * 1000000
  242. + lv0104cs_calibscales[i].val2;
  243. ceil = lv0104cs_calibscales[i + 1].val * 1000000
  244. + lv0104cs_calibscales[i + 1].val2;
  245. mid = (floor + ceil) / 2;
  246. /* round down */
  247. if (calibscale >= floor && calibscale < mid) {
  248. index = i;
  249. break;
  250. }
  251. /* round up */
  252. if (calibscale >= mid && calibscale <= ceil) {
  253. index = i + 1;
  254. break;
  255. }
  256. }
  257. if (i == ARRAY_SIZE(lv0104cs_calibscales) - 1)
  258. return -EINVAL;
  259. mutex_lock(&lv0104cs->lock);
  260. /* set calibscale (sensitivity) */
  261. ret = lv0104cs_write_reg(lv0104cs->client,
  262. lv0104cs_calibscales[index].regval);
  263. if (ret)
  264. goto err_mutex;
  265. lv0104cs->calibscale = index;
  266. err_mutex:
  267. mutex_unlock(&lv0104cs->lock);
  268. return ret;
  269. }
  270. static int lv0104cs_set_scale(struct lv0104cs_private *lv0104cs,
  271. int val, int val2)
  272. {
  273. int i;
  274. /* hard matching */
  275. for (i = 0; i < ARRAY_SIZE(lv0104cs_scales); i++) {
  276. if (val != lv0104cs_scales[i].val)
  277. continue;
  278. if (val2 == lv0104cs_scales[i].val2)
  279. break;
  280. }
  281. if (i == ARRAY_SIZE(lv0104cs_scales))
  282. return -EINVAL;
  283. mutex_lock(&lv0104cs->lock);
  284. lv0104cs->scale = i;
  285. mutex_unlock(&lv0104cs->lock);
  286. return 0;
  287. }
  288. static int lv0104cs_set_int_time(struct lv0104cs_private *lv0104cs,
  289. int val, int val2)
  290. {
  291. int i;
  292. /* hard matching */
  293. for (i = 0; i < ARRAY_SIZE(lv0104cs_int_times); i++) {
  294. if (val != lv0104cs_int_times[i].val)
  295. continue;
  296. if (val2 == lv0104cs_int_times[i].val2)
  297. break;
  298. }
  299. if (i == ARRAY_SIZE(lv0104cs_int_times))
  300. return -EINVAL;
  301. mutex_lock(&lv0104cs->lock);
  302. lv0104cs->int_time = i;
  303. mutex_unlock(&lv0104cs->lock);
  304. return 0;
  305. }
  306. static int lv0104cs_write_raw(struct iio_dev *indio_dev,
  307. struct iio_chan_spec const *chan,
  308. int val, int val2, long mask)
  309. {
  310. struct lv0104cs_private *lv0104cs = iio_priv(indio_dev);
  311. if (chan->type != IIO_LIGHT)
  312. return -EINVAL;
  313. switch (mask) {
  314. case IIO_CHAN_INFO_CALIBSCALE:
  315. return lv0104cs_set_calibscale(lv0104cs, val, val2);
  316. case IIO_CHAN_INFO_SCALE:
  317. return lv0104cs_set_scale(lv0104cs, val, val2);
  318. case IIO_CHAN_INFO_INT_TIME:
  319. return lv0104cs_set_int_time(lv0104cs, val, val2);
  320. default:
  321. return -EINVAL;
  322. }
  323. }
  324. static ssize_t lv0104cs_show_calibscale_avail(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. ssize_t len = 0;
  328. int i;
  329. for (i = 0; i < ARRAY_SIZE(lv0104cs_calibscales); i++) {
  330. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
  331. lv0104cs_calibscales[i].val,
  332. lv0104cs_calibscales[i].val2);
  333. }
  334. buf[len - 1] = '\n';
  335. return len;
  336. }
  337. static ssize_t lv0104cs_show_scale_avail(struct device *dev,
  338. struct device_attribute *attr, char *buf)
  339. {
  340. ssize_t len = 0;
  341. int i;
  342. for (i = 0; i < ARRAY_SIZE(lv0104cs_scales); i++) {
  343. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
  344. lv0104cs_scales[i].val,
  345. lv0104cs_scales[i].val2);
  346. }
  347. buf[len - 1] = '\n';
  348. return len;
  349. }
  350. static ssize_t lv0104cs_show_int_time_avail(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. ssize_t len = 0;
  354. int i;
  355. for (i = 0; i < ARRAY_SIZE(lv0104cs_int_times); i++) {
  356. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
  357. lv0104cs_int_times[i].val,
  358. lv0104cs_int_times[i].val2);
  359. }
  360. buf[len - 1] = '\n';
  361. return len;
  362. }
  363. static IIO_DEVICE_ATTR(calibscale_available, 0444,
  364. lv0104cs_show_calibscale_avail, NULL, 0);
  365. static IIO_DEVICE_ATTR(scale_available, 0444,
  366. lv0104cs_show_scale_avail, NULL, 0);
  367. static IIO_DEV_ATTR_INT_TIME_AVAIL(lv0104cs_show_int_time_avail);
  368. static struct attribute *lv0104cs_attributes[] = {
  369. &iio_dev_attr_calibscale_available.dev_attr.attr,
  370. &iio_dev_attr_scale_available.dev_attr.attr,
  371. &iio_dev_attr_integration_time_available.dev_attr.attr,
  372. NULL
  373. };
  374. static const struct attribute_group lv0104cs_attribute_group = {
  375. .attrs = lv0104cs_attributes,
  376. };
  377. static const struct iio_info lv0104cs_info = {
  378. .attrs = &lv0104cs_attribute_group,
  379. .read_raw = &lv0104cs_read_raw,
  380. .write_raw = &lv0104cs_write_raw,
  381. };
  382. static const struct iio_chan_spec lv0104cs_channels[] = {
  383. {
  384. .type = IIO_LIGHT,
  385. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  386. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  387. BIT(IIO_CHAN_INFO_SCALE) |
  388. BIT(IIO_CHAN_INFO_INT_TIME),
  389. },
  390. };
  391. static int lv0104cs_probe(struct i2c_client *client,
  392. const struct i2c_device_id *id)
  393. {
  394. struct iio_dev *indio_dev;
  395. struct lv0104cs_private *lv0104cs;
  396. int ret;
  397. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*lv0104cs));
  398. if (!indio_dev)
  399. return -ENOMEM;
  400. lv0104cs = iio_priv(indio_dev);
  401. i2c_set_clientdata(client, lv0104cs);
  402. lv0104cs->client = client;
  403. mutex_init(&lv0104cs->lock);
  404. lv0104cs->calibscale = LV0104CS_CALIBSCALE_UNITY;
  405. lv0104cs->scale = LV0104CS_SCALE_1X;
  406. lv0104cs->int_time = LV0104CS_INTEG_200MS;
  407. ret = lv0104cs_write_reg(lv0104cs->client,
  408. lv0104cs_calibscales[LV0104CS_CALIBSCALE_UNITY].regval);
  409. if (ret)
  410. return ret;
  411. indio_dev->modes = INDIO_DIRECT_MODE;
  412. indio_dev->dev.parent = &client->dev;
  413. indio_dev->channels = lv0104cs_channels;
  414. indio_dev->num_channels = ARRAY_SIZE(lv0104cs_channels);
  415. indio_dev->name = client->name;
  416. indio_dev->info = &lv0104cs_info;
  417. return devm_iio_device_register(&client->dev, indio_dev);
  418. }
  419. static const struct i2c_device_id lv0104cs_id[] = {
  420. { "lv0104cs", 0 },
  421. { }
  422. };
  423. MODULE_DEVICE_TABLE(i2c, lv0104cs_id);
  424. static struct i2c_driver lv0104cs_i2c_driver = {
  425. .driver = {
  426. .name = "lv0104cs",
  427. },
  428. .id_table = lv0104cs_id,
  429. .probe = lv0104cs_probe,
  430. };
  431. module_i2c_driver(lv0104cs_i2c_driver);
  432. MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
  433. MODULE_DESCRIPTION("LV0104CS Ambient Light Sensor Driver");
  434. MODULE_LICENSE("GPL");