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