pa12203001.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2015 Intel Corporation
  3. *
  4. * Driver for TXC PA12203001 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. * To do: Interrupt support.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/acpi.h>
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/mutex.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regmap.h>
  22. #define PA12203001_DRIVER_NAME "pa12203001"
  23. #define PA12203001_REG_CFG0 0x00
  24. #define PA12203001_REG_CFG1 0x01
  25. #define PA12203001_REG_CFG2 0x02
  26. #define PA12203001_REG_CFG3 0x03
  27. #define PA12203001_REG_ADL 0x0b
  28. #define PA12203001_REG_PDH 0x0e
  29. #define PA12203001_REG_POFS 0x10
  30. #define PA12203001_REG_PSET 0x11
  31. #define PA12203001_ALS_EN_MASK BIT(0)
  32. #define PA12203001_PX_EN_MASK BIT(1)
  33. #define PA12203001_PX_NORMAL_MODE_MASK GENMASK(7, 6)
  34. #define PA12203001_AFSR_MASK GENMASK(5, 4)
  35. #define PA12203001_AFSR_SHIFT 4
  36. #define PA12203001_PSCAN 0x03
  37. /* als range 31000, ps, als disabled */
  38. #define PA12203001_REG_CFG0_DEFAULT 0x30
  39. /* led current: 100 mA */
  40. #define PA12203001_REG_CFG1_DEFAULT 0x20
  41. /* ps mode: normal, interrupts not active */
  42. #define PA12203001_REG_CFG2_DEFAULT 0xcc
  43. #define PA12203001_REG_CFG3_DEFAULT 0x00
  44. #define PA12203001_SLEEP_DELAY_MS 3000
  45. #define PA12203001_CHIP_ENABLE 0xff
  46. #define PA12203001_CHIP_DISABLE 0x00
  47. /* available scales: corresponding to [500, 4000, 7000, 31000] lux */
  48. static const int pa12203001_scales[] = { 7629, 61036, 106813, 473029};
  49. struct pa12203001_data {
  50. struct i2c_client *client;
  51. /* protect device states */
  52. struct mutex lock;
  53. bool als_enabled;
  54. bool px_enabled;
  55. bool als_needs_enable;
  56. bool px_needs_enable;
  57. struct regmap *map;
  58. };
  59. static const struct {
  60. u8 reg;
  61. u8 val;
  62. } regvals[] = {
  63. {PA12203001_REG_CFG0, PA12203001_REG_CFG0_DEFAULT},
  64. {PA12203001_REG_CFG1, PA12203001_REG_CFG1_DEFAULT},
  65. {PA12203001_REG_CFG2, PA12203001_REG_CFG2_DEFAULT},
  66. {PA12203001_REG_CFG3, PA12203001_REG_CFG3_DEFAULT},
  67. {PA12203001_REG_PSET, PA12203001_PSCAN},
  68. };
  69. static IIO_CONST_ATTR(in_illuminance_scale_available,
  70. "0.007629 0.061036 0.106813 0.473029");
  71. static struct attribute *pa12203001_attrs[] = {
  72. &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
  73. NULL
  74. };
  75. static const struct attribute_group pa12203001_attr_group = {
  76. .attrs = pa12203001_attrs,
  77. };
  78. static const struct iio_chan_spec pa12203001_channels[] = {
  79. {
  80. .type = IIO_LIGHT,
  81. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  82. BIT(IIO_CHAN_INFO_SCALE),
  83. },
  84. {
  85. .type = IIO_PROXIMITY,
  86. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  87. }
  88. };
  89. static const struct regmap_range pa12203001_volatile_regs_ranges[] = {
  90. regmap_reg_range(PA12203001_REG_ADL, PA12203001_REG_ADL + 1),
  91. regmap_reg_range(PA12203001_REG_PDH, PA12203001_REG_PDH),
  92. };
  93. static const struct regmap_access_table pa12203001_volatile_regs = {
  94. .yes_ranges = pa12203001_volatile_regs_ranges,
  95. .n_yes_ranges = ARRAY_SIZE(pa12203001_volatile_regs_ranges),
  96. };
  97. static const struct regmap_config pa12203001_regmap_config = {
  98. .reg_bits = 8,
  99. .val_bits = 8,
  100. .max_register = PA12203001_REG_PSET,
  101. .cache_type = REGCACHE_RBTREE,
  102. .volatile_table = &pa12203001_volatile_regs,
  103. };
  104. static inline int pa12203001_als_enable(struct pa12203001_data *data, u8 enable)
  105. {
  106. int ret;
  107. ret = regmap_update_bits(data->map, PA12203001_REG_CFG0,
  108. PA12203001_ALS_EN_MASK, enable);
  109. if (ret < 0)
  110. return ret;
  111. data->als_enabled = !!enable;
  112. return 0;
  113. }
  114. static inline int pa12203001_px_enable(struct pa12203001_data *data, u8 enable)
  115. {
  116. int ret;
  117. ret = regmap_update_bits(data->map, PA12203001_REG_CFG0,
  118. PA12203001_PX_EN_MASK, enable);
  119. if (ret < 0)
  120. return ret;
  121. data->px_enabled = !!enable;
  122. return 0;
  123. }
  124. static int pa12203001_set_power_state(struct pa12203001_data *data, bool on,
  125. u8 mask)
  126. {
  127. #ifdef CONFIG_PM
  128. int ret;
  129. if (on && (mask & PA12203001_ALS_EN_MASK)) {
  130. mutex_lock(&data->lock);
  131. if (data->px_enabled) {
  132. ret = pa12203001_als_enable(data,
  133. PA12203001_ALS_EN_MASK);
  134. if (ret < 0)
  135. goto err;
  136. } else {
  137. data->als_needs_enable = true;
  138. }
  139. mutex_unlock(&data->lock);
  140. }
  141. if (on && (mask & PA12203001_PX_EN_MASK)) {
  142. mutex_lock(&data->lock);
  143. if (data->als_enabled) {
  144. ret = pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
  145. if (ret < 0)
  146. goto err;
  147. } else {
  148. data->px_needs_enable = true;
  149. }
  150. mutex_unlock(&data->lock);
  151. }
  152. if (on) {
  153. ret = pm_runtime_get_sync(&data->client->dev);
  154. if (ret < 0)
  155. pm_runtime_put_noidle(&data->client->dev);
  156. } else {
  157. pm_runtime_mark_last_busy(&data->client->dev);
  158. ret = pm_runtime_put_autosuspend(&data->client->dev);
  159. }
  160. return ret;
  161. err:
  162. mutex_unlock(&data->lock);
  163. return ret;
  164. #endif
  165. return 0;
  166. }
  167. static int pa12203001_read_raw(struct iio_dev *indio_dev,
  168. struct iio_chan_spec const *chan, int *val,
  169. int *val2, long mask)
  170. {
  171. struct pa12203001_data *data = iio_priv(indio_dev);
  172. int ret;
  173. u8 dev_mask;
  174. unsigned int reg_byte;
  175. __le16 reg_word;
  176. switch (mask) {
  177. case IIO_CHAN_INFO_RAW:
  178. switch (chan->type) {
  179. case IIO_LIGHT:
  180. dev_mask = PA12203001_ALS_EN_MASK;
  181. ret = pa12203001_set_power_state(data, true, dev_mask);
  182. if (ret < 0)
  183. return ret;
  184. /*
  185. * ALS ADC value is stored in registers
  186. * PA12203001_REG_ADL and in PA12203001_REG_ADL + 1.
  187. */
  188. ret = regmap_bulk_read(data->map, PA12203001_REG_ADL,
  189. &reg_word, 2);
  190. if (ret < 0)
  191. goto reg_err;
  192. *val = le16_to_cpu(reg_word);
  193. ret = pa12203001_set_power_state(data, false, dev_mask);
  194. if (ret < 0)
  195. return ret;
  196. break;
  197. case IIO_PROXIMITY:
  198. dev_mask = PA12203001_PX_EN_MASK;
  199. ret = pa12203001_set_power_state(data, true, dev_mask);
  200. if (ret < 0)
  201. return ret;
  202. ret = regmap_read(data->map, PA12203001_REG_PDH,
  203. &reg_byte);
  204. if (ret < 0)
  205. goto reg_err;
  206. *val = reg_byte;
  207. ret = pa12203001_set_power_state(data, false, dev_mask);
  208. if (ret < 0)
  209. return ret;
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. return IIO_VAL_INT;
  215. case IIO_CHAN_INFO_SCALE:
  216. ret = regmap_read(data->map, PA12203001_REG_CFG0, &reg_byte);
  217. if (ret < 0)
  218. return ret;
  219. *val = 0;
  220. reg_byte = (reg_byte & PA12203001_AFSR_MASK);
  221. *val2 = pa12203001_scales[reg_byte >> 4];
  222. return IIO_VAL_INT_PLUS_MICRO;
  223. default:
  224. return -EINVAL;
  225. }
  226. reg_err:
  227. pa12203001_set_power_state(data, false, dev_mask);
  228. return ret;
  229. }
  230. static int pa12203001_write_raw(struct iio_dev *indio_dev,
  231. struct iio_chan_spec const *chan, int val,
  232. int val2, long mask)
  233. {
  234. struct pa12203001_data *data = iio_priv(indio_dev);
  235. int i, ret, new_val;
  236. unsigned int reg_byte;
  237. switch (mask) {
  238. case IIO_CHAN_INFO_SCALE:
  239. ret = regmap_read(data->map, PA12203001_REG_CFG0, &reg_byte);
  240. if (val != 0 || ret < 0)
  241. return -EINVAL;
  242. for (i = 0; i < ARRAY_SIZE(pa12203001_scales); i++) {
  243. if (val2 == pa12203001_scales[i]) {
  244. new_val = i << PA12203001_AFSR_SHIFT;
  245. return regmap_update_bits(data->map,
  246. PA12203001_REG_CFG0,
  247. PA12203001_AFSR_MASK,
  248. new_val);
  249. }
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. return -EINVAL;
  256. }
  257. static const struct iio_info pa12203001_info = {
  258. .read_raw = pa12203001_read_raw,
  259. .write_raw = pa12203001_write_raw,
  260. .attrs = &pa12203001_attr_group,
  261. };
  262. static int pa12203001_init(struct iio_dev *indio_dev)
  263. {
  264. struct pa12203001_data *data = iio_priv(indio_dev);
  265. int i, ret;
  266. for (i = 0; i < ARRAY_SIZE(regvals); i++) {
  267. ret = regmap_write(data->map, regvals[i].reg, regvals[i].val);
  268. if (ret < 0)
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static int pa12203001_power_chip(struct iio_dev *indio_dev, u8 state)
  274. {
  275. struct pa12203001_data *data = iio_priv(indio_dev);
  276. int ret;
  277. mutex_lock(&data->lock);
  278. ret = pa12203001_als_enable(data, state);
  279. if (ret < 0)
  280. goto out;
  281. ret = pa12203001_px_enable(data, state);
  282. out:
  283. mutex_unlock(&data->lock);
  284. return ret;
  285. }
  286. static int pa12203001_probe(struct i2c_client *client,
  287. const struct i2c_device_id *id)
  288. {
  289. struct pa12203001_data *data;
  290. struct iio_dev *indio_dev;
  291. int ret;
  292. indio_dev = devm_iio_device_alloc(&client->dev,
  293. sizeof(struct pa12203001_data));
  294. if (!indio_dev)
  295. return -ENOMEM;
  296. data = iio_priv(indio_dev);
  297. i2c_set_clientdata(client, indio_dev);
  298. data->client = client;
  299. data->map = devm_regmap_init_i2c(client, &pa12203001_regmap_config);
  300. if (IS_ERR(data->map))
  301. return PTR_ERR(data->map);
  302. mutex_init(&data->lock);
  303. indio_dev->dev.parent = &client->dev;
  304. indio_dev->info = &pa12203001_info;
  305. indio_dev->name = PA12203001_DRIVER_NAME;
  306. indio_dev->channels = pa12203001_channels;
  307. indio_dev->num_channels = ARRAY_SIZE(pa12203001_channels);
  308. indio_dev->modes = INDIO_DIRECT_MODE;
  309. ret = pa12203001_init(indio_dev);
  310. if (ret < 0)
  311. return ret;
  312. ret = pa12203001_power_chip(indio_dev, PA12203001_CHIP_ENABLE);
  313. if (ret < 0)
  314. return ret;
  315. ret = pm_runtime_set_active(&client->dev);
  316. if (ret < 0)
  317. goto out_err;
  318. pm_runtime_enable(&client->dev);
  319. pm_runtime_set_autosuspend_delay(&client->dev,
  320. PA12203001_SLEEP_DELAY_MS);
  321. pm_runtime_use_autosuspend(&client->dev);
  322. ret = iio_device_register(indio_dev);
  323. if (ret < 0)
  324. goto out_err;
  325. return 0;
  326. out_err:
  327. pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
  328. return ret;
  329. }
  330. static int pa12203001_remove(struct i2c_client *client)
  331. {
  332. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  333. iio_device_unregister(indio_dev);
  334. pm_runtime_disable(&client->dev);
  335. pm_runtime_set_suspended(&client->dev);
  336. return pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
  337. }
  338. #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM)
  339. static int pa12203001_suspend(struct device *dev)
  340. {
  341. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  342. return pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
  343. }
  344. #endif
  345. #ifdef CONFIG_PM_SLEEP
  346. static int pa12203001_resume(struct device *dev)
  347. {
  348. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  349. return pa12203001_power_chip(indio_dev, PA12203001_CHIP_ENABLE);
  350. }
  351. #endif
  352. #ifdef CONFIG_PM
  353. static int pa12203001_runtime_resume(struct device *dev)
  354. {
  355. struct pa12203001_data *data;
  356. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  357. mutex_lock(&data->lock);
  358. if (data->als_needs_enable) {
  359. pa12203001_als_enable(data, PA12203001_ALS_EN_MASK);
  360. data->als_needs_enable = false;
  361. }
  362. if (data->px_needs_enable) {
  363. pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
  364. data->px_needs_enable = false;
  365. }
  366. mutex_unlock(&data->lock);
  367. return 0;
  368. }
  369. #endif
  370. static const struct dev_pm_ops pa12203001_pm_ops = {
  371. SET_SYSTEM_SLEEP_PM_OPS(pa12203001_suspend, pa12203001_resume)
  372. SET_RUNTIME_PM_OPS(pa12203001_suspend, pa12203001_runtime_resume, NULL)
  373. };
  374. static const struct acpi_device_id pa12203001_acpi_match[] = {
  375. { "TXCPA122", 0},
  376. {}
  377. };
  378. MODULE_DEVICE_TABLE(acpi, pa12203001_acpi_match);
  379. static const struct i2c_device_id pa12203001_id[] = {
  380. {"txcpa122", 0},
  381. {}
  382. };
  383. MODULE_DEVICE_TABLE(i2c, pa12203001_id);
  384. static struct i2c_driver pa12203001_driver = {
  385. .driver = {
  386. .name = PA12203001_DRIVER_NAME,
  387. .pm = &pa12203001_pm_ops,
  388. .acpi_match_table = ACPI_PTR(pa12203001_acpi_match),
  389. },
  390. .probe = pa12203001_probe,
  391. .remove = pa12203001_remove,
  392. .id_table = pa12203001_id,
  393. };
  394. module_i2c_driver(pa12203001_driver);
  395. MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
  396. MODULE_DESCRIPTION("Driver for TXC PA12203001 Proximity and Light Sensor");
  397. MODULE_LICENSE("GPL v2");