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