cm3605.c 8.0 KB

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