zopt2201.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * zopt2201.c - Support for IDT ZOPT2201 ambient light and UV B sensor
  3. *
  4. * Copyright 2017 Peter Meerwald-Stadler <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. * Datasheet: https://www.idt.com/document/dst/zopt2201-datasheet
  11. * 7-bit I2C slave addresses 0x53 (default) or 0x52 (programmed)
  12. *
  13. * TODO: interrupt support, ALS/UVB raw mode
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/mutex.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #define ZOPT2201_DRV_NAME "zopt2201"
  23. /* Registers */
  24. #define ZOPT2201_MAIN_CTRL 0x00
  25. #define ZOPT2201_LS_MEAS_RATE 0x04
  26. #define ZOPT2201_LS_GAIN 0x05
  27. #define ZOPT2201_PART_ID 0x06
  28. #define ZOPT2201_MAIN_STATUS 0x07
  29. #define ZOPT2201_ALS_DATA 0x0d /* LSB first, 13 to 20 bits */
  30. #define ZOPT2201_UVB_DATA 0x10 /* LSB first, 13 to 20 bits */
  31. #define ZOPT2201_UV_COMP_DATA 0x13 /* LSB first, 13 to 20 bits */
  32. #define ZOPT2201_COMP_DATA 0x16 /* LSB first, 13 to 20 bits */
  33. #define ZOPT2201_INT_CFG 0x19
  34. #define ZOPT2201_INT_PST 0x1a
  35. #define ZOPT2201_MAIN_CTRL_LS_MODE BIT(3) /* 0 .. ALS, 1 .. UV B */
  36. #define ZOPT2201_MAIN_CTRL_LS_EN BIT(1)
  37. /* Values for ZOPT2201_LS_MEAS_RATE resolution / bit width */
  38. #define ZOPT2201_MEAS_RES_20BIT 0 /* takes 400 ms */
  39. #define ZOPT2201_MEAS_RES_19BIT 1 /* takes 200 ms */
  40. #define ZOPT2201_MEAS_RES_18BIT 2 /* takes 100 ms, default */
  41. #define ZOPT2201_MEAS_RES_17BIT 3 /* takes 50 ms */
  42. #define ZOPT2201_MEAS_RES_16BIT 4 /* takes 25 ms */
  43. #define ZOPT2201_MEAS_RES_13BIT 5 /* takes 3.125 ms */
  44. #define ZOPT2201_MEAS_RES_SHIFT 4
  45. /* Values for ZOPT2201_LS_MEAS_RATE measurement rate */
  46. #define ZOPT2201_MEAS_FREQ_25MS 0
  47. #define ZOPT2201_MEAS_FREQ_50MS 1
  48. #define ZOPT2201_MEAS_FREQ_100MS 2 /* default */
  49. #define ZOPT2201_MEAS_FREQ_200MS 3
  50. #define ZOPT2201_MEAS_FREQ_500MS 4
  51. #define ZOPT2201_MEAS_FREQ_1000MS 5
  52. #define ZOPT2201_MEAS_FREQ_2000MS 6
  53. /* Values for ZOPT2201_LS_GAIN */
  54. #define ZOPT2201_LS_GAIN_1 0
  55. #define ZOPT2201_LS_GAIN_3 1
  56. #define ZOPT2201_LS_GAIN_6 2
  57. #define ZOPT2201_LS_GAIN_9 3
  58. #define ZOPT2201_LS_GAIN_18 4
  59. /* Values for ZOPT2201_MAIN_STATUS */
  60. #define ZOPT2201_MAIN_STATUS_POWERON BIT(5)
  61. #define ZOPT2201_MAIN_STATUS_INT BIT(4)
  62. #define ZOPT2201_MAIN_STATUS_DRDY BIT(3)
  63. #define ZOPT2201_PART_NUMBER 0xb2
  64. struct zopt2201_data {
  65. struct i2c_client *client;
  66. struct mutex lock;
  67. u8 gain;
  68. u8 res;
  69. u8 rate;
  70. };
  71. static const struct {
  72. unsigned int gain; /* gain factor */
  73. unsigned int scale; /* micro lux per count */
  74. } zopt2201_gain_als[] = {
  75. { 1, 19200000 },
  76. { 3, 6400000 },
  77. { 6, 3200000 },
  78. { 9, 2133333 },
  79. { 18, 1066666 },
  80. };
  81. static const struct {
  82. unsigned int gain; /* gain factor */
  83. unsigned int scale; /* micro W/m2 per count */
  84. } zopt2201_gain_uvb[] = {
  85. { 1, 460800 },
  86. { 3, 153600 },
  87. { 6, 76800 },
  88. { 9, 51200 },
  89. { 18, 25600 },
  90. };
  91. static const struct {
  92. unsigned int bits; /* sensor resolution in bits */
  93. unsigned long us; /* measurement time in micro seconds */
  94. } zopt2201_resolution[] = {
  95. { 20, 400000 },
  96. { 19, 200000 },
  97. { 18, 100000 },
  98. { 17, 50000 },
  99. { 16, 25000 },
  100. { 13, 3125 },
  101. };
  102. static const struct {
  103. unsigned int scale, uscale; /* scale factor as integer + micro */
  104. u8 gain; /* gain register value */
  105. u8 res; /* resolution register value */
  106. } zopt2201_scale_als[] = {
  107. { 19, 200000, 0, 5 },
  108. { 6, 400000, 1, 5 },
  109. { 3, 200000, 2, 5 },
  110. { 2, 400000, 0, 4 },
  111. { 2, 133333, 3, 5 },
  112. { 1, 200000, 0, 3 },
  113. { 1, 66666, 4, 5 },
  114. { 0, 800000, 1, 4 },
  115. { 0, 600000, 0, 2 },
  116. { 0, 400000, 2, 4 },
  117. { 0, 300000, 0, 1 },
  118. { 0, 266666, 3, 4 },
  119. { 0, 200000, 2, 3 },
  120. { 0, 150000, 0, 0 },
  121. { 0, 133333, 4, 4 },
  122. { 0, 100000, 2, 2 },
  123. { 0, 66666, 4, 3 },
  124. { 0, 50000, 2, 1 },
  125. { 0, 33333, 4, 2 },
  126. { 0, 25000, 2, 0 },
  127. { 0, 16666, 4, 1 },
  128. { 0, 8333, 4, 0 },
  129. };
  130. static const struct {
  131. unsigned int scale, uscale; /* scale factor as integer + micro */
  132. u8 gain; /* gain register value */
  133. u8 res; /* resolution register value */
  134. } zopt2201_scale_uvb[] = {
  135. { 0, 460800, 0, 5 },
  136. { 0, 153600, 1, 5 },
  137. { 0, 76800, 2, 5 },
  138. { 0, 57600, 0, 4 },
  139. { 0, 51200, 3, 5 },
  140. { 0, 28800, 0, 3 },
  141. { 0, 25600, 4, 5 },
  142. { 0, 19200, 1, 4 },
  143. { 0, 14400, 0, 2 },
  144. { 0, 9600, 2, 4 },
  145. { 0, 7200, 0, 1 },
  146. { 0, 6400, 3, 4 },
  147. { 0, 4800, 2, 3 },
  148. { 0, 3600, 0, 0 },
  149. { 0, 3200, 4, 4 },
  150. { 0, 2400, 2, 2 },
  151. { 0, 1600, 4, 3 },
  152. { 0, 1200, 2, 1 },
  153. { 0, 800, 4, 2 },
  154. { 0, 600, 2, 0 },
  155. { 0, 400, 4, 1 },
  156. { 0, 200, 4, 0 },
  157. };
  158. static int zopt2201_enable_mode(struct zopt2201_data *data, bool uvb_mode)
  159. {
  160. u8 out = ZOPT2201_MAIN_CTRL_LS_EN;
  161. if (uvb_mode)
  162. out |= ZOPT2201_MAIN_CTRL_LS_MODE;
  163. return i2c_smbus_write_byte_data(data->client, ZOPT2201_MAIN_CTRL, out);
  164. }
  165. static int zopt2201_read(struct zopt2201_data *data, u8 reg)
  166. {
  167. struct i2c_client *client = data->client;
  168. int tries = 10;
  169. u8 buf[3];
  170. int ret;
  171. mutex_lock(&data->lock);
  172. ret = zopt2201_enable_mode(data, reg == ZOPT2201_UVB_DATA);
  173. if (ret < 0)
  174. goto fail;
  175. while (tries--) {
  176. unsigned long t = zopt2201_resolution[data->res].us;
  177. if (t <= 20000)
  178. usleep_range(t, t + 1000);
  179. else
  180. msleep(t / 1000);
  181. ret = i2c_smbus_read_byte_data(client, ZOPT2201_MAIN_STATUS);
  182. if (ret < 0)
  183. goto fail;
  184. if (ret & ZOPT2201_MAIN_STATUS_DRDY)
  185. break;
  186. }
  187. if (tries < 0) {
  188. ret = -ETIMEDOUT;
  189. goto fail;
  190. }
  191. ret = i2c_smbus_read_i2c_block_data(client, reg, sizeof(buf), buf);
  192. if (ret < 0)
  193. goto fail;
  194. ret = i2c_smbus_write_byte_data(client, ZOPT2201_MAIN_CTRL, 0x00);
  195. if (ret < 0)
  196. goto fail;
  197. mutex_unlock(&data->lock);
  198. return (buf[2] << 16) | (buf[1] << 8) | buf[0];
  199. fail:
  200. mutex_unlock(&data->lock);
  201. return ret;
  202. }
  203. static const struct iio_chan_spec zopt2201_channels[] = {
  204. {
  205. .type = IIO_LIGHT,
  206. .address = ZOPT2201_ALS_DATA,
  207. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  208. BIT(IIO_CHAN_INFO_SCALE),
  209. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
  210. },
  211. {
  212. .type = IIO_INTENSITY,
  213. .modified = 1,
  214. .channel2 = IIO_MOD_LIGHT_UV,
  215. .address = ZOPT2201_UVB_DATA,
  216. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  217. BIT(IIO_CHAN_INFO_SCALE),
  218. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
  219. },
  220. {
  221. .type = IIO_UVINDEX,
  222. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  223. },
  224. };
  225. static int zopt2201_read_raw(struct iio_dev *indio_dev,
  226. struct iio_chan_spec const *chan,
  227. int *val, int *val2, long mask)
  228. {
  229. struct zopt2201_data *data = iio_priv(indio_dev);
  230. u64 tmp;
  231. int ret;
  232. switch (mask) {
  233. case IIO_CHAN_INFO_RAW:
  234. ret = zopt2201_read(data, chan->address);
  235. if (ret < 0)
  236. return ret;
  237. *val = ret;
  238. return IIO_VAL_INT;
  239. case IIO_CHAN_INFO_PROCESSED:
  240. ret = zopt2201_read(data, ZOPT2201_UVB_DATA);
  241. if (ret < 0)
  242. return ret;
  243. *val = ret * 18 *
  244. (1 << (20 - zopt2201_resolution[data->res].bits)) /
  245. zopt2201_gain_uvb[data->gain].gain;
  246. return IIO_VAL_INT;
  247. case IIO_CHAN_INFO_SCALE:
  248. switch (chan->address) {
  249. case ZOPT2201_ALS_DATA:
  250. *val = zopt2201_gain_als[data->gain].scale;
  251. break;
  252. case ZOPT2201_UVB_DATA:
  253. *val = zopt2201_gain_uvb[data->gain].scale;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. *val2 = 1000000;
  259. *val2 *= (1 << (zopt2201_resolution[data->res].bits - 13));
  260. tmp = div_s64(*val * 1000000ULL, *val2);
  261. *val = div_s64_rem(tmp, 1000000, val2);
  262. return IIO_VAL_INT_PLUS_MICRO;
  263. case IIO_CHAN_INFO_INT_TIME:
  264. *val = 0;
  265. *val2 = zopt2201_resolution[data->res].us;
  266. return IIO_VAL_INT_PLUS_MICRO;
  267. default:
  268. return -EINVAL;
  269. }
  270. }
  271. static int zopt2201_set_resolution(struct zopt2201_data *data, u8 res)
  272. {
  273. int ret;
  274. ret = i2c_smbus_write_byte_data(data->client, ZOPT2201_LS_MEAS_RATE,
  275. (res << ZOPT2201_MEAS_RES_SHIFT) |
  276. data->rate);
  277. if (ret < 0)
  278. return ret;
  279. data->res = res;
  280. return 0;
  281. }
  282. static int zopt2201_write_resolution(struct zopt2201_data *data,
  283. int val, int val2)
  284. {
  285. int i, ret;
  286. if (val != 0)
  287. return -EINVAL;
  288. for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
  289. if (val2 == zopt2201_resolution[i].us) {
  290. mutex_lock(&data->lock);
  291. ret = zopt2201_set_resolution(data, i);
  292. mutex_unlock(&data->lock);
  293. return ret;
  294. }
  295. return -EINVAL;
  296. }
  297. static int zopt2201_set_gain(struct zopt2201_data *data, u8 gain)
  298. {
  299. int ret;
  300. ret = i2c_smbus_write_byte_data(data->client, ZOPT2201_LS_GAIN, gain);
  301. if (ret < 0)
  302. return ret;
  303. data->gain = gain;
  304. return 0;
  305. }
  306. static int zopt2201_write_scale_als_by_idx(struct zopt2201_data *data, int idx)
  307. {
  308. int ret;
  309. mutex_lock(&data->lock);
  310. ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
  311. if (ret < 0)
  312. goto unlock;
  313. ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
  314. unlock:
  315. mutex_unlock(&data->lock);
  316. return ret;
  317. }
  318. static int zopt2201_write_scale_als(struct zopt2201_data *data,
  319. int val, int val2)
  320. {
  321. int i;
  322. for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++)
  323. if (val == zopt2201_scale_als[i].scale &&
  324. val2 == zopt2201_scale_als[i].uscale) {
  325. return zopt2201_write_scale_als_by_idx(data, i);
  326. }
  327. return -EINVAL;
  328. }
  329. static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data *data, int idx)
  330. {
  331. int ret;
  332. mutex_lock(&data->lock);
  333. ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
  334. if (ret < 0)
  335. goto unlock;
  336. ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
  337. unlock:
  338. mutex_unlock(&data->lock);
  339. return ret;
  340. }
  341. static int zopt2201_write_scale_uvb(struct zopt2201_data *data,
  342. int val, int val2)
  343. {
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++)
  346. if (val == zopt2201_scale_uvb[i].scale &&
  347. val2 == zopt2201_scale_uvb[i].uscale)
  348. return zopt2201_write_scale_uvb_by_idx(data, i);
  349. return -EINVAL;
  350. }
  351. static int zopt2201_write_raw(struct iio_dev *indio_dev,
  352. struct iio_chan_spec const *chan,
  353. int val, int val2, long mask)
  354. {
  355. struct zopt2201_data *data = iio_priv(indio_dev);
  356. switch (mask) {
  357. case IIO_CHAN_INFO_INT_TIME:
  358. return zopt2201_write_resolution(data, val, val2);
  359. case IIO_CHAN_INFO_SCALE:
  360. switch (chan->address) {
  361. case ZOPT2201_ALS_DATA:
  362. return zopt2201_write_scale_als(data, val, val2);
  363. case ZOPT2201_UVB_DATA:
  364. return zopt2201_write_scale_uvb(data, val, val2);
  365. default:
  366. return -EINVAL;
  367. }
  368. }
  369. return -EINVAL;
  370. }
  371. static ssize_t zopt2201_show_int_time_available(struct device *dev,
  372. struct device_attribute *attr,
  373. char *buf)
  374. {
  375. size_t len = 0;
  376. int i;
  377. for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
  378. len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06lu ",
  379. zopt2201_resolution[i].us);
  380. buf[len - 1] = '\n';
  381. return len;
  382. }
  383. static IIO_DEV_ATTR_INT_TIME_AVAIL(zopt2201_show_int_time_available);
  384. static ssize_t zopt2201_show_als_scale_avail(struct device *dev,
  385. struct device_attribute *attr,
  386. char *buf)
  387. {
  388. ssize_t len = 0;
  389. int i;
  390. for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++)
  391. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
  392. zopt2201_scale_als[i].scale,
  393. zopt2201_scale_als[i].uscale);
  394. buf[len - 1] = '\n';
  395. return len;
  396. }
  397. static ssize_t zopt2201_show_uvb_scale_avail(struct device *dev,
  398. struct device_attribute *attr,
  399. char *buf)
  400. {
  401. ssize_t len = 0;
  402. int i;
  403. for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++)
  404. len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
  405. zopt2201_scale_uvb[i].scale,
  406. zopt2201_scale_uvb[i].uscale);
  407. buf[len - 1] = '\n';
  408. return len;
  409. }
  410. static IIO_DEVICE_ATTR(in_illuminance_scale_available, 0444,
  411. zopt2201_show_als_scale_avail, NULL, 0);
  412. static IIO_DEVICE_ATTR(in_intensity_uv_scale_available, 0444,
  413. zopt2201_show_uvb_scale_avail, NULL, 0);
  414. static struct attribute *zopt2201_attributes[] = {
  415. &iio_dev_attr_integration_time_available.dev_attr.attr,
  416. &iio_dev_attr_in_illuminance_scale_available.dev_attr.attr,
  417. &iio_dev_attr_in_intensity_uv_scale_available.dev_attr.attr,
  418. NULL
  419. };
  420. static const struct attribute_group zopt2201_attribute_group = {
  421. .attrs = zopt2201_attributes,
  422. };
  423. static const struct iio_info zopt2201_info = {
  424. .read_raw = zopt2201_read_raw,
  425. .write_raw = zopt2201_write_raw,
  426. .attrs = &zopt2201_attribute_group,
  427. };
  428. static int zopt2201_probe(struct i2c_client *client,
  429. const struct i2c_device_id *id)
  430. {
  431. struct zopt2201_data *data;
  432. struct iio_dev *indio_dev;
  433. int ret;
  434. if (!i2c_check_functionality(client->adapter,
  435. I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  436. return -EOPNOTSUPP;
  437. ret = i2c_smbus_read_byte_data(client, ZOPT2201_PART_ID);
  438. if (ret < 0)
  439. return ret;
  440. if (ret != ZOPT2201_PART_NUMBER)
  441. return -ENODEV;
  442. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  443. if (!indio_dev)
  444. return -ENOMEM;
  445. data = iio_priv(indio_dev);
  446. i2c_set_clientdata(client, indio_dev);
  447. data->client = client;
  448. mutex_init(&data->lock);
  449. indio_dev->dev.parent = &client->dev;
  450. indio_dev->info = &zopt2201_info;
  451. indio_dev->channels = zopt2201_channels;
  452. indio_dev->num_channels = ARRAY_SIZE(zopt2201_channels);
  453. indio_dev->name = ZOPT2201_DRV_NAME;
  454. indio_dev->modes = INDIO_DIRECT_MODE;
  455. data->rate = ZOPT2201_MEAS_FREQ_100MS;
  456. ret = zopt2201_set_resolution(data, ZOPT2201_MEAS_RES_18BIT);
  457. if (ret < 0)
  458. return ret;
  459. ret = zopt2201_set_gain(data, ZOPT2201_LS_GAIN_3);
  460. if (ret < 0)
  461. return ret;
  462. return devm_iio_device_register(&client->dev, indio_dev);
  463. }
  464. static const struct i2c_device_id zopt2201_id[] = {
  465. { "zopt2201", 0 },
  466. { }
  467. };
  468. MODULE_DEVICE_TABLE(i2c, zopt2201_id);
  469. static struct i2c_driver zopt2201_driver = {
  470. .driver = {
  471. .name = ZOPT2201_DRV_NAME,
  472. },
  473. .probe = zopt2201_probe,
  474. .id_table = zopt2201_id,
  475. };
  476. module_i2c_driver(zopt2201_driver);
  477. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  478. MODULE_DESCRIPTION("IDT ZOPT2201 ambient light and UV B sensor driver");
  479. MODULE_LICENSE("GPL");