cm3605.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CM3605 Ambient Light and Proximity Sensor
  4. *
  5. * Copyright (C) 2016 Linaro Ltd.
  6. * Author: Linus Walleij <[email protected]>
  7. *
  8. * This hardware was found in the very first Nexus One handset from Google/HTC
  9. * and an early endavour into mobile light and proximity sensors.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/sysfs.h>
  15. #include <linux/iio/events.h>
  16. #include <linux/iio/consumer.h> /* To get our ADC channel */
  17. #include <linux/iio/types.h> /* To deal with our ADC channel */
  18. #include <linux/init.h>
  19. #include <linux/leds.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/property.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/gpio/consumer.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/math64.h>
  26. #include <linux/pm.h>
  27. #define CM3605_PROX_CHANNEL 0
  28. #define CM3605_ALS_CHANNEL 1
  29. #define CM3605_AOUT_TYP_MAX_MV 1550
  30. /* It should not go above 1.650V according to the data sheet */
  31. #define CM3605_AOUT_MAX_MV 1650
  32. /**
  33. * struct cm3605 - CM3605 state
  34. * @dev: pointer to parent device
  35. * @vdd: regulator controlling VDD
  36. * @aset: sleep enable GPIO, high = sleep
  37. * @aout: IIO ADC channel to convert the AOUT signal
  38. * @als_max: maximum LUX detection (depends on RSET)
  39. * @dir: proximity direction: start as FALLING
  40. * @led: trigger for the infrared LED used by the proximity sensor
  41. */
  42. struct cm3605 {
  43. struct device *dev;
  44. struct regulator *vdd;
  45. struct gpio_desc *aset;
  46. struct iio_channel *aout;
  47. s32 als_max;
  48. enum iio_event_direction dir;
  49. struct led_trigger *led;
  50. };
  51. static irqreturn_t cm3605_prox_irq(int irq, void *d)
  52. {
  53. struct iio_dev *indio_dev = d;
  54. struct cm3605 *cm3605 = iio_priv(indio_dev);
  55. u64 ev;
  56. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, CM3605_PROX_CHANNEL,
  57. IIO_EV_TYPE_THRESH, cm3605->dir);
  58. iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
  59. /* Invert the edge for each event */
  60. if (cm3605->dir == IIO_EV_DIR_RISING)
  61. cm3605->dir = IIO_EV_DIR_FALLING;
  62. else
  63. cm3605->dir = IIO_EV_DIR_RISING;
  64. return IRQ_HANDLED;
  65. }
  66. static int cm3605_get_lux(struct cm3605 *cm3605)
  67. {
  68. int ret, res;
  69. s64 lux;
  70. ret = iio_read_channel_processed(cm3605->aout, &res);
  71. if (ret < 0)
  72. return ret;
  73. dev_dbg(cm3605->dev, "read %d mV from ADC\n", res);
  74. /*
  75. * AOUT has an offset of ~30mV then linear at dark
  76. * then goes from 2.54 up to 650 LUX yielding 1.55V
  77. * (1550 mV) so scale the returned value to this interval
  78. * using simple linear interpolation.
  79. */
  80. if (res < 30)
  81. return 0;
  82. if (res > CM3605_AOUT_MAX_MV)
  83. dev_err(cm3605->dev, "device out of range\n");
  84. /* Remove bias */
  85. lux = res - 30;
  86. /* Linear interpolation between 0 and ALS typ max */
  87. lux *= cm3605->als_max;
  88. lux = div64_s64(lux, CM3605_AOUT_TYP_MAX_MV);
  89. return lux;
  90. }
  91. static int cm3605_read_raw(struct iio_dev *indio_dev,
  92. struct iio_chan_spec const *chan,
  93. int *val, int *val2, long mask)
  94. {
  95. struct cm3605 *cm3605 = iio_priv(indio_dev);
  96. int ret;
  97. switch (mask) {
  98. case IIO_CHAN_INFO_RAW:
  99. switch (chan->type) {
  100. case IIO_LIGHT:
  101. ret = cm3605_get_lux(cm3605);
  102. if (ret < 0)
  103. return ret;
  104. *val = ret;
  105. return IIO_VAL_INT;
  106. default:
  107. return -EINVAL;
  108. }
  109. default:
  110. return -EINVAL;
  111. }
  112. }
  113. static const struct iio_info cm3605_info = {
  114. .read_raw = cm3605_read_raw,
  115. };
  116. static const struct iio_event_spec cm3605_events[] = {
  117. {
  118. .type = IIO_EV_TYPE_THRESH,
  119. .dir = IIO_EV_DIR_EITHER,
  120. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  121. },
  122. };
  123. static const struct iio_chan_spec cm3605_channels[] = {
  124. {
  125. .type = IIO_PROXIMITY,
  126. .event_spec = cm3605_events,
  127. .num_event_specs = ARRAY_SIZE(cm3605_events),
  128. },
  129. {
  130. .type = IIO_LIGHT,
  131. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  132. .channel = CM3605_ALS_CHANNEL,
  133. },
  134. };
  135. static int cm3605_probe(struct platform_device *pdev)
  136. {
  137. struct cm3605 *cm3605;
  138. struct iio_dev *indio_dev;
  139. struct device *dev = &pdev->dev;
  140. enum iio_chan_type ch_type;
  141. u32 rset;
  142. int irq;
  143. int ret;
  144. indio_dev = devm_iio_device_alloc(dev, sizeof(*cm3605));
  145. if (!indio_dev)
  146. return -ENOMEM;
  147. platform_set_drvdata(pdev, indio_dev);
  148. cm3605 = iio_priv(indio_dev);
  149. cm3605->dev = dev;
  150. cm3605->dir = IIO_EV_DIR_FALLING;
  151. ret = device_property_read_u32(dev, "capella,aset-resistance-ohms", &rset);
  152. if (ret) {
  153. dev_info(dev, "no RSET specified, assuming 100K\n");
  154. rset = 100000;
  155. }
  156. switch (rset) {
  157. case 50000:
  158. cm3605->als_max = 650;
  159. break;
  160. case 100000:
  161. cm3605->als_max = 300;
  162. break;
  163. case 300000:
  164. cm3605->als_max = 100;
  165. break;
  166. case 600000:
  167. cm3605->als_max = 50;
  168. break;
  169. default:
  170. dev_info(dev, "non-standard resistance\n");
  171. return -EINVAL;
  172. }
  173. cm3605->aout = devm_iio_channel_get(dev, "aout");
  174. if (IS_ERR(cm3605->aout)) {
  175. ret = PTR_ERR(cm3605->aout);
  176. ret = (ret == -ENODEV) ? -EPROBE_DEFER : ret;
  177. return dev_err_probe(dev, ret, "failed to get AOUT ADC channel\n");
  178. }
  179. ret = iio_get_channel_type(cm3605->aout, &ch_type);
  180. if (ret < 0)
  181. return ret;
  182. if (ch_type != IIO_VOLTAGE) {
  183. dev_err(dev, "wrong type of IIO channel specified for AOUT\n");
  184. return -EINVAL;
  185. }
  186. cm3605->vdd = devm_regulator_get(dev, "vdd");
  187. if (IS_ERR(cm3605->vdd))
  188. return dev_err_probe(dev, PTR_ERR(cm3605->vdd),
  189. "failed to get VDD regulator\n");
  190. ret = regulator_enable(cm3605->vdd);
  191. if (ret) {
  192. dev_err(dev, "failed to enable VDD regulator\n");
  193. return ret;
  194. }
  195. cm3605->aset = devm_gpiod_get(dev, "aset", GPIOD_OUT_HIGH);
  196. if (IS_ERR(cm3605->aset)) {
  197. ret = dev_err_probe(dev, PTR_ERR(cm3605->aset), "no ASET GPIO\n");
  198. goto out_disable_vdd;
  199. }
  200. irq = platform_get_irq(pdev, 0);
  201. if (irq < 0) {
  202. ret = dev_err_probe(dev, irq, "failed to get irq\n");
  203. goto out_disable_aset;
  204. }
  205. ret = devm_request_threaded_irq(dev, irq, cm3605_prox_irq,
  206. NULL, 0, "cm3605", indio_dev);
  207. if (ret) {
  208. dev_err(dev, "unable to request IRQ\n");
  209. goto out_disable_aset;
  210. }
  211. /* Just name the trigger the same as the driver */
  212. led_trigger_register_simple("cm3605", &cm3605->led);
  213. led_trigger_event(cm3605->led, LED_FULL);
  214. indio_dev->info = &cm3605_info;
  215. indio_dev->name = "cm3605";
  216. indio_dev->channels = cm3605_channels;
  217. indio_dev->num_channels = ARRAY_SIZE(cm3605_channels);
  218. indio_dev->modes = INDIO_DIRECT_MODE;
  219. ret = iio_device_register(indio_dev);
  220. if (ret)
  221. goto out_remove_trigger;
  222. dev_info(dev, "Capella Microsystems CM3605 enabled range 0..%d LUX\n",
  223. cm3605->als_max);
  224. return 0;
  225. out_remove_trigger:
  226. led_trigger_event(cm3605->led, LED_OFF);
  227. led_trigger_unregister_simple(cm3605->led);
  228. out_disable_aset:
  229. gpiod_set_value_cansleep(cm3605->aset, 0);
  230. out_disable_vdd:
  231. regulator_disable(cm3605->vdd);
  232. return ret;
  233. }
  234. static int cm3605_remove(struct platform_device *pdev)
  235. {
  236. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  237. struct cm3605 *cm3605 = iio_priv(indio_dev);
  238. led_trigger_event(cm3605->led, LED_OFF);
  239. led_trigger_unregister_simple(cm3605->led);
  240. gpiod_set_value_cansleep(cm3605->aset, 0);
  241. iio_device_unregister(indio_dev);
  242. regulator_disable(cm3605->vdd);
  243. return 0;
  244. }
  245. static int cm3605_pm_suspend(struct device *dev)
  246. {
  247. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  248. struct cm3605 *cm3605 = iio_priv(indio_dev);
  249. led_trigger_event(cm3605->led, LED_OFF);
  250. regulator_disable(cm3605->vdd);
  251. return 0;
  252. }
  253. static int cm3605_pm_resume(struct device *dev)
  254. {
  255. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  256. struct cm3605 *cm3605 = iio_priv(indio_dev);
  257. int ret;
  258. ret = regulator_enable(cm3605->vdd);
  259. if (ret)
  260. dev_err(dev, "failed to enable regulator in resume path\n");
  261. led_trigger_event(cm3605->led, LED_FULL);
  262. return 0;
  263. }
  264. static DEFINE_SIMPLE_DEV_PM_OPS(cm3605_dev_pm_ops, cm3605_pm_suspend,
  265. cm3605_pm_resume);
  266. static const struct of_device_id cm3605_of_match[] = {
  267. {.compatible = "capella,cm3605"},
  268. { },
  269. };
  270. MODULE_DEVICE_TABLE(of, cm3605_of_match);
  271. static struct platform_driver cm3605_driver = {
  272. .driver = {
  273. .name = "cm3605",
  274. .of_match_table = cm3605_of_match,
  275. .pm = pm_sleep_ptr(&cm3605_dev_pm_ops),
  276. },
  277. .probe = cm3605_probe,
  278. .remove = cm3605_remove,
  279. };
  280. module_platform_driver(cm3605_driver);
  281. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  282. MODULE_DESCRIPTION("CM3605 ambient light and proximity sensor driver");
  283. MODULE_LICENSE("GPL");