xref: /linux/drivers/iio/light/apds9960.c (revision 50e5bae0c1f976e881eecf4d1ad70ce1593021c9)
1d6ad8058SMatt Ranostay // SPDX-License-Identifier: GPL-2.0+
2aff268cdSMatt Ranostay /*
3aff268cdSMatt Ranostay  * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor
4aff268cdSMatt Ranostay  *
5d6ad8058SMatt Ranostay  * Copyright (C) 2015, 2018
6d6ad8058SMatt Ranostay  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7aff268cdSMatt Ranostay  *
8aff268cdSMatt Ranostay  * TODO: gesture + proximity calib offsets
9aff268cdSMatt Ranostay  */
10aff268cdSMatt Ranostay 
11aff268cdSMatt Ranostay #include <linux/module.h>
12aff268cdSMatt Ranostay #include <linux/init.h>
13aff268cdSMatt Ranostay #include <linux/interrupt.h>
14aff268cdSMatt Ranostay #include <linux/delay.h>
15aff268cdSMatt Ranostay #include <linux/mutex.h>
16aff268cdSMatt Ranostay #include <linux/err.h>
17aff268cdSMatt Ranostay #include <linux/irq.h>
18aff268cdSMatt Ranostay #include <linux/gpio.h>
19aff268cdSMatt Ranostay #include <linux/i2c.h>
20aff268cdSMatt Ranostay #include <linux/pm_runtime.h>
21aff268cdSMatt Ranostay #include <linux/regmap.h>
22aff268cdSMatt Ranostay #include <linux/iio/iio.h>
23aff268cdSMatt Ranostay #include <linux/iio/buffer.h>
24aff268cdSMatt Ranostay #include <linux/iio/events.h>
25aff268cdSMatt Ranostay #include <linux/iio/kfifo_buf.h>
26aff268cdSMatt Ranostay #include <linux/iio/sysfs.h>
27aff268cdSMatt Ranostay #include <linux/of_gpio.h>
28aff268cdSMatt Ranostay 
29aff268cdSMatt Ranostay #define APDS9960_REGMAP_NAME	"apds9960_regmap"
30aff268cdSMatt Ranostay #define APDS9960_DRV_NAME	"apds9960"
31aff268cdSMatt Ranostay 
32aff268cdSMatt Ranostay #define APDS9960_REG_RAM_START	0x00
33aff268cdSMatt Ranostay #define APDS9960_REG_RAM_END	0x7f
34aff268cdSMatt Ranostay 
35aff268cdSMatt Ranostay #define APDS9960_REG_ENABLE	0x80
36aff268cdSMatt Ranostay #define APDS9960_REG_ATIME	0x81
37aff268cdSMatt Ranostay #define APDS9960_REG_WTIME	0x83
38aff268cdSMatt Ranostay 
39aff268cdSMatt Ranostay #define APDS9960_REG_AILTL	0x84
40aff268cdSMatt Ranostay #define APDS9960_REG_AILTH	0x85
41aff268cdSMatt Ranostay #define APDS9960_REG_AIHTL	0x86
42aff268cdSMatt Ranostay #define APDS9960_REG_AIHTH	0x87
43aff268cdSMatt Ranostay 
44aff268cdSMatt Ranostay #define APDS9960_REG_PILT	0x89
45aff268cdSMatt Ranostay #define APDS9960_REG_PIHT	0x8b
46aff268cdSMatt Ranostay #define APDS9960_REG_PERS	0x8c
47aff268cdSMatt Ranostay 
48aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_1	0x8d
49aff268cdSMatt Ranostay #define APDS9960_REG_PPULSE	0x8e
50aff268cdSMatt Ranostay 
51aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL	0x8f
52aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_AGAIN_MASK		0x03
53aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_PGAIN_MASK		0x0c
54aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT	0
55aff268cdSMatt Ranostay #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT	2
56aff268cdSMatt Ranostay 
57aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2	0x90
58aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2_GGAIN_MASK	0x60
59aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT	5
60aff268cdSMatt Ranostay 
61aff268cdSMatt Ranostay #define APDS9960_REG_ID		0x92
62aff268cdSMatt Ranostay 
63aff268cdSMatt Ranostay #define APDS9960_REG_STATUS	0x93
64aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_PS_INT	BIT(5)
65aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_ALS_INT	BIT(4)
66aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_GINT	BIT(2)
67aff268cdSMatt Ranostay 
68aff268cdSMatt Ranostay #define APDS9960_REG_PDATA	0x9c
69aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_UR	0x9d
70aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_DL 0x9e
71aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_3	0x9f
72aff268cdSMatt Ranostay 
73aff268cdSMatt Ranostay #define APDS9960_REG_GPENTH	0xa0
74aff268cdSMatt Ranostay #define APDS9960_REG_GEXTH	0xa1
75aff268cdSMatt Ranostay 
76aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1	0xa2
77aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK		0xc0
78aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT	6
79aff268cdSMatt Ranostay 
80aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_2	0xa3
81aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_U	0xa4
82aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_D	0xa5
83aff268cdSMatt Ranostay #define APDS9960_REG_GPULSE	0xa6
84aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_L	0xa7
85aff268cdSMatt Ranostay #define APDS9960_REG_GOFFSET_R	0xa9
86aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_3	0xaa
87aff268cdSMatt Ranostay 
88aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_4	0xab
89aff268cdSMatt Ranostay #define APDS9960_REG_GFLVL	0xae
90aff268cdSMatt Ranostay #define APDS9960_REG_GSTATUS	0xaf
91aff268cdSMatt Ranostay 
92aff268cdSMatt Ranostay #define APDS9960_REG_IFORCE	0xe4
93aff268cdSMatt Ranostay #define APDS9960_REG_PICLEAR	0xe5
94aff268cdSMatt Ranostay #define APDS9960_REG_CICLEAR	0xe6
95aff268cdSMatt Ranostay #define APDS9960_REG_AICLEAR	0xe7
96aff268cdSMatt Ranostay 
97aff268cdSMatt Ranostay #define APDS9960_DEFAULT_PERS	0x33
98aff268cdSMatt Ranostay #define APDS9960_DEFAULT_GPENTH	0x50
99aff268cdSMatt Ranostay #define APDS9960_DEFAULT_GEXTH	0x40
100aff268cdSMatt Ranostay 
101aff268cdSMatt Ranostay #define APDS9960_MAX_PXS_THRES_VAL	255
102aff268cdSMatt Ranostay #define APDS9960_MAX_ALS_THRES_VAL	0xffff
103aff268cdSMatt Ranostay #define APDS9960_MAX_INT_TIME_IN_US	1000000
104aff268cdSMatt Ranostay 
105aff268cdSMatt Ranostay enum apds9960_als_channel_idx {
106aff268cdSMatt Ranostay 	IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
107aff268cdSMatt Ranostay };
108aff268cdSMatt Ranostay 
109aff268cdSMatt Ranostay #define APDS9960_REG_ALS_BASE	0x94
110aff268cdSMatt Ranostay #define APDS9960_REG_ALS_CHANNEL(_colour) \
111aff268cdSMatt Ranostay 	(APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
112aff268cdSMatt Ranostay 
113aff268cdSMatt Ranostay enum apds9960_gesture_channel_idx {
114aff268cdSMatt Ranostay 	IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
115aff268cdSMatt Ranostay };
116aff268cdSMatt Ranostay 
117aff268cdSMatt Ranostay #define APDS9960_REG_GFIFO_BASE	0xfc
118aff268cdSMatt Ranostay #define APDS9960_REG_GFIFO_DIR(_dir) \
119aff268cdSMatt Ranostay 	(APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
120aff268cdSMatt Ranostay 
121aff268cdSMatt Ranostay struct apds9960_data {
122aff268cdSMatt Ranostay 	struct i2c_client *client;
123aff268cdSMatt Ranostay 	struct iio_dev *indio_dev;
124aff268cdSMatt Ranostay 	struct mutex lock;
125aff268cdSMatt Ranostay 
126aff268cdSMatt Ranostay 	/* regmap fields */
127aff268cdSMatt Ranostay 	struct regmap *regmap;
128aff268cdSMatt Ranostay 	struct regmap_field *reg_int_als;
129aff268cdSMatt Ranostay 	struct regmap_field *reg_int_ges;
130aff268cdSMatt Ranostay 	struct regmap_field *reg_int_pxs;
131aff268cdSMatt Ranostay 
132aff268cdSMatt Ranostay 	struct regmap_field *reg_enable_als;
133aff268cdSMatt Ranostay 	struct regmap_field *reg_enable_ges;
134aff268cdSMatt Ranostay 	struct regmap_field *reg_enable_pxs;
135aff268cdSMatt Ranostay 
136aff268cdSMatt Ranostay 	/* state */
137aff268cdSMatt Ranostay 	int als_int;
138aff268cdSMatt Ranostay 	int pxs_int;
139aff268cdSMatt Ranostay 	int gesture_mode_running;
140aff268cdSMatt Ranostay 
141aff268cdSMatt Ranostay 	/* gain values */
142aff268cdSMatt Ranostay 	int als_gain;
143aff268cdSMatt Ranostay 	int pxs_gain;
144aff268cdSMatt Ranostay 
145aff268cdSMatt Ranostay 	/* integration time value in us */
146aff268cdSMatt Ranostay 	int als_adc_int_us;
147aff268cdSMatt Ranostay 
148aff268cdSMatt Ranostay 	/* gesture buffer */
149aff268cdSMatt Ranostay 	u8 buffer[4]; /* 4 8-bit channels */
150aff268cdSMatt Ranostay };
151aff268cdSMatt Ranostay 
152aff268cdSMatt Ranostay static const struct reg_default apds9960_reg_defaults[] = {
153aff268cdSMatt Ranostay 	/* Default ALS integration time = 2.48ms */
154aff268cdSMatt Ranostay 	{ APDS9960_REG_ATIME, 0xff },
155aff268cdSMatt Ranostay };
156aff268cdSMatt Ranostay 
157aff268cdSMatt Ranostay static const struct regmap_range apds9960_volatile_ranges[] = {
158aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_STATUS,
159aff268cdSMatt Ranostay 				APDS9960_REG_PDATA),
160aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFLVL,
161aff268cdSMatt Ranostay 				APDS9960_REG_GSTATUS),
162aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
163aff268cdSMatt Ranostay 				APDS9960_REG_GFIFO_DIR(RIGHT)),
164aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_IFORCE,
165aff268cdSMatt Ranostay 				APDS9960_REG_AICLEAR),
166aff268cdSMatt Ranostay };
167aff268cdSMatt Ranostay 
168aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_volatile_table = {
169aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_volatile_ranges,
170aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_volatile_ranges),
171aff268cdSMatt Ranostay };
172aff268cdSMatt Ranostay 
173aff268cdSMatt Ranostay static const struct regmap_range apds9960_precious_ranges[] = {
174aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
175aff268cdSMatt Ranostay };
176aff268cdSMatt Ranostay 
177aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_precious_table = {
178aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_precious_ranges,
179aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_precious_ranges),
180aff268cdSMatt Ranostay };
181aff268cdSMatt Ranostay 
182aff268cdSMatt Ranostay static const struct regmap_range apds9960_readable_ranges[] = {
183aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_ENABLE,
184aff268cdSMatt Ranostay 				APDS9960_REG_GSTATUS),
185aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
186aff268cdSMatt Ranostay 				APDS9960_REG_GFIFO_DIR(RIGHT)),
187aff268cdSMatt Ranostay };
188aff268cdSMatt Ranostay 
189aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_readable_table = {
190aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_readable_ranges,
191aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_readable_ranges),
192aff268cdSMatt Ranostay };
193aff268cdSMatt Ranostay 
194aff268cdSMatt Ranostay static const struct regmap_range apds9960_writeable_ranges[] = {
195aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
196aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
197aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
198aff268cdSMatt Ranostay };
199aff268cdSMatt Ranostay 
200aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_writeable_table = {
201aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_writeable_ranges,
202aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_writeable_ranges),
203aff268cdSMatt Ranostay };
204aff268cdSMatt Ranostay 
205aff268cdSMatt Ranostay static const struct regmap_config apds9960_regmap_config = {
206aff268cdSMatt Ranostay 	.name = APDS9960_REGMAP_NAME,
207aff268cdSMatt Ranostay 	.reg_bits = 8,
208aff268cdSMatt Ranostay 	.val_bits = 8,
2091c96a2f6SDavid Frey 	.use_single_read = true,
2101c96a2f6SDavid Frey 	.use_single_write = true,
211aff268cdSMatt Ranostay 
212aff268cdSMatt Ranostay 	.volatile_table = &apds9960_volatile_table,
213aff268cdSMatt Ranostay 	.precious_table = &apds9960_precious_table,
214aff268cdSMatt Ranostay 	.rd_table = &apds9960_readable_table,
215aff268cdSMatt Ranostay 	.wr_table = &apds9960_writeable_table,
216aff268cdSMatt Ranostay 
217aff268cdSMatt Ranostay 	.reg_defaults = apds9960_reg_defaults,
218aff268cdSMatt Ranostay 	.num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
219aff268cdSMatt Ranostay 	.max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
220aff268cdSMatt Ranostay 	.cache_type = REGCACHE_RBTREE,
221aff268cdSMatt Ranostay };
222aff268cdSMatt Ranostay 
223aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_pxs_event_spec[] = {
224aff268cdSMatt Ranostay 	{
225aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
226aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_RISING,
227aff268cdSMatt Ranostay 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
228aff268cdSMatt Ranostay 			BIT(IIO_EV_INFO_ENABLE),
229aff268cdSMatt Ranostay 	},
230aff268cdSMatt Ranostay 	{
231aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
232aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_FALLING,
233aff268cdSMatt Ranostay 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
234aff268cdSMatt Ranostay 			BIT(IIO_EV_INFO_ENABLE),
235aff268cdSMatt Ranostay 	},
236aff268cdSMatt Ranostay };
237aff268cdSMatt Ranostay 
238aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_als_event_spec[] = {
239aff268cdSMatt Ranostay 	{
240aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
241aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_RISING,
242aff268cdSMatt Ranostay 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
243aff268cdSMatt Ranostay 			BIT(IIO_EV_INFO_ENABLE),
244aff268cdSMatt Ranostay 	},
245aff268cdSMatt Ranostay 	{
246aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
247aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_FALLING,
248aff268cdSMatt Ranostay 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
249aff268cdSMatt Ranostay 			BIT(IIO_EV_INFO_ENABLE),
250aff268cdSMatt Ranostay 	},
251aff268cdSMatt Ranostay };
252aff268cdSMatt Ranostay 
253aff268cdSMatt Ranostay #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
254aff268cdSMatt Ranostay 	.type = IIO_PROXIMITY, \
255aff268cdSMatt Ranostay 	.channel = _si + 1, \
256aff268cdSMatt Ranostay 	.scan_index = _si, \
257aff268cdSMatt Ranostay 	.indexed = 1, \
258aff268cdSMatt Ranostay 	.scan_type = { \
259aff268cdSMatt Ranostay 		.sign = 'u', \
260aff268cdSMatt Ranostay 		.realbits = 8, \
261aff268cdSMatt Ranostay 		.storagebits = 8, \
262aff268cdSMatt Ranostay 	}, \
263aff268cdSMatt Ranostay }
264aff268cdSMatt Ranostay 
265aff268cdSMatt Ranostay #define APDS9960_INTENSITY_CHANNEL(_colour) { \
266aff268cdSMatt Ranostay 	.type = IIO_INTENSITY, \
267aff268cdSMatt Ranostay 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
268aff268cdSMatt Ranostay 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
269aff268cdSMatt Ranostay 			BIT(IIO_CHAN_INFO_INT_TIME), \
270aff268cdSMatt Ranostay 	.channel2 = IIO_MOD_LIGHT_##_colour, \
271aff268cdSMatt Ranostay 	.address = APDS9960_REG_ALS_CHANNEL(_colour), \
272aff268cdSMatt Ranostay 	.modified = 1, \
273aff268cdSMatt Ranostay 	.scan_index = -1, \
274aff268cdSMatt Ranostay }
275aff268cdSMatt Ranostay 
276aff268cdSMatt Ranostay static const unsigned long apds9960_scan_masks[] = {0xf, 0};
277aff268cdSMatt Ranostay 
278aff268cdSMatt Ranostay static const struct iio_chan_spec apds9960_channels[] = {
279aff268cdSMatt Ranostay 	{
280aff268cdSMatt Ranostay 		.type = IIO_PROXIMITY,
281aff268cdSMatt Ranostay 		.address = APDS9960_REG_PDATA,
282aff268cdSMatt Ranostay 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
283aff268cdSMatt Ranostay 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
284aff268cdSMatt Ranostay 		.channel = 0,
285aff268cdSMatt Ranostay 		.indexed = 0,
286aff268cdSMatt Ranostay 		.scan_index = -1,
287aff268cdSMatt Ranostay 
288aff268cdSMatt Ranostay 		.event_spec = apds9960_pxs_event_spec,
289aff268cdSMatt Ranostay 		.num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
290aff268cdSMatt Ranostay 	},
291aff268cdSMatt Ranostay 	/* Gesture Sensor */
292aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(UP, 0),
293aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(DOWN, 1),
294aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(LEFT, 2),
295aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(RIGHT, 3),
296aff268cdSMatt Ranostay 	/* ALS */
297aff268cdSMatt Ranostay 	{
298aff268cdSMatt Ranostay 		.type = IIO_INTENSITY,
299aff268cdSMatt Ranostay 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
300aff268cdSMatt Ranostay 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
301aff268cdSMatt Ranostay 			BIT(IIO_CHAN_INFO_INT_TIME),
302aff268cdSMatt Ranostay 		.channel2 = IIO_MOD_LIGHT_CLEAR,
303aff268cdSMatt Ranostay 		.address = APDS9960_REG_ALS_CHANNEL(CLEAR),
304aff268cdSMatt Ranostay 		.modified = 1,
305aff268cdSMatt Ranostay 		.scan_index = -1,
306aff268cdSMatt Ranostay 
307aff268cdSMatt Ranostay 		.event_spec = apds9960_als_event_spec,
308aff268cdSMatt Ranostay 		.num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
309aff268cdSMatt Ranostay 	},
310aff268cdSMatt Ranostay 	/* RGB Sensor */
311aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(RED),
312aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(GREEN),
313aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(BLUE),
314aff268cdSMatt Ranostay };
315aff268cdSMatt Ranostay 
316aff268cdSMatt Ranostay /* integration time in us */
317ebe16806SSlawomir Stepien static const int apds9960_int_time[][2] = {
318ebe16806SSlawomir Stepien 	{ 28000, 246},
319ebe16806SSlawomir Stepien 	{100000, 219},
320ebe16806SSlawomir Stepien 	{200000, 182},
321ebe16806SSlawomir Stepien 	{700000,   0}
322ebe16806SSlawomir Stepien };
323aff268cdSMatt Ranostay 
324aff268cdSMatt Ranostay /* gain mapping */
325aff268cdSMatt Ranostay static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
326aff268cdSMatt Ranostay static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
327aff268cdSMatt Ranostay 
328aff268cdSMatt Ranostay static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8");
329aff268cdSMatt Ranostay static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64");
330aff268cdSMatt Ranostay static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
331aff268cdSMatt Ranostay 
332aff268cdSMatt Ranostay static struct attribute *apds9960_attributes[] = {
333aff268cdSMatt Ranostay 	&iio_const_attr_proximity_scale_available.dev_attr.attr,
334aff268cdSMatt Ranostay 	&iio_const_attr_intensity_scale_available.dev_attr.attr,
335aff268cdSMatt Ranostay 	&iio_const_attr_integration_time_available.dev_attr.attr,
336aff268cdSMatt Ranostay 	NULL,
337aff268cdSMatt Ranostay };
338aff268cdSMatt Ranostay 
3393ca437c0Ssimran singhal static const struct attribute_group apds9960_attribute_group = {
340aff268cdSMatt Ranostay 	.attrs = apds9960_attributes,
341aff268cdSMatt Ranostay };
342aff268cdSMatt Ranostay 
343aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_als =
344aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
345aff268cdSMatt Ranostay 
346aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_ges =
347aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
348aff268cdSMatt Ranostay 
349aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_pxs =
350aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
351aff268cdSMatt Ranostay 
352aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_als =
353aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
354aff268cdSMatt Ranostay 
355aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_ges =
356aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
357aff268cdSMatt Ranostay 
358aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_pxs =
359aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
360aff268cdSMatt Ranostay 
361aff268cdSMatt Ranostay static int apds9960_set_it_time(struct apds9960_data *data, int val2)
362aff268cdSMatt Ranostay {
363aff268cdSMatt Ranostay 	int ret = -EINVAL;
364aff268cdSMatt Ranostay 	int idx;
365aff268cdSMatt Ranostay 
366aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) {
367aff268cdSMatt Ranostay 		if (apds9960_int_time[idx][0] == val2) {
368aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
369aff268cdSMatt Ranostay 			ret = regmap_write(data->regmap, APDS9960_REG_ATIME,
370aff268cdSMatt Ranostay 						 apds9960_int_time[idx][1]);
371aff268cdSMatt Ranostay 			if (!ret)
372aff268cdSMatt Ranostay 				data->als_adc_int_us = val2;
373aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
374aff268cdSMatt Ranostay 			break;
375aff268cdSMatt Ranostay 		}
376aff268cdSMatt Ranostay 	}
377aff268cdSMatt Ranostay 
378aff268cdSMatt Ranostay 	return ret;
379aff268cdSMatt Ranostay }
380aff268cdSMatt Ranostay 
381aff268cdSMatt Ranostay static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
382aff268cdSMatt Ranostay {
383aff268cdSMatt Ranostay 	int ret = -EINVAL;
384aff268cdSMatt Ranostay 	int idx;
385aff268cdSMatt Ranostay 
386aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) {
387aff268cdSMatt Ranostay 		if (apds9960_pxs_gain_map[idx] == val) {
388aff268cdSMatt Ranostay 			/* pxs + gesture gains are mirrored */
389aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
390aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
391aff268cdSMatt Ranostay 				APDS9960_REG_CONTROL,
392aff268cdSMatt Ranostay 				APDS9960_REG_CONTROL_PGAIN_MASK,
393aff268cdSMatt Ranostay 				idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT);
394aff268cdSMatt Ranostay 			if (ret) {
395aff268cdSMatt Ranostay 				mutex_unlock(&data->lock);
396aff268cdSMatt Ranostay 				break;
397aff268cdSMatt Ranostay 			}
398aff268cdSMatt Ranostay 
399aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
400aff268cdSMatt Ranostay 				APDS9960_REG_CONFIG_2,
401aff268cdSMatt Ranostay 				APDS9960_REG_CONFIG_2_GGAIN_MASK,
402aff268cdSMatt Ranostay 				idx << APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT);
403aff268cdSMatt Ranostay 			if (!ret)
404aff268cdSMatt Ranostay 				data->pxs_gain = idx;
405aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
406aff268cdSMatt Ranostay 			break;
407aff268cdSMatt Ranostay 		}
408aff268cdSMatt Ranostay 	}
409aff268cdSMatt Ranostay 
410aff268cdSMatt Ranostay 	return ret;
411aff268cdSMatt Ranostay }
412aff268cdSMatt Ranostay 
413aff268cdSMatt Ranostay static int apds9960_set_als_gain(struct apds9960_data *data, int val)
414aff268cdSMatt Ranostay {
415aff268cdSMatt Ranostay 	int ret = -EINVAL;
416aff268cdSMatt Ranostay 	int idx;
417aff268cdSMatt Ranostay 
418aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) {
419aff268cdSMatt Ranostay 		if (apds9960_als_gain_map[idx] == val) {
420aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
421aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
422aff268cdSMatt Ranostay 					APDS9960_REG_CONTROL,
423aff268cdSMatt Ranostay 					APDS9960_REG_CONTROL_AGAIN_MASK, idx);
424aff268cdSMatt Ranostay 			if (!ret)
425aff268cdSMatt Ranostay 				data->als_gain = idx;
426aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
427aff268cdSMatt Ranostay 			break;
428aff268cdSMatt Ranostay 		}
429aff268cdSMatt Ranostay 	}
430aff268cdSMatt Ranostay 
431aff268cdSMatt Ranostay 	return ret;
432aff268cdSMatt Ranostay }
433aff268cdSMatt Ranostay 
434aff268cdSMatt Ranostay #ifdef CONFIG_PM
435aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on)
436aff268cdSMatt Ranostay {
437aff268cdSMatt Ranostay 	struct device *dev = &data->client->dev;
438aff268cdSMatt Ranostay 	int ret = 0;
439aff268cdSMatt Ranostay 
440aff268cdSMatt Ranostay 	mutex_lock(&data->lock);
441aff268cdSMatt Ranostay 
442aff268cdSMatt Ranostay 	if (on) {
443aff268cdSMatt Ranostay 		int suspended;
444aff268cdSMatt Ranostay 
445aff268cdSMatt Ranostay 		suspended = pm_runtime_suspended(dev);
446aff268cdSMatt Ranostay 		ret = pm_runtime_get_sync(dev);
447aff268cdSMatt Ranostay 
448aff268cdSMatt Ranostay 		/* Allow one integration cycle before allowing a reading */
449aff268cdSMatt Ranostay 		if (suspended)
450aff268cdSMatt Ranostay 			usleep_range(data->als_adc_int_us,
451aff268cdSMatt Ranostay 				     APDS9960_MAX_INT_TIME_IN_US);
452aff268cdSMatt Ranostay 	} else {
4538386c275SMatt Ranostay 		pm_runtime_mark_last_busy(dev);
454aff268cdSMatt Ranostay 		ret = pm_runtime_put_autosuspend(dev);
455aff268cdSMatt Ranostay 	}
456aff268cdSMatt Ranostay 
457aff268cdSMatt Ranostay 	mutex_unlock(&data->lock);
458aff268cdSMatt Ranostay 
459aff268cdSMatt Ranostay 	return ret;
460aff268cdSMatt Ranostay }
461aff268cdSMatt Ranostay #else
462aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on)
463aff268cdSMatt Ranostay {
464aff268cdSMatt Ranostay 	return 0;
465aff268cdSMatt Ranostay }
466aff268cdSMatt Ranostay #endif
467aff268cdSMatt Ranostay 
468aff268cdSMatt Ranostay static int apds9960_read_raw(struct iio_dev *indio_dev,
469aff268cdSMatt Ranostay 			     struct iio_chan_spec const *chan,
470aff268cdSMatt Ranostay 			     int *val, int *val2, long mask)
471aff268cdSMatt Ranostay {
472aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
473801ab335SJonathan Cameron 	__le16 buf;
474aff268cdSMatt Ranostay 	int ret = -EINVAL;
475aff268cdSMatt Ranostay 
476aff268cdSMatt Ranostay 	if (data->gesture_mode_running)
477aff268cdSMatt Ranostay 		return -EBUSY;
478aff268cdSMatt Ranostay 
479aff268cdSMatt Ranostay 	switch (mask) {
480aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_RAW:
481aff268cdSMatt Ranostay 		apds9960_set_power_state(data, true);
482aff268cdSMatt Ranostay 		switch (chan->type) {
483aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
484aff268cdSMatt Ranostay 			ret = regmap_read(data->regmap, chan->address, val);
485aff268cdSMatt Ranostay 			if (!ret)
486aff268cdSMatt Ranostay 				ret = IIO_VAL_INT;
487aff268cdSMatt Ranostay 			break;
488aff268cdSMatt Ranostay 		case IIO_INTENSITY:
489aff268cdSMatt Ranostay 			ret = regmap_bulk_read(data->regmap, chan->address,
490aff268cdSMatt Ranostay 					       &buf, 2);
491f2112259SDan Carpenter 			if (!ret) {
492aff268cdSMatt Ranostay 				ret = IIO_VAL_INT;
493aff268cdSMatt Ranostay 				*val = le16_to_cpu(buf);
494f2112259SDan Carpenter 			}
495aff268cdSMatt Ranostay 			break;
496aff268cdSMatt Ranostay 		default:
497aff268cdSMatt Ranostay 			ret = -EINVAL;
498aff268cdSMatt Ranostay 		}
499aff268cdSMatt Ranostay 		apds9960_set_power_state(data, false);
500aff268cdSMatt Ranostay 		break;
501aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_INT_TIME:
502aff268cdSMatt Ranostay 		/* RGB + ALS sensors only have integration time */
503aff268cdSMatt Ranostay 		mutex_lock(&data->lock);
504aff268cdSMatt Ranostay 		switch (chan->type) {
505aff268cdSMatt Ranostay 		case IIO_INTENSITY:
506aff268cdSMatt Ranostay 			*val = 0;
507aff268cdSMatt Ranostay 			*val2 = data->als_adc_int_us;
508aff268cdSMatt Ranostay 			ret = IIO_VAL_INT_PLUS_MICRO;
509aff268cdSMatt Ranostay 			break;
510aff268cdSMatt Ranostay 		default:
511aff268cdSMatt Ranostay 			ret = -EINVAL;
512aff268cdSMatt Ranostay 		}
513aff268cdSMatt Ranostay 		mutex_unlock(&data->lock);
514aff268cdSMatt Ranostay 		break;
515aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_SCALE:
516aff268cdSMatt Ranostay 		mutex_lock(&data->lock);
517aff268cdSMatt Ranostay 		switch (chan->type) {
518aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
519aff268cdSMatt Ranostay 			*val = apds9960_pxs_gain_map[data->pxs_gain];
520aff268cdSMatt Ranostay 			ret = IIO_VAL_INT;
521aff268cdSMatt Ranostay 			break;
522aff268cdSMatt Ranostay 		case IIO_INTENSITY:
523aff268cdSMatt Ranostay 			*val = apds9960_als_gain_map[data->als_gain];
524aff268cdSMatt Ranostay 			ret = IIO_VAL_INT;
525aff268cdSMatt Ranostay 			break;
526aff268cdSMatt Ranostay 		default:
527aff268cdSMatt Ranostay 			ret = -EINVAL;
528aff268cdSMatt Ranostay 		}
529aff268cdSMatt Ranostay 		mutex_unlock(&data->lock);
530aff268cdSMatt Ranostay 		break;
531aff268cdSMatt Ranostay 	}
532aff268cdSMatt Ranostay 
533aff268cdSMatt Ranostay 	return ret;
534aff268cdSMatt Ranostay };
535aff268cdSMatt Ranostay 
536aff268cdSMatt Ranostay static int apds9960_write_raw(struct iio_dev *indio_dev,
537aff268cdSMatt Ranostay 			     struct iio_chan_spec const *chan,
538aff268cdSMatt Ranostay 			     int val, int val2, long mask)
539aff268cdSMatt Ranostay {
540aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
541aff268cdSMatt Ranostay 
542aff268cdSMatt Ranostay 	switch (mask) {
543aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_INT_TIME:
544aff268cdSMatt Ranostay 		/* RGB + ALS sensors only have int time */
545aff268cdSMatt Ranostay 		switch (chan->type) {
546aff268cdSMatt Ranostay 		case IIO_INTENSITY:
547aff268cdSMatt Ranostay 			if (val != 0)
548aff268cdSMatt Ranostay 				return -EINVAL;
549aff268cdSMatt Ranostay 			return apds9960_set_it_time(data, val2);
550aff268cdSMatt Ranostay 		default:
551aff268cdSMatt Ranostay 			return -EINVAL;
552aff268cdSMatt Ranostay 		}
553aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_SCALE:
554aff268cdSMatt Ranostay 		if (val2 != 0)
555aff268cdSMatt Ranostay 			return -EINVAL;
556aff268cdSMatt Ranostay 		switch (chan->type) {
557aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
558aff268cdSMatt Ranostay 			return apds9960_set_pxs_gain(data, val);
559aff268cdSMatt Ranostay 		case IIO_INTENSITY:
560aff268cdSMatt Ranostay 			return apds9960_set_als_gain(data, val);
561aff268cdSMatt Ranostay 		default:
562aff268cdSMatt Ranostay 			return -EINVAL;
563aff268cdSMatt Ranostay 		}
564aff268cdSMatt Ranostay 	default:
565aff268cdSMatt Ranostay 		return -EINVAL;
566aff268cdSMatt Ranostay 	};
567aff268cdSMatt Ranostay 
568aff268cdSMatt Ranostay 	return 0;
569aff268cdSMatt Ranostay }
570aff268cdSMatt Ranostay 
571aff268cdSMatt Ranostay static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
572aff268cdSMatt Ranostay 					 enum iio_event_direction dir,
573aff268cdSMatt Ranostay 					 u8 *reg)
574aff268cdSMatt Ranostay {
575aff268cdSMatt Ranostay 	switch (dir) {
576aff268cdSMatt Ranostay 	case IIO_EV_DIR_RISING:
577aff268cdSMatt Ranostay 		switch (chan->type) {
578aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
579aff268cdSMatt Ranostay 			*reg = APDS9960_REG_PIHT;
580aff268cdSMatt Ranostay 			break;
581aff268cdSMatt Ranostay 		case IIO_INTENSITY:
582aff268cdSMatt Ranostay 			*reg = APDS9960_REG_AIHTL;
583aff268cdSMatt Ranostay 			break;
584aff268cdSMatt Ranostay 		default:
585aff268cdSMatt Ranostay 			return -EINVAL;
586aff268cdSMatt Ranostay 		}
587aff268cdSMatt Ranostay 		break;
588aff268cdSMatt Ranostay 	case IIO_EV_DIR_FALLING:
589aff268cdSMatt Ranostay 		switch (chan->type) {
590aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
591aff268cdSMatt Ranostay 			*reg = APDS9960_REG_PILT;
592aff268cdSMatt Ranostay 			break;
593aff268cdSMatt Ranostay 		case IIO_INTENSITY:
594aff268cdSMatt Ranostay 			*reg = APDS9960_REG_AILTL;
595aff268cdSMatt Ranostay 			break;
596aff268cdSMatt Ranostay 		default:
597aff268cdSMatt Ranostay 			return -EINVAL;
598aff268cdSMatt Ranostay 		}
599aff268cdSMatt Ranostay 		break;
600aff268cdSMatt Ranostay 	default:
601aff268cdSMatt Ranostay 		return -EINVAL;
602aff268cdSMatt Ranostay 	}
603aff268cdSMatt Ranostay 
604aff268cdSMatt Ranostay 	return 0;
605aff268cdSMatt Ranostay }
606aff268cdSMatt Ranostay 
607aff268cdSMatt Ranostay static int apds9960_read_event(struct iio_dev *indio_dev,
608aff268cdSMatt Ranostay 			       const struct iio_chan_spec *chan,
609aff268cdSMatt Ranostay 			       enum iio_event_type type,
610aff268cdSMatt Ranostay 			       enum iio_event_direction dir,
611aff268cdSMatt Ranostay 			       enum iio_event_info info,
612aff268cdSMatt Ranostay 			       int *val, int *val2)
613aff268cdSMatt Ranostay {
614aff268cdSMatt Ranostay 	u8 reg;
615801ab335SJonathan Cameron 	__le16 buf;
616aff268cdSMatt Ranostay 	int ret = 0;
617aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
618aff268cdSMatt Ranostay 
619aff268cdSMatt Ranostay 	if (info != IIO_EV_INFO_VALUE)
620aff268cdSMatt Ranostay 		return -EINVAL;
621aff268cdSMatt Ranostay 
622aff268cdSMatt Ranostay 	ret = apds9960_get_thres_reg(chan, dir, &reg);
623aff268cdSMatt Ranostay 	if (ret < 0)
624aff268cdSMatt Ranostay 		return ret;
625aff268cdSMatt Ranostay 
626aff268cdSMatt Ranostay 	if (chan->type == IIO_PROXIMITY) {
627aff268cdSMatt Ranostay 		ret = regmap_read(data->regmap, reg, val);
628aff268cdSMatt Ranostay 		if (ret < 0)
629aff268cdSMatt Ranostay 			return ret;
630aff268cdSMatt Ranostay 	} else if (chan->type == IIO_INTENSITY) {
631aff268cdSMatt Ranostay 		ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
632aff268cdSMatt Ranostay 		if (ret < 0)
633aff268cdSMatt Ranostay 			return ret;
634aff268cdSMatt Ranostay 		*val = le16_to_cpu(buf);
635aff268cdSMatt Ranostay 	} else
636aff268cdSMatt Ranostay 		return -EINVAL;
637aff268cdSMatt Ranostay 
638aff268cdSMatt Ranostay 	*val2 = 0;
639aff268cdSMatt Ranostay 
640aff268cdSMatt Ranostay 	return IIO_VAL_INT;
641aff268cdSMatt Ranostay }
642aff268cdSMatt Ranostay 
643aff268cdSMatt Ranostay static int apds9960_write_event(struct iio_dev *indio_dev,
644aff268cdSMatt Ranostay 				const struct iio_chan_spec *chan,
645aff268cdSMatt Ranostay 				enum iio_event_type type,
646aff268cdSMatt Ranostay 				enum iio_event_direction dir,
647aff268cdSMatt Ranostay 				enum iio_event_info info,
648aff268cdSMatt Ranostay 				int val, int val2)
649aff268cdSMatt Ranostay {
650aff268cdSMatt Ranostay 	u8 reg;
651801ab335SJonathan Cameron 	__le16 buf;
652aff268cdSMatt Ranostay 	int ret = 0;
653aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
654aff268cdSMatt Ranostay 
655aff268cdSMatt Ranostay 	if (info != IIO_EV_INFO_VALUE)
656aff268cdSMatt Ranostay 		return -EINVAL;
657aff268cdSMatt Ranostay 
658aff268cdSMatt Ranostay 	ret = apds9960_get_thres_reg(chan, dir, &reg);
659aff268cdSMatt Ranostay 	if (ret < 0)
660aff268cdSMatt Ranostay 		return ret;
661aff268cdSMatt Ranostay 
662aff268cdSMatt Ranostay 	if (chan->type == IIO_PROXIMITY) {
663aff268cdSMatt Ranostay 		if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
664aff268cdSMatt Ranostay 			return -EINVAL;
665aff268cdSMatt Ranostay 		ret = regmap_write(data->regmap, reg, val);
666aff268cdSMatt Ranostay 		if (ret < 0)
667aff268cdSMatt Ranostay 			return ret;
668aff268cdSMatt Ranostay 	} else if (chan->type == IIO_INTENSITY) {
669aff268cdSMatt Ranostay 		if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
670aff268cdSMatt Ranostay 			return -EINVAL;
671aff268cdSMatt Ranostay 		buf = cpu_to_le16(val);
672aff268cdSMatt Ranostay 		ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
673aff268cdSMatt Ranostay 		if (ret < 0)
674aff268cdSMatt Ranostay 			return ret;
675aff268cdSMatt Ranostay 	} else
676aff268cdSMatt Ranostay 		return -EINVAL;
677aff268cdSMatt Ranostay 
678aff268cdSMatt Ranostay 	return 0;
679aff268cdSMatt Ranostay }
680aff268cdSMatt Ranostay 
681aff268cdSMatt Ranostay static int apds9960_read_event_config(struct iio_dev *indio_dev,
682aff268cdSMatt Ranostay 				      const struct iio_chan_spec *chan,
683aff268cdSMatt Ranostay 				      enum iio_event_type type,
684aff268cdSMatt Ranostay 				      enum iio_event_direction dir)
685aff268cdSMatt Ranostay {
686aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
687aff268cdSMatt Ranostay 
688aff268cdSMatt Ranostay 	switch (chan->type) {
689aff268cdSMatt Ranostay 	case IIO_PROXIMITY:
690aff268cdSMatt Ranostay 		return data->pxs_int;
691aff268cdSMatt Ranostay 	case IIO_INTENSITY:
692aff268cdSMatt Ranostay 		return data->als_int;
693aff268cdSMatt Ranostay 	default:
694aff268cdSMatt Ranostay 		return -EINVAL;
695aff268cdSMatt Ranostay 	}
696aff268cdSMatt Ranostay 
697aff268cdSMatt Ranostay 	return 0;
698aff268cdSMatt Ranostay }
699aff268cdSMatt Ranostay 
700aff268cdSMatt Ranostay static int apds9960_write_event_config(struct iio_dev *indio_dev,
701aff268cdSMatt Ranostay 				       const struct iio_chan_spec *chan,
702aff268cdSMatt Ranostay 				       enum iio_event_type type,
703aff268cdSMatt Ranostay 				       enum iio_event_direction dir,
704aff268cdSMatt Ranostay 				       int state)
705aff268cdSMatt Ranostay {
706aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
707aff268cdSMatt Ranostay 	int ret;
708aff268cdSMatt Ranostay 
709aff268cdSMatt Ranostay 	state = !!state;
710aff268cdSMatt Ranostay 
711aff268cdSMatt Ranostay 	switch (chan->type) {
712aff268cdSMatt Ranostay 	case IIO_PROXIMITY:
713aff268cdSMatt Ranostay 		if (data->pxs_int == state)
714aff268cdSMatt Ranostay 			return -EINVAL;
715aff268cdSMatt Ranostay 
716aff268cdSMatt Ranostay 		ret = regmap_field_write(data->reg_int_pxs, state);
717aff268cdSMatt Ranostay 		if (ret)
718aff268cdSMatt Ranostay 			return ret;
719aff268cdSMatt Ranostay 		data->pxs_int = state;
720aff268cdSMatt Ranostay 		apds9960_set_power_state(data, state);
721aff268cdSMatt Ranostay 		break;
722aff268cdSMatt Ranostay 	case IIO_INTENSITY:
723aff268cdSMatt Ranostay 		if (data->als_int == state)
724aff268cdSMatt Ranostay 			return -EINVAL;
725aff268cdSMatt Ranostay 
726aff268cdSMatt Ranostay 		ret = regmap_field_write(data->reg_int_als, state);
727aff268cdSMatt Ranostay 		if (ret)
728aff268cdSMatt Ranostay 			return ret;
729aff268cdSMatt Ranostay 		data->als_int = state;
730aff268cdSMatt Ranostay 		apds9960_set_power_state(data, state);
731aff268cdSMatt Ranostay 		break;
732aff268cdSMatt Ranostay 	default:
733aff268cdSMatt Ranostay 		return -EINVAL;
734aff268cdSMatt Ranostay 	}
735aff268cdSMatt Ranostay 
736aff268cdSMatt Ranostay 	return 0;
737aff268cdSMatt Ranostay }
738aff268cdSMatt Ranostay 
739aff268cdSMatt Ranostay static const struct iio_info apds9960_info = {
740aff268cdSMatt Ranostay 	.attrs = &apds9960_attribute_group,
741aff268cdSMatt Ranostay 	.read_raw = apds9960_read_raw,
742aff268cdSMatt Ranostay 	.write_raw = apds9960_write_raw,
743aff268cdSMatt Ranostay 	.read_event_value = apds9960_read_event,
744aff268cdSMatt Ranostay 	.write_event_value = apds9960_write_event,
745aff268cdSMatt Ranostay 	.read_event_config = apds9960_read_event_config,
746aff268cdSMatt Ranostay 	.write_event_config = apds9960_write_event_config,
747aff268cdSMatt Ranostay 
748aff268cdSMatt Ranostay };
749aff268cdSMatt Ranostay 
750aff268cdSMatt Ranostay static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
751aff268cdSMatt Ranostay {
752aff268cdSMatt Ranostay 	int cnt;
753aff268cdSMatt Ranostay 	int ret;
754aff268cdSMatt Ranostay 
755aff268cdSMatt Ranostay 	ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
756aff268cdSMatt Ranostay 	if (ret)
757aff268cdSMatt Ranostay 		return ret;
758aff268cdSMatt Ranostay 
759aff268cdSMatt Ranostay 	return cnt;
760aff268cdSMatt Ranostay }
761aff268cdSMatt Ranostay 
762aff268cdSMatt Ranostay static void apds9960_read_gesture_fifo(struct apds9960_data *data)
763aff268cdSMatt Ranostay {
764aff268cdSMatt Ranostay 	int ret, cnt = 0;
765aff268cdSMatt Ranostay 
766aff268cdSMatt Ranostay 	mutex_lock(&data->lock);
767aff268cdSMatt Ranostay 	data->gesture_mode_running = 1;
768aff268cdSMatt Ranostay 
769655048a0SMatt Ranostay 	while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
770aff268cdSMatt Ranostay 		ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
771aff268cdSMatt Ranostay 				      &data->buffer, 4);
772aff268cdSMatt Ranostay 
773aff268cdSMatt Ranostay 		if (ret)
774aff268cdSMatt Ranostay 			goto err_read;
775aff268cdSMatt Ranostay 
776aff268cdSMatt Ranostay 		iio_push_to_buffers(data->indio_dev, data->buffer);
777655048a0SMatt Ranostay 		cnt--;
778aff268cdSMatt Ranostay 	}
779aff268cdSMatt Ranostay 
780aff268cdSMatt Ranostay err_read:
781aff268cdSMatt Ranostay 	data->gesture_mode_running = 0;
782aff268cdSMatt Ranostay 	mutex_unlock(&data->lock);
783aff268cdSMatt Ranostay }
784aff268cdSMatt Ranostay 
785aff268cdSMatt Ranostay static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
786aff268cdSMatt Ranostay {
787aff268cdSMatt Ranostay 	struct iio_dev *indio_dev = private;
788aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
789aff268cdSMatt Ranostay 	int ret, status;
790aff268cdSMatt Ranostay 
791aff268cdSMatt Ranostay 	ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
792aff268cdSMatt Ranostay 	if (ret < 0) {
793aff268cdSMatt Ranostay 		dev_err(&data->client->dev, "irq status reg read failed\n");
794aff268cdSMatt Ranostay 		return IRQ_HANDLED;
795aff268cdSMatt Ranostay 	}
796aff268cdSMatt Ranostay 
797aff268cdSMatt Ranostay 	if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
798aff268cdSMatt Ranostay 		iio_push_event(indio_dev,
799aff268cdSMatt Ranostay 			       IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
800aff268cdSMatt Ranostay 						    IIO_EV_TYPE_THRESH,
801aff268cdSMatt Ranostay 						    IIO_EV_DIR_EITHER),
802bc2b7dabSGregor Boirie 			       iio_get_time_ns(indio_dev));
803aff268cdSMatt Ranostay 		regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
804aff268cdSMatt Ranostay 	}
805aff268cdSMatt Ranostay 
806aff268cdSMatt Ranostay 	if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
807aff268cdSMatt Ranostay 		iio_push_event(indio_dev,
808aff268cdSMatt Ranostay 			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
809aff268cdSMatt Ranostay 						    IIO_EV_TYPE_THRESH,
810aff268cdSMatt Ranostay 						    IIO_EV_DIR_EITHER),
811bc2b7dabSGregor Boirie 			       iio_get_time_ns(indio_dev));
812aff268cdSMatt Ranostay 		regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
813aff268cdSMatt Ranostay 	}
814aff268cdSMatt Ranostay 
815aff268cdSMatt Ranostay 	if (status & APDS9960_REG_STATUS_GINT)
816aff268cdSMatt Ranostay 		apds9960_read_gesture_fifo(data);
817aff268cdSMatt Ranostay 
818aff268cdSMatt Ranostay 	return IRQ_HANDLED;
819aff268cdSMatt Ranostay }
820aff268cdSMatt Ranostay 
821aff268cdSMatt Ranostay static int apds9960_set_powermode(struct apds9960_data *data, bool state)
822aff268cdSMatt Ranostay {
823aff268cdSMatt Ranostay 	return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
824aff268cdSMatt Ranostay }
825aff268cdSMatt Ranostay 
826aff268cdSMatt Ranostay static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
827aff268cdSMatt Ranostay {
828aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
829aff268cdSMatt Ranostay 	int ret;
830aff268cdSMatt Ranostay 
831aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 1);
832aff268cdSMatt Ranostay 	if (ret)
833aff268cdSMatt Ranostay 		return ret;
834aff268cdSMatt Ranostay 
835aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 1);
836aff268cdSMatt Ranostay 	if (ret)
837aff268cdSMatt Ranostay 		return ret;
838aff268cdSMatt Ranostay 
839aff268cdSMatt Ranostay 	pm_runtime_get_sync(&data->client->dev);
840aff268cdSMatt Ranostay 
841aff268cdSMatt Ranostay 	return 0;
842aff268cdSMatt Ranostay }
843aff268cdSMatt Ranostay 
844aff268cdSMatt Ranostay static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
845aff268cdSMatt Ranostay {
846aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
847aff268cdSMatt Ranostay 	int ret;
848aff268cdSMatt Ranostay 
849aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 0);
850aff268cdSMatt Ranostay 	if (ret)
851aff268cdSMatt Ranostay 		return ret;
852aff268cdSMatt Ranostay 
853aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 0);
854aff268cdSMatt Ranostay 	if (ret)
855aff268cdSMatt Ranostay 		return ret;
856aff268cdSMatt Ranostay 
857aff268cdSMatt Ranostay 	pm_runtime_put_autosuspend(&data->client->dev);
858aff268cdSMatt Ranostay 
859aff268cdSMatt Ranostay 	return 0;
860aff268cdSMatt Ranostay }
861aff268cdSMatt Ranostay 
862aff268cdSMatt Ranostay static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
863aff268cdSMatt Ranostay 	.postenable = apds9960_buffer_postenable,
864aff268cdSMatt Ranostay 	.predisable = apds9960_buffer_predisable,
865aff268cdSMatt Ranostay };
866aff268cdSMatt Ranostay 
867aff268cdSMatt Ranostay static int apds9960_regfield_init(struct apds9960_data *data)
868aff268cdSMatt Ranostay {
869aff268cdSMatt Ranostay 	struct device *dev = &data->client->dev;
870aff268cdSMatt Ranostay 	struct regmap *regmap = data->regmap;
871aff268cdSMatt Ranostay 
872aff268cdSMatt Ranostay 	data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
873aff268cdSMatt Ranostay 						apds9960_reg_field_int_als);
874aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_als)) {
875aff268cdSMatt Ranostay 		dev_err(dev, "INT ALS reg field init failed\n");
876aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_als);
877aff268cdSMatt Ranostay 	}
878aff268cdSMatt Ranostay 
879aff268cdSMatt Ranostay 	data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
880aff268cdSMatt Ranostay 						apds9960_reg_field_int_ges);
881aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_ges)) {
882aff268cdSMatt Ranostay 		dev_err(dev, "INT gesture reg field init failed\n");
883aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_ges);
884aff268cdSMatt Ranostay 	}
885aff268cdSMatt Ranostay 
886aff268cdSMatt Ranostay 	data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
887aff268cdSMatt Ranostay 						apds9960_reg_field_int_pxs);
888aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_pxs)) {
889aff268cdSMatt Ranostay 		dev_err(dev, "INT pxs reg field init failed\n");
890aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_pxs);
891aff268cdSMatt Ranostay 	}
892aff268cdSMatt Ranostay 
893aff268cdSMatt Ranostay 	data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
894aff268cdSMatt Ranostay 						apds9960_reg_field_enable_als);
895aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_als)) {
896aff268cdSMatt Ranostay 		dev_err(dev, "Enable ALS reg field init failed\n");
897aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_als);
898aff268cdSMatt Ranostay 	}
899aff268cdSMatt Ranostay 
900aff268cdSMatt Ranostay 	data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
901aff268cdSMatt Ranostay 						apds9960_reg_field_enable_ges);
902aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_ges)) {
903aff268cdSMatt Ranostay 		dev_err(dev, "Enable gesture reg field init failed\n");
904aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_ges);
905aff268cdSMatt Ranostay 	}
906aff268cdSMatt Ranostay 
907aff268cdSMatt Ranostay 	data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
908aff268cdSMatt Ranostay 						apds9960_reg_field_enable_pxs);
909aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_pxs)) {
910aff268cdSMatt Ranostay 		dev_err(dev, "Enable PXS reg field init failed\n");
911aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_pxs);
912aff268cdSMatt Ranostay 	}
913aff268cdSMatt Ranostay 
914aff268cdSMatt Ranostay 	return 0;
915aff268cdSMatt Ranostay }
916aff268cdSMatt Ranostay 
917aff268cdSMatt Ranostay static int apds9960_chip_init(struct apds9960_data *data)
918aff268cdSMatt Ranostay {
919aff268cdSMatt Ranostay 	int ret;
920aff268cdSMatt Ranostay 
921aff268cdSMatt Ranostay 	/* Default IT for ALS of 28 ms */
922aff268cdSMatt Ranostay 	ret = apds9960_set_it_time(data, 28000);
923aff268cdSMatt Ranostay 	if (ret)
924aff268cdSMatt Ranostay 		return ret;
925aff268cdSMatt Ranostay 
926aff268cdSMatt Ranostay 	/* Ensure gesture interrupt is OFF */
927aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 0);
928aff268cdSMatt Ranostay 	if (ret)
929aff268cdSMatt Ranostay 		return ret;
930aff268cdSMatt Ranostay 
931aff268cdSMatt Ranostay 	/* Disable gesture sensor, since polling is useless from user-space */
932aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 0);
933aff268cdSMatt Ranostay 	if (ret)
934aff268cdSMatt Ranostay 		return ret;
935aff268cdSMatt Ranostay 
936aff268cdSMatt Ranostay 	/* Ensure proximity interrupt is OFF */
937aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_pxs, 0);
938aff268cdSMatt Ranostay 	if (ret)
939aff268cdSMatt Ranostay 		return ret;
940aff268cdSMatt Ranostay 
941aff268cdSMatt Ranostay 	/* Enable proximity sensor for polling */
942aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_pxs, 1);
943aff268cdSMatt Ranostay 	if (ret)
944aff268cdSMatt Ranostay 		return ret;
945aff268cdSMatt Ranostay 
946aff268cdSMatt Ranostay 	/* Ensure ALS interrupt is OFF */
947aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_als, 0);
948aff268cdSMatt Ranostay 	if (ret)
949aff268cdSMatt Ranostay 		return ret;
950aff268cdSMatt Ranostay 
951aff268cdSMatt Ranostay 	/* Enable ALS sensor for polling */
952aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_als, 1);
953aff268cdSMatt Ranostay 	if (ret)
954aff268cdSMatt Ranostay 		return ret;
955aff268cdSMatt Ranostay 	/*
956aff268cdSMatt Ranostay 	 * When enabled trigger an interrupt after 3 readings
957aff268cdSMatt Ranostay 	 * outside threshold for ALS + PXS
958aff268cdSMatt Ranostay 	 */
959aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_PERS,
960aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_PERS);
961aff268cdSMatt Ranostay 	if (ret)
962aff268cdSMatt Ranostay 		return ret;
963aff268cdSMatt Ranostay 
964aff268cdSMatt Ranostay 	/*
965aff268cdSMatt Ranostay 	 * Wait for 4 event outside gesture threshold to prevent interrupt
966aff268cdSMatt Ranostay 	 * flooding.
967aff268cdSMatt Ranostay 	 */
968aff268cdSMatt Ranostay 	ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
969aff268cdSMatt Ranostay 			APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
970aff268cdSMatt Ranostay 			BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
971aff268cdSMatt Ranostay 	if (ret)
972aff268cdSMatt Ranostay 		return ret;
973aff268cdSMatt Ranostay 
974aff268cdSMatt Ranostay 	/* Default ENTER and EXIT thresholds for the GESTURE engine. */
975aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
976aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_GPENTH);
977aff268cdSMatt Ranostay 	if (ret)
978aff268cdSMatt Ranostay 		return ret;
979aff268cdSMatt Ranostay 
980aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
981aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_GEXTH);
982aff268cdSMatt Ranostay 	if (ret)
983aff268cdSMatt Ranostay 		return ret;
984aff268cdSMatt Ranostay 
985ebe5c543Skbuild test robot 	return apds9960_set_powermode(data, 1);
986aff268cdSMatt Ranostay }
987aff268cdSMatt Ranostay 
988aff268cdSMatt Ranostay static int apds9960_probe(struct i2c_client *client,
989aff268cdSMatt Ranostay 			  const struct i2c_device_id *id)
990aff268cdSMatt Ranostay {
991aff268cdSMatt Ranostay 	struct apds9960_data *data;
992aff268cdSMatt Ranostay 	struct iio_buffer *buffer;
993aff268cdSMatt Ranostay 	struct iio_dev *indio_dev;
994aff268cdSMatt Ranostay 	int ret;
995aff268cdSMatt Ranostay 
996aff268cdSMatt Ranostay 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
997aff268cdSMatt Ranostay 	if (!indio_dev)
998aff268cdSMatt Ranostay 		return -ENOMEM;
999aff268cdSMatt Ranostay 
1000aff268cdSMatt Ranostay 	buffer = devm_iio_kfifo_allocate(&client->dev);
1001aff268cdSMatt Ranostay 	if (!buffer)
1002aff268cdSMatt Ranostay 		return -ENOMEM;
1003aff268cdSMatt Ranostay 
1004aff268cdSMatt Ranostay 	iio_device_attach_buffer(indio_dev, buffer);
1005aff268cdSMatt Ranostay 
1006590b92a3SYong Li 	indio_dev->dev.parent = &client->dev;
1007aff268cdSMatt Ranostay 	indio_dev->info = &apds9960_info;
1008aff268cdSMatt Ranostay 	indio_dev->name = APDS9960_DRV_NAME;
1009aff268cdSMatt Ranostay 	indio_dev->channels = apds9960_channels;
1010aff268cdSMatt Ranostay 	indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
1011aff268cdSMatt Ranostay 	indio_dev->available_scan_masks = apds9960_scan_masks;
1012aff268cdSMatt Ranostay 	indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
1013aff268cdSMatt Ranostay 	indio_dev->setup_ops = &apds9960_buffer_setup_ops;
1014aff268cdSMatt Ranostay 
1015aff268cdSMatt Ranostay 	data = iio_priv(indio_dev);
1016aff268cdSMatt Ranostay 	i2c_set_clientdata(client, indio_dev);
1017aff268cdSMatt Ranostay 
1018aff268cdSMatt Ranostay 	data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config);
1019aff268cdSMatt Ranostay 	if (IS_ERR(data->regmap)) {
1020aff268cdSMatt Ranostay 		dev_err(&client->dev, "regmap initialization failed.\n");
1021aff268cdSMatt Ranostay 		return PTR_ERR(data->regmap);
1022aff268cdSMatt Ranostay 	}
1023aff268cdSMatt Ranostay 
1024aff268cdSMatt Ranostay 	data->client = client;
1025aff268cdSMatt Ranostay 	data->indio_dev = indio_dev;
1026aff268cdSMatt Ranostay 	mutex_init(&data->lock);
1027aff268cdSMatt Ranostay 
1028aff268cdSMatt Ranostay 	ret = pm_runtime_set_active(&client->dev);
1029aff268cdSMatt Ranostay 	if (ret)
1030aff268cdSMatt Ranostay 		goto error_power_down;
1031aff268cdSMatt Ranostay 
1032aff268cdSMatt Ranostay 	pm_runtime_enable(&client->dev);
1033aff268cdSMatt Ranostay 	pm_runtime_set_autosuspend_delay(&client->dev, 5000);
1034aff268cdSMatt Ranostay 	pm_runtime_use_autosuspend(&client->dev);
1035aff268cdSMatt Ranostay 
1036aff268cdSMatt Ranostay 	apds9960_set_power_state(data, true);
1037aff268cdSMatt Ranostay 
1038aff268cdSMatt Ranostay 	ret = apds9960_regfield_init(data);
1039aff268cdSMatt Ranostay 	if (ret)
1040aff268cdSMatt Ranostay 		goto error_power_down;
1041aff268cdSMatt Ranostay 
1042aff268cdSMatt Ranostay 	ret = apds9960_chip_init(data);
1043aff268cdSMatt Ranostay 	if (ret)
1044aff268cdSMatt Ranostay 		goto error_power_down;
1045aff268cdSMatt Ranostay 
1046aff268cdSMatt Ranostay 	if (client->irq <= 0) {
1047aff268cdSMatt Ranostay 		dev_err(&client->dev, "no valid irq defined\n");
1048aff268cdSMatt Ranostay 		ret = -EINVAL;
1049aff268cdSMatt Ranostay 		goto error_power_down;
1050aff268cdSMatt Ranostay 	}
1051aff268cdSMatt Ranostay 	ret = devm_request_threaded_irq(&client->dev, client->irq,
1052aff268cdSMatt Ranostay 					NULL, apds9960_interrupt_handler,
1053aff268cdSMatt Ranostay 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1054aff268cdSMatt Ranostay 					"apds9960_event",
1055aff268cdSMatt Ranostay 					indio_dev);
1056aff268cdSMatt Ranostay 	if (ret) {
1057aff268cdSMatt Ranostay 		dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1058aff268cdSMatt Ranostay 		goto error_power_down;
1059aff268cdSMatt Ranostay 	}
1060aff268cdSMatt Ranostay 
1061aff268cdSMatt Ranostay 	ret = iio_device_register(indio_dev);
1062aff268cdSMatt Ranostay 	if (ret)
1063aff268cdSMatt Ranostay 		goto error_power_down;
1064aff268cdSMatt Ranostay 
1065aff268cdSMatt Ranostay 	apds9960_set_power_state(data, false);
1066aff268cdSMatt Ranostay 
1067aff268cdSMatt Ranostay 	return 0;
1068aff268cdSMatt Ranostay 
1069aff268cdSMatt Ranostay error_power_down:
1070aff268cdSMatt Ranostay 	apds9960_set_power_state(data, false);
1071aff268cdSMatt Ranostay 
1072aff268cdSMatt Ranostay 	return ret;
1073aff268cdSMatt Ranostay }
1074aff268cdSMatt Ranostay 
1075aff268cdSMatt Ranostay static int apds9960_remove(struct i2c_client *client)
1076aff268cdSMatt Ranostay {
1077aff268cdSMatt Ranostay 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1078aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
1079aff268cdSMatt Ranostay 
1080aff268cdSMatt Ranostay 	iio_device_unregister(indio_dev);
1081aff268cdSMatt Ranostay 	pm_runtime_disable(&client->dev);
1082aff268cdSMatt Ranostay 	pm_runtime_set_suspended(&client->dev);
1083aff268cdSMatt Ranostay 	apds9960_set_powermode(data, 0);
1084aff268cdSMatt Ranostay 
1085aff268cdSMatt Ranostay 	return 0;
1086aff268cdSMatt Ranostay }
1087aff268cdSMatt Ranostay 
1088aff268cdSMatt Ranostay #ifdef CONFIG_PM
1089aff268cdSMatt Ranostay static int apds9960_runtime_suspend(struct device *dev)
1090aff268cdSMatt Ranostay {
1091aff268cdSMatt Ranostay 	struct apds9960_data *data =
1092aff268cdSMatt Ranostay 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1093aff268cdSMatt Ranostay 
1094aff268cdSMatt Ranostay 	return apds9960_set_powermode(data, 0);
1095aff268cdSMatt Ranostay }
1096aff268cdSMatt Ranostay 
1097aff268cdSMatt Ranostay static int apds9960_runtime_resume(struct device *dev)
1098aff268cdSMatt Ranostay {
1099aff268cdSMatt Ranostay 	struct apds9960_data *data =
1100aff268cdSMatt Ranostay 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1101aff268cdSMatt Ranostay 
1102aff268cdSMatt Ranostay 	return apds9960_set_powermode(data, 1);
1103aff268cdSMatt Ranostay }
1104aff268cdSMatt Ranostay #endif
1105aff268cdSMatt Ranostay 
1106aff268cdSMatt Ranostay static const struct dev_pm_ops apds9960_pm_ops = {
1107d454ae2eSMatt Ranostay 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1108d454ae2eSMatt Ranostay 				pm_runtime_force_resume)
1109aff268cdSMatt Ranostay 	SET_RUNTIME_PM_OPS(apds9960_runtime_suspend,
1110aff268cdSMatt Ranostay 			   apds9960_runtime_resume, NULL)
1111aff268cdSMatt Ranostay };
1112aff268cdSMatt Ranostay 
1113aff268cdSMatt Ranostay static const struct i2c_device_id apds9960_id[] = {
1114aff268cdSMatt Ranostay 	{ "apds9960", 0 },
1115aff268cdSMatt Ranostay 	{}
1116aff268cdSMatt Ranostay };
1117aff268cdSMatt Ranostay MODULE_DEVICE_TABLE(i2c, apds9960_id);
1118aff268cdSMatt Ranostay 
1119ae4e825bSJavier Martinez Canillas static const struct of_device_id apds9960_of_match[] = {
1120ae4e825bSJavier Martinez Canillas 	{ .compatible = "avago,apds9960" },
1121ae4e825bSJavier Martinez Canillas 	{ }
1122ae4e825bSJavier Martinez Canillas };
1123ae4e825bSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, apds9960_of_match);
1124ae4e825bSJavier Martinez Canillas 
1125aff268cdSMatt Ranostay static struct i2c_driver apds9960_driver = {
1126aff268cdSMatt Ranostay 	.driver = {
1127aff268cdSMatt Ranostay 		.name	= APDS9960_DRV_NAME,
1128ae4e825bSJavier Martinez Canillas 		.of_match_table = apds9960_of_match,
1129aff268cdSMatt Ranostay 		.pm	= &apds9960_pm_ops,
1130aff268cdSMatt Ranostay 	},
1131aff268cdSMatt Ranostay 	.probe		= apds9960_probe,
1132aff268cdSMatt Ranostay 	.remove		= apds9960_remove,
1133aff268cdSMatt Ranostay 	.id_table	= apds9960_id,
1134aff268cdSMatt Ranostay };
1135aff268cdSMatt Ranostay module_i2c_driver(apds9960_driver);
1136aff268cdSMatt Ranostay 
1137d6ad8058SMatt Ranostay MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
1138*50e5bae0SChristophe JAILLET MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1139aff268cdSMatt Ranostay MODULE_LICENSE("GPL");
1140