us5182d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Copyright (c) 2015 Intel Corporation
  3. *
  4. * Driver for UPISEMI us5182d Proximity and Ambient Light Sensor.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * To do: Interrupt support.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/acpi.h>
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/sysfs.h>
  24. #include <linux/mutex.h>
  25. #define US5182D_REG_CFG0 0x00
  26. #define US5182D_CFG0_ONESHOT_EN BIT(6)
  27. #define US5182D_CFG0_SHUTDOWN_EN BIT(7)
  28. #define US5182D_CFG0_WORD_ENABLE BIT(0)
  29. #define US5182D_REG_CFG1 0x01
  30. #define US5182D_CFG1_ALS_RES16 BIT(4)
  31. #define US5182D_CFG1_AGAIN_DEFAULT 0x00
  32. #define US5182D_REG_CFG2 0x02
  33. #define US5182D_CFG2_PX_RES16 BIT(4)
  34. #define US5182D_CFG2_PXGAIN_DEFAULT BIT(2)
  35. #define US5182D_REG_CFG3 0x03
  36. #define US5182D_CFG3_LED_CURRENT100 (BIT(4) | BIT(5))
  37. #define US5182D_REG_CFG4 0x10
  38. /*
  39. * Registers for tuning the auto dark current cancelling feature.
  40. * DARK_TH(reg 0x27,0x28) - threshold (counts) for auto dark cancelling.
  41. * when ALS > DARK_TH --> ALS_Code = ALS - Upper(0x2A) * Dark
  42. * when ALS < DARK_TH --> ALS_Code = ALS - Lower(0x29) * Dark
  43. */
  44. #define US5182D_REG_UDARK_TH 0x27
  45. #define US5182D_REG_DARK_AUTO_EN 0x2b
  46. #define US5182D_REG_AUTO_LDARK_GAIN 0x29
  47. #define US5182D_REG_AUTO_HDARK_GAIN 0x2a
  48. #define US5182D_OPMODE_ALS 0x01
  49. #define US5182D_OPMODE_PX 0x02
  50. #define US5182D_OPMODE_SHIFT 4
  51. #define US5182D_REG_DARK_AUTO_EN_DEFAULT 0x80
  52. #define US5182D_REG_AUTO_LDARK_GAIN_DEFAULT 0x16
  53. #define US5182D_REG_AUTO_HDARK_GAIN_DEFAULT 0x00
  54. #define US5182D_REG_ADL 0x0c
  55. #define US5182D_REG_PDL 0x0e
  56. #define US5182D_REG_MODE_STORE 0x21
  57. #define US5182D_STORE_MODE 0x01
  58. #define US5182D_REG_CHIPID 0xb2
  59. #define US5182D_OPMODE_MASK GENMASK(5, 4)
  60. #define US5182D_AGAIN_MASK 0x07
  61. #define US5182D_RESET_CHIP 0x01
  62. #define US5182D_CHIPID 0x26
  63. #define US5182D_DRV_NAME "us5182d"
  64. #define US5182D_GA_RESOLUTION 1000
  65. #define US5182D_READ_BYTE 1
  66. #define US5182D_READ_WORD 2
  67. #define US5182D_OPSTORE_SLEEP_TIME 20 /* ms */
  68. /* Available ranges: [12354, 7065, 3998, 2202, 1285, 498, 256, 138] lux */
  69. static const int us5182d_scales[] = {188500, 107800, 61000, 33600, 19600, 7600,
  70. 3900, 2100};
  71. /*
  72. * Experimental thresholds that work with US5182D sensor on evaluation board
  73. * roughly between 12-32 lux
  74. */
  75. static u16 us5182d_dark_ths_vals[] = {170, 200, 512, 512, 800, 2000, 4000,
  76. 8000};
  77. enum mode {
  78. US5182D_ALS_PX,
  79. US5182D_ALS_ONLY,
  80. US5182D_PX_ONLY
  81. };
  82. struct us5182d_data {
  83. struct i2c_client *client;
  84. struct mutex lock;
  85. /* Glass attenuation factor */
  86. u32 ga;
  87. /* Dark gain tuning */
  88. u8 lower_dark_gain;
  89. u8 upper_dark_gain;
  90. u16 *us5182d_dark_ths;
  91. u8 opmode;
  92. };
  93. static IIO_CONST_ATTR(in_illuminance_scale_available,
  94. "0.0021 0.0039 0.0076 0.0196 0.0336 0.061 0.1078 0.1885");
  95. static struct attribute *us5182d_attrs[] = {
  96. &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
  97. NULL
  98. };
  99. static const struct attribute_group us5182d_attr_group = {
  100. .attrs = us5182d_attrs,
  101. };
  102. static const struct {
  103. u8 reg;
  104. u8 val;
  105. } us5182d_regvals[] = {
  106. {US5182D_REG_CFG0, (US5182D_CFG0_SHUTDOWN_EN |
  107. US5182D_CFG0_WORD_ENABLE)},
  108. {US5182D_REG_CFG1, US5182D_CFG1_ALS_RES16},
  109. {US5182D_REG_CFG2, (US5182D_CFG2_PX_RES16 |
  110. US5182D_CFG2_PXGAIN_DEFAULT)},
  111. {US5182D_REG_CFG3, US5182D_CFG3_LED_CURRENT100},
  112. {US5182D_REG_MODE_STORE, US5182D_STORE_MODE},
  113. {US5182D_REG_CFG4, 0x00},
  114. };
  115. static const struct iio_chan_spec us5182d_channels[] = {
  116. {
  117. .type = IIO_LIGHT,
  118. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  119. BIT(IIO_CHAN_INFO_SCALE),
  120. },
  121. {
  122. .type = IIO_PROXIMITY,
  123. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  124. }
  125. };
  126. static int us5182d_get_als(struct us5182d_data *data)
  127. {
  128. int ret;
  129. unsigned long result;
  130. ret = i2c_smbus_read_word_data(data->client,
  131. US5182D_REG_ADL);
  132. if (ret < 0)
  133. return ret;
  134. result = ret * data->ga / US5182D_GA_RESOLUTION;
  135. if (result > 0xffff)
  136. result = 0xffff;
  137. return result;
  138. }
  139. static int us5182d_set_opmode(struct us5182d_data *data, u8 mode)
  140. {
  141. int ret;
  142. ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG0);
  143. if (ret < 0)
  144. return ret;
  145. /*
  146. * In oneshot mode the chip will power itself down after taking the
  147. * required measurement.
  148. */
  149. ret = ret | US5182D_CFG0_ONESHOT_EN;
  150. /* update mode */
  151. ret = ret & ~US5182D_OPMODE_MASK;
  152. ret = ret | (mode << US5182D_OPMODE_SHIFT);
  153. /*
  154. * After updating the operating mode, the chip requires that
  155. * the operation is stored, by writing 1 in the STORE_MODE
  156. * register (auto-clearing).
  157. */
  158. ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_CFG0, ret);
  159. if (ret < 0)
  160. return ret;
  161. if (mode == data->opmode)
  162. return 0;
  163. ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_MODE_STORE,
  164. US5182D_STORE_MODE);
  165. if (ret < 0)
  166. return ret;
  167. data->opmode = mode;
  168. msleep(US5182D_OPSTORE_SLEEP_TIME);
  169. return 0;
  170. }
  171. static int us5182d_read_raw(struct iio_dev *indio_dev,
  172. struct iio_chan_spec const *chan, int *val,
  173. int *val2, long mask)
  174. {
  175. struct us5182d_data *data = iio_priv(indio_dev);
  176. int ret;
  177. switch (mask) {
  178. case IIO_CHAN_INFO_RAW:
  179. switch (chan->type) {
  180. case IIO_LIGHT:
  181. mutex_lock(&data->lock);
  182. ret = us5182d_set_opmode(data, US5182D_OPMODE_ALS);
  183. if (ret < 0)
  184. goto out_err;
  185. ret = us5182d_get_als(data);
  186. if (ret < 0)
  187. goto out_err;
  188. mutex_unlock(&data->lock);
  189. *val = ret;
  190. return IIO_VAL_INT;
  191. case IIO_PROXIMITY:
  192. mutex_lock(&data->lock);
  193. ret = us5182d_set_opmode(data, US5182D_OPMODE_PX);
  194. if (ret < 0)
  195. goto out_err;
  196. ret = i2c_smbus_read_word_data(data->client,
  197. US5182D_REG_PDL);
  198. if (ret < 0)
  199. goto out_err;
  200. mutex_unlock(&data->lock);
  201. *val = ret;
  202. return IIO_VAL_INT;
  203. default:
  204. return -EINVAL;
  205. }
  206. case IIO_CHAN_INFO_SCALE:
  207. ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1);
  208. if (ret < 0)
  209. return ret;
  210. *val = 0;
  211. *val2 = us5182d_scales[ret & US5182D_AGAIN_MASK];
  212. return IIO_VAL_INT_PLUS_MICRO;
  213. default:
  214. return -EINVAL;
  215. }
  216. return -EINVAL;
  217. out_err:
  218. mutex_unlock(&data->lock);
  219. return ret;
  220. }
  221. /**
  222. * us5182d_update_dark_th - update Darh_Th registers
  223. * @data us5182d_data structure
  224. * @index index in us5182d_dark_ths array to use for the updated value
  225. *
  226. * Function needs to be called with a lock held because it needs two i2c write
  227. * byte operations as these registers (0x27 0x28) don't work in word mode
  228. * accessing.
  229. */
  230. static int us5182d_update_dark_th(struct us5182d_data *data, int index)
  231. {
  232. __be16 dark_th = cpu_to_be16(data->us5182d_dark_ths[index]);
  233. int ret;
  234. ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_UDARK_TH,
  235. ((u8 *)&dark_th)[0]);
  236. if (ret < 0)
  237. return ret;
  238. return i2c_smbus_write_byte_data(data->client, US5182D_REG_UDARK_TH + 1,
  239. ((u8 *)&dark_th)[1]);
  240. }
  241. /**
  242. * us5182d_apply_scale - update the ALS scale
  243. * @data us5182d_data structure
  244. * @index index in us5182d_scales array to use for the updated value
  245. *
  246. * Function needs to be called with a lock held as we're having more than one
  247. * i2c operation.
  248. */
  249. static int us5182d_apply_scale(struct us5182d_data *data, int index)
  250. {
  251. int ret;
  252. ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1);
  253. if (ret < 0)
  254. return ret;
  255. ret = ret & (~US5182D_AGAIN_MASK);
  256. ret |= index;
  257. ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_CFG1, ret);
  258. if (ret < 0)
  259. return ret;
  260. return us5182d_update_dark_th(data, index);
  261. }
  262. static int us5182d_write_raw(struct iio_dev *indio_dev,
  263. struct iio_chan_spec const *chan, int val,
  264. int val2, long mask)
  265. {
  266. struct us5182d_data *data = iio_priv(indio_dev);
  267. int ret, i;
  268. switch (mask) {
  269. case IIO_CHAN_INFO_SCALE:
  270. if (val != 0)
  271. return -EINVAL;
  272. for (i = 0; i < ARRAY_SIZE(us5182d_scales); i++)
  273. if (val2 == us5182d_scales[i]) {
  274. mutex_lock(&data->lock);
  275. ret = us5182d_apply_scale(data, i);
  276. mutex_unlock(&data->lock);
  277. return ret;
  278. }
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. return -EINVAL;
  284. }
  285. static const struct iio_info us5182d_info = {
  286. .driver_module = THIS_MODULE,
  287. .read_raw = us5182d_read_raw,
  288. .write_raw = us5182d_write_raw,
  289. .attrs = &us5182d_attr_group,
  290. };
  291. static int us5182d_reset(struct iio_dev *indio_dev)
  292. {
  293. struct us5182d_data *data = iio_priv(indio_dev);
  294. return i2c_smbus_write_byte_data(data->client, US5182D_REG_CFG3,
  295. US5182D_RESET_CHIP);
  296. }
  297. static int us5182d_init(struct iio_dev *indio_dev)
  298. {
  299. struct us5182d_data *data = iio_priv(indio_dev);
  300. int i, ret;
  301. ret = us5182d_reset(indio_dev);
  302. if (ret < 0)
  303. return ret;
  304. data->opmode = 0;
  305. for (i = 0; i < ARRAY_SIZE(us5182d_regvals); i++) {
  306. ret = i2c_smbus_write_byte_data(data->client,
  307. us5182d_regvals[i].reg,
  308. us5182d_regvals[i].val);
  309. if (ret < 0)
  310. return ret;
  311. }
  312. return 0;
  313. }
  314. static void us5182d_get_platform_data(struct iio_dev *indio_dev)
  315. {
  316. struct us5182d_data *data = iio_priv(indio_dev);
  317. if (device_property_read_u32(&data->client->dev, "upisemi,glass-coef",
  318. &data->ga))
  319. data->ga = US5182D_GA_RESOLUTION;
  320. if (device_property_read_u16_array(&data->client->dev,
  321. "upisemi,dark-ths",
  322. data->us5182d_dark_ths,
  323. ARRAY_SIZE(us5182d_dark_ths_vals)))
  324. data->us5182d_dark_ths = us5182d_dark_ths_vals;
  325. if (device_property_read_u8(&data->client->dev,
  326. "upisemi,upper-dark-gain",
  327. &data->upper_dark_gain))
  328. data->upper_dark_gain = US5182D_REG_AUTO_HDARK_GAIN_DEFAULT;
  329. if (device_property_read_u8(&data->client->dev,
  330. "upisemi,lower-dark-gain",
  331. &data->lower_dark_gain))
  332. data->lower_dark_gain = US5182D_REG_AUTO_LDARK_GAIN_DEFAULT;
  333. }
  334. static int us5182d_dark_gain_config(struct iio_dev *indio_dev)
  335. {
  336. struct us5182d_data *data = iio_priv(indio_dev);
  337. int ret;
  338. ret = us5182d_update_dark_th(data, US5182D_CFG1_AGAIN_DEFAULT);
  339. if (ret < 0)
  340. return ret;
  341. ret = i2c_smbus_write_byte_data(data->client,
  342. US5182D_REG_AUTO_LDARK_GAIN,
  343. data->lower_dark_gain);
  344. if (ret < 0)
  345. return ret;
  346. ret = i2c_smbus_write_byte_data(data->client,
  347. US5182D_REG_AUTO_HDARK_GAIN,
  348. data->upper_dark_gain);
  349. if (ret < 0)
  350. return ret;
  351. return i2c_smbus_write_byte_data(data->client, US5182D_REG_DARK_AUTO_EN,
  352. US5182D_REG_DARK_AUTO_EN_DEFAULT);
  353. }
  354. static int us5182d_probe(struct i2c_client *client,
  355. const struct i2c_device_id *id)
  356. {
  357. struct us5182d_data *data;
  358. struct iio_dev *indio_dev;
  359. int ret;
  360. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  361. if (!indio_dev)
  362. return -ENOMEM;
  363. data = iio_priv(indio_dev);
  364. i2c_set_clientdata(client, indio_dev);
  365. data->client = client;
  366. mutex_init(&data->lock);
  367. indio_dev->dev.parent = &client->dev;
  368. indio_dev->info = &us5182d_info;
  369. indio_dev->name = US5182D_DRV_NAME;
  370. indio_dev->channels = us5182d_channels;
  371. indio_dev->num_channels = ARRAY_SIZE(us5182d_channels);
  372. indio_dev->modes = INDIO_DIRECT_MODE;
  373. ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CHIPID);
  374. if (ret != US5182D_CHIPID) {
  375. dev_err(&data->client->dev,
  376. "Failed to detect US5182 light chip\n");
  377. return (ret < 0) ? ret : -ENODEV;
  378. }
  379. us5182d_get_platform_data(indio_dev);
  380. ret = us5182d_init(indio_dev);
  381. if (ret < 0)
  382. return ret;
  383. ret = us5182d_dark_gain_config(indio_dev);
  384. if (ret < 0)
  385. return ret;
  386. return iio_device_register(indio_dev);
  387. }
  388. static int us5182d_remove(struct i2c_client *client)
  389. {
  390. iio_device_unregister(i2c_get_clientdata(client));
  391. return i2c_smbus_write_byte_data(client, US5182D_REG_CFG0,
  392. US5182D_CFG0_SHUTDOWN_EN);
  393. }
  394. static const struct acpi_device_id us5182d_acpi_match[] = {
  395. { "USD5182", 0},
  396. {}
  397. };
  398. MODULE_DEVICE_TABLE(acpi, us5182d_acpi_match);
  399. static const struct i2c_device_id us5182d_id[] = {
  400. {"usd5182", 0},
  401. {}
  402. };
  403. MODULE_DEVICE_TABLE(i2c, us5182d_id);
  404. static struct i2c_driver us5182d_driver = {
  405. .driver = {
  406. .name = US5182D_DRV_NAME,
  407. .acpi_match_table = ACPI_PTR(us5182d_acpi_match),
  408. },
  409. .probe = us5182d_probe,
  410. .remove = us5182d_remove,
  411. .id_table = us5182d_id,
  412. };
  413. module_i2c_driver(us5182d_driver);
  414. MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
  415. MODULE_DESCRIPTION("Driver for us5182d Proximity and Light Sensor");
  416. MODULE_LICENSE("GPL v2");