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