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