xref: /linux/drivers/iio/light/apds9960.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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_ID		0x92
58aff268cdSMatt Ranostay 
59aff268cdSMatt Ranostay #define APDS9960_REG_STATUS	0x93
60aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_PS_INT	BIT(5)
61aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_ALS_INT	BIT(4)
62aff268cdSMatt Ranostay #define APDS9960_REG_STATUS_GINT	BIT(2)
63aff268cdSMatt Ranostay 
64aff268cdSMatt Ranostay #define APDS9960_REG_PDATA	0x9c
65aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_UR	0x9d
66aff268cdSMatt Ranostay #define APDS9960_REG_POFFSET_DL 0x9e
67aff268cdSMatt Ranostay #define APDS9960_REG_CONFIG_3	0x9f
68aff268cdSMatt Ranostay 
69aff268cdSMatt Ranostay #define APDS9960_REG_GPENTH	0xa0
70aff268cdSMatt Ranostay #define APDS9960_REG_GEXTH	0xa1
71aff268cdSMatt Ranostay 
72aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1	0xa2
73aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK		0xc0
74aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT	6
75aff268cdSMatt Ranostay 
76aff268cdSMatt Ranostay #define APDS9960_REG_GCONF_2	0xa3
770aa60ff5SAlejandro Concepción Rodríguez #define APDS9960_REG_GCONF_2_GGAIN_MASK			0x60
780aa60ff5SAlejandro Concepción Rodríguez #define APDS9960_REG_GCONF_2_GGAIN_MASK_SHIFT		5
790aa60ff5SAlejandro Concepción Rodríguez 
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 */
149*06e91ad3SAbhash Jha 
150*06e91ad3SAbhash Jha 	/* calibration value buffer */
151*06e91ad3SAbhash Jha 	int calibbias[5];
152*06e91ad3SAbhash Jha };
153*06e91ad3SAbhash Jha 
154*06e91ad3SAbhash Jha enum {
155*06e91ad3SAbhash Jha 	APDS9960_CHAN_PROXIMITY,
156*06e91ad3SAbhash Jha 	APDS9960_CHAN_GESTURE_UP,
157*06e91ad3SAbhash Jha 	APDS9960_CHAN_GESTURE_DOWN,
158*06e91ad3SAbhash Jha 	APDS9960_CHAN_GESTURE_LEFT,
159*06e91ad3SAbhash Jha 	APDS9960_CHAN_GESTURE_RIGHT,
160*06e91ad3SAbhash Jha };
161*06e91ad3SAbhash Jha 
162*06e91ad3SAbhash Jha static const unsigned int apds9960_offset_regs[][2] = {
163*06e91ad3SAbhash Jha 	[APDS9960_CHAN_PROXIMITY] = {APDS9960_REG_POFFSET_UR, APDS9960_REG_POFFSET_DL},
164*06e91ad3SAbhash Jha 	[APDS9960_CHAN_GESTURE_UP] = {APDS9960_REG_GOFFSET_U, 0},
165*06e91ad3SAbhash Jha 	[APDS9960_CHAN_GESTURE_DOWN] = {APDS9960_REG_GOFFSET_D, 0},
166*06e91ad3SAbhash Jha 	[APDS9960_CHAN_GESTURE_LEFT] = {APDS9960_REG_GOFFSET_L, 0},
167*06e91ad3SAbhash Jha 	[APDS9960_CHAN_GESTURE_RIGHT] = {APDS9960_REG_GOFFSET_R, 0},
168aff268cdSMatt Ranostay };
169aff268cdSMatt Ranostay 
170aff268cdSMatt Ranostay static const struct reg_default apds9960_reg_defaults[] = {
171aff268cdSMatt Ranostay 	/* Default ALS integration time = 2.48ms */
172aff268cdSMatt Ranostay 	{ APDS9960_REG_ATIME, 0xff },
173aff268cdSMatt Ranostay };
174aff268cdSMatt Ranostay 
175aff268cdSMatt Ranostay static const struct regmap_range apds9960_volatile_ranges[] = {
176aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_STATUS,
177aff268cdSMatt Ranostay 				APDS9960_REG_PDATA),
178aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFLVL,
179aff268cdSMatt Ranostay 				APDS9960_REG_GSTATUS),
180aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
181aff268cdSMatt Ranostay 				APDS9960_REG_GFIFO_DIR(RIGHT)),
182aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_IFORCE,
183aff268cdSMatt Ranostay 				APDS9960_REG_AICLEAR),
184aff268cdSMatt Ranostay };
185aff268cdSMatt Ranostay 
186aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_volatile_table = {
187aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_volatile_ranges,
188aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_volatile_ranges),
189aff268cdSMatt Ranostay };
190aff268cdSMatt Ranostay 
191aff268cdSMatt Ranostay static const struct regmap_range apds9960_precious_ranges[] = {
192aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
193aff268cdSMatt Ranostay };
194aff268cdSMatt Ranostay 
195aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_precious_table = {
196aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_precious_ranges,
197aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_precious_ranges),
198aff268cdSMatt Ranostay };
199aff268cdSMatt Ranostay 
200aff268cdSMatt Ranostay static const struct regmap_range apds9960_readable_ranges[] = {
201aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_ENABLE,
202aff268cdSMatt Ranostay 				APDS9960_REG_GSTATUS),
203aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
204aff268cdSMatt Ranostay 				APDS9960_REG_GFIFO_DIR(RIGHT)),
205aff268cdSMatt Ranostay };
206aff268cdSMatt Ranostay 
207aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_readable_table = {
208aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_readable_ranges,
209aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_readable_ranges),
210aff268cdSMatt Ranostay };
211aff268cdSMatt Ranostay 
212aff268cdSMatt Ranostay static const struct regmap_range apds9960_writeable_ranges[] = {
213aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
214aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
215aff268cdSMatt Ranostay 	regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
216aff268cdSMatt Ranostay };
217aff268cdSMatt Ranostay 
218aff268cdSMatt Ranostay static const struct regmap_access_table apds9960_writeable_table = {
219aff268cdSMatt Ranostay 	.yes_ranges	= apds9960_writeable_ranges,
220aff268cdSMatt Ranostay 	.n_yes_ranges	= ARRAY_SIZE(apds9960_writeable_ranges),
221aff268cdSMatt Ranostay };
222aff268cdSMatt Ranostay 
223aff268cdSMatt Ranostay static const struct regmap_config apds9960_regmap_config = {
224aff268cdSMatt Ranostay 	.name = APDS9960_REGMAP_NAME,
225aff268cdSMatt Ranostay 	.reg_bits = 8,
226aff268cdSMatt Ranostay 	.val_bits = 8,
2271c96a2f6SDavid Frey 	.use_single_read = true,
2281c96a2f6SDavid Frey 	.use_single_write = true,
229aff268cdSMatt Ranostay 
230aff268cdSMatt Ranostay 	.volatile_table = &apds9960_volatile_table,
231aff268cdSMatt Ranostay 	.precious_table = &apds9960_precious_table,
232aff268cdSMatt Ranostay 	.rd_table = &apds9960_readable_table,
233aff268cdSMatt Ranostay 	.wr_table = &apds9960_writeable_table,
234aff268cdSMatt Ranostay 
235aff268cdSMatt Ranostay 	.reg_defaults = apds9960_reg_defaults,
236aff268cdSMatt Ranostay 	.num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
237aff268cdSMatt Ranostay 	.max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
238aff268cdSMatt Ranostay 	.cache_type = REGCACHE_RBTREE,
239aff268cdSMatt Ranostay };
240aff268cdSMatt Ranostay 
241aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_pxs_event_spec[] = {
242aff268cdSMatt Ranostay 	{
243aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
244aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_RISING,
2455ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
246aff268cdSMatt Ranostay 	},
247aff268cdSMatt Ranostay 	{
248aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
249aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_FALLING,
2505ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
2515ae34494SSubhajit Ghosh 	},
2525ae34494SSubhajit Ghosh 	{
2535ae34494SSubhajit Ghosh 		.type = IIO_EV_TYPE_THRESH,
2545ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
255aff268cdSMatt Ranostay 	},
256aff268cdSMatt Ranostay };
257aff268cdSMatt Ranostay 
258aff268cdSMatt Ranostay static const struct iio_event_spec apds9960_als_event_spec[] = {
259aff268cdSMatt Ranostay 	{
260aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
261aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_RISING,
2625ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
263aff268cdSMatt Ranostay 	},
264aff268cdSMatt Ranostay 	{
265aff268cdSMatt Ranostay 		.type = IIO_EV_TYPE_THRESH,
266aff268cdSMatt Ranostay 		.dir = IIO_EV_DIR_FALLING,
2675ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
2685ae34494SSubhajit Ghosh 	},
2695ae34494SSubhajit Ghosh 	{
2705ae34494SSubhajit Ghosh 		.type = IIO_EV_TYPE_THRESH,
2715ae34494SSubhajit Ghosh 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
272aff268cdSMatt Ranostay 	},
273aff268cdSMatt Ranostay };
274aff268cdSMatt Ranostay 
275aff268cdSMatt Ranostay #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
276aff268cdSMatt Ranostay 	.type = IIO_PROXIMITY, \
277*06e91ad3SAbhash Jha 	.info_mask_separate = BIT(IIO_CHAN_INFO_CALIBBIAS), \
278aff268cdSMatt Ranostay 	.channel = _si + 1, \
279aff268cdSMatt Ranostay 	.scan_index = _si, \
280aff268cdSMatt Ranostay 	.indexed = 1, \
281aff268cdSMatt Ranostay 	.scan_type = { \
282aff268cdSMatt Ranostay 		.sign = 'u', \
283aff268cdSMatt Ranostay 		.realbits = 8, \
284aff268cdSMatt Ranostay 		.storagebits = 8, \
285aff268cdSMatt Ranostay 	}, \
286aff268cdSMatt Ranostay }
287aff268cdSMatt Ranostay 
288aff268cdSMatt Ranostay #define APDS9960_INTENSITY_CHANNEL(_colour) { \
289aff268cdSMatt Ranostay 	.type = IIO_INTENSITY, \
290aff268cdSMatt Ranostay 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
291aff268cdSMatt Ranostay 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
292aff268cdSMatt Ranostay 			BIT(IIO_CHAN_INFO_INT_TIME), \
293aff268cdSMatt Ranostay 	.channel2 = IIO_MOD_LIGHT_##_colour, \
294aff268cdSMatt Ranostay 	.address = APDS9960_REG_ALS_CHANNEL(_colour), \
295aff268cdSMatt Ranostay 	.modified = 1, \
296aff268cdSMatt Ranostay 	.scan_index = -1, \
297aff268cdSMatt Ranostay }
298aff268cdSMatt Ranostay 
299aff268cdSMatt Ranostay static const unsigned long apds9960_scan_masks[] = {0xf, 0};
300aff268cdSMatt Ranostay 
301aff268cdSMatt Ranostay static const struct iio_chan_spec apds9960_channels[] = {
302aff268cdSMatt Ranostay 	{
303aff268cdSMatt Ranostay 		.type = IIO_PROXIMITY,
304aff268cdSMatt Ranostay 		.address = APDS9960_REG_PDATA,
305*06e91ad3SAbhash Jha 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
306*06e91ad3SAbhash Jha 			BIT(IIO_CHAN_INFO_CALIBBIAS),
307aff268cdSMatt Ranostay 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
308aff268cdSMatt Ranostay 		.channel = 0,
309aff268cdSMatt Ranostay 		.indexed = 0,
310aff268cdSMatt Ranostay 		.scan_index = -1,
311aff268cdSMatt Ranostay 
312aff268cdSMatt Ranostay 		.event_spec = apds9960_pxs_event_spec,
313aff268cdSMatt Ranostay 		.num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
314aff268cdSMatt Ranostay 	},
315aff268cdSMatt Ranostay 	/* Gesture Sensor */
316aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(UP, 0),
317aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(DOWN, 1),
318aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(LEFT, 2),
319aff268cdSMatt Ranostay 	APDS9960_GESTURE_CHANNEL(RIGHT, 3),
320aff268cdSMatt Ranostay 	/* ALS */
321aff268cdSMatt Ranostay 	{
322aff268cdSMatt Ranostay 		.type = IIO_INTENSITY,
323aff268cdSMatt Ranostay 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
324aff268cdSMatt Ranostay 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
325aff268cdSMatt Ranostay 			BIT(IIO_CHAN_INFO_INT_TIME),
326aff268cdSMatt Ranostay 		.channel2 = IIO_MOD_LIGHT_CLEAR,
327aff268cdSMatt Ranostay 		.address = APDS9960_REG_ALS_CHANNEL(CLEAR),
328aff268cdSMatt Ranostay 		.modified = 1,
329aff268cdSMatt Ranostay 		.scan_index = -1,
330aff268cdSMatt Ranostay 
331aff268cdSMatt Ranostay 		.event_spec = apds9960_als_event_spec,
332aff268cdSMatt Ranostay 		.num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
333aff268cdSMatt Ranostay 	},
334aff268cdSMatt Ranostay 	/* RGB Sensor */
335aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(RED),
336aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(GREEN),
337aff268cdSMatt Ranostay 	APDS9960_INTENSITY_CHANNEL(BLUE),
338aff268cdSMatt Ranostay };
339aff268cdSMatt Ranostay 
apds9960_set_calibbias(struct apds9960_data * data,struct iio_chan_spec const * chan,int calibbias)340*06e91ad3SAbhash Jha static int apds9960_set_calibbias(struct apds9960_data *data,
341*06e91ad3SAbhash Jha 		struct iio_chan_spec const *chan, int calibbias)
342*06e91ad3SAbhash Jha {
343*06e91ad3SAbhash Jha 	int ret, i;
344*06e91ad3SAbhash Jha 
345*06e91ad3SAbhash Jha 	if (calibbias < S8_MIN || calibbias > S8_MAX)
346*06e91ad3SAbhash Jha 		return -EINVAL;
347*06e91ad3SAbhash Jha 
348*06e91ad3SAbhash Jha 	guard(mutex)(&data->lock);
349*06e91ad3SAbhash Jha 	for (i = 0; i < 2; i++) {
350*06e91ad3SAbhash Jha 		if (apds9960_offset_regs[chan->channel][i] == 0)
351*06e91ad3SAbhash Jha 			break;
352*06e91ad3SAbhash Jha 
353*06e91ad3SAbhash Jha 		ret = regmap_write(data->regmap, apds9960_offset_regs[chan->channel][i], calibbias);
354*06e91ad3SAbhash Jha 		if (ret < 0)
355*06e91ad3SAbhash Jha 			return ret;
356*06e91ad3SAbhash Jha 	}
357*06e91ad3SAbhash Jha 	data->calibbias[chan->channel] = calibbias;
358*06e91ad3SAbhash Jha 
359*06e91ad3SAbhash Jha 	return 0;
360*06e91ad3SAbhash Jha }
361*06e91ad3SAbhash Jha 
362aff268cdSMatt Ranostay /* integration time in us */
363ebe16806SSlawomir Stepien static const int apds9960_int_time[][2] = {
364ebe16806SSlawomir Stepien 	{ 28000, 246},
365ebe16806SSlawomir Stepien 	{100000, 219},
366ebe16806SSlawomir Stepien 	{200000, 182},
367ebe16806SSlawomir Stepien 	{700000,   0}
368ebe16806SSlawomir Stepien };
369aff268cdSMatt Ranostay 
370aff268cdSMatt Ranostay /* gain mapping */
371aff268cdSMatt Ranostay static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
372aff268cdSMatt Ranostay static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
373aff268cdSMatt Ranostay 
374aff268cdSMatt Ranostay static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8");
375aff268cdSMatt Ranostay static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64");
376aff268cdSMatt Ranostay static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
377aff268cdSMatt Ranostay 
378aff268cdSMatt Ranostay static struct attribute *apds9960_attributes[] = {
379aff268cdSMatt Ranostay 	&iio_const_attr_proximity_scale_available.dev_attr.attr,
380aff268cdSMatt Ranostay 	&iio_const_attr_intensity_scale_available.dev_attr.attr,
381aff268cdSMatt Ranostay 	&iio_const_attr_integration_time_available.dev_attr.attr,
382aff268cdSMatt Ranostay 	NULL,
383aff268cdSMatt Ranostay };
384aff268cdSMatt Ranostay 
3853ca437c0Ssimran singhal static const struct attribute_group apds9960_attribute_group = {
386aff268cdSMatt Ranostay 	.attrs = apds9960_attributes,
387aff268cdSMatt Ranostay };
388aff268cdSMatt Ranostay 
389aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_als =
390aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
391aff268cdSMatt Ranostay 
392aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_ges =
393aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
394aff268cdSMatt Ranostay 
395aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_int_pxs =
396aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
397aff268cdSMatt Ranostay 
398aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_als =
399aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
400aff268cdSMatt Ranostay 
401aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_ges =
402aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
403aff268cdSMatt Ranostay 
404aff268cdSMatt Ranostay static const struct reg_field apds9960_reg_field_enable_pxs =
405aff268cdSMatt Ranostay 				REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
406aff268cdSMatt Ranostay 
apds9960_set_it_time(struct apds9960_data * data,int val2)407aff268cdSMatt Ranostay static int apds9960_set_it_time(struct apds9960_data *data, int val2)
408aff268cdSMatt Ranostay {
409aff268cdSMatt Ranostay 	int ret = -EINVAL;
410aff268cdSMatt Ranostay 	int idx;
411aff268cdSMatt Ranostay 
412aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) {
413aff268cdSMatt Ranostay 		if (apds9960_int_time[idx][0] == val2) {
414aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
415aff268cdSMatt Ranostay 			ret = regmap_write(data->regmap, APDS9960_REG_ATIME,
416aff268cdSMatt Ranostay 						 apds9960_int_time[idx][1]);
417aff268cdSMatt Ranostay 			if (!ret)
418aff268cdSMatt Ranostay 				data->als_adc_int_us = val2;
419aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
420aff268cdSMatt Ranostay 			break;
421aff268cdSMatt Ranostay 		}
422aff268cdSMatt Ranostay 	}
423aff268cdSMatt Ranostay 
424aff268cdSMatt Ranostay 	return ret;
425aff268cdSMatt Ranostay }
426aff268cdSMatt Ranostay 
apds9960_set_pxs_gain(struct apds9960_data * data,int val)427aff268cdSMatt Ranostay static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
428aff268cdSMatt Ranostay {
429aff268cdSMatt Ranostay 	int ret = -EINVAL;
430aff268cdSMatt Ranostay 	int idx;
431aff268cdSMatt Ranostay 
432aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) {
433aff268cdSMatt Ranostay 		if (apds9960_pxs_gain_map[idx] == val) {
434aff268cdSMatt Ranostay 			/* pxs + gesture gains are mirrored */
435aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
436aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
437aff268cdSMatt Ranostay 				APDS9960_REG_CONTROL,
438aff268cdSMatt Ranostay 				APDS9960_REG_CONTROL_PGAIN_MASK,
439aff268cdSMatt Ranostay 				idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT);
440aff268cdSMatt Ranostay 			if (ret) {
441aff268cdSMatt Ranostay 				mutex_unlock(&data->lock);
442aff268cdSMatt Ranostay 				break;
443aff268cdSMatt Ranostay 			}
444aff268cdSMatt Ranostay 
445aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
4460aa60ff5SAlejandro Concepción Rodríguez 				APDS9960_REG_GCONF_2,
4470aa60ff5SAlejandro Concepción Rodríguez 				APDS9960_REG_GCONF_2_GGAIN_MASK,
4480aa60ff5SAlejandro Concepción Rodríguez 				idx << APDS9960_REG_GCONF_2_GGAIN_MASK_SHIFT);
449aff268cdSMatt Ranostay 			if (!ret)
450aff268cdSMatt Ranostay 				data->pxs_gain = idx;
451aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
452aff268cdSMatt Ranostay 			break;
453aff268cdSMatt Ranostay 		}
454aff268cdSMatt Ranostay 	}
455aff268cdSMatt Ranostay 
456aff268cdSMatt Ranostay 	return ret;
457aff268cdSMatt Ranostay }
458aff268cdSMatt Ranostay 
apds9960_set_als_gain(struct apds9960_data * data,int val)459aff268cdSMatt Ranostay static int apds9960_set_als_gain(struct apds9960_data *data, int val)
460aff268cdSMatt Ranostay {
461aff268cdSMatt Ranostay 	int ret = -EINVAL;
462aff268cdSMatt Ranostay 	int idx;
463aff268cdSMatt Ranostay 
464aff268cdSMatt Ranostay 	for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) {
465aff268cdSMatt Ranostay 		if (apds9960_als_gain_map[idx] == val) {
466aff268cdSMatt Ranostay 			mutex_lock(&data->lock);
467aff268cdSMatt Ranostay 			ret = regmap_update_bits(data->regmap,
468aff268cdSMatt Ranostay 					APDS9960_REG_CONTROL,
469aff268cdSMatt Ranostay 					APDS9960_REG_CONTROL_AGAIN_MASK, idx);
470aff268cdSMatt Ranostay 			if (!ret)
471aff268cdSMatt Ranostay 				data->als_gain = idx;
472aff268cdSMatt Ranostay 			mutex_unlock(&data->lock);
473aff268cdSMatt Ranostay 			break;
474aff268cdSMatt Ranostay 		}
475aff268cdSMatt Ranostay 	}
476aff268cdSMatt Ranostay 
477aff268cdSMatt Ranostay 	return ret;
478aff268cdSMatt Ranostay }
479aff268cdSMatt Ranostay 
480aff268cdSMatt Ranostay #ifdef CONFIG_PM
apds9960_set_power_state(struct apds9960_data * data,bool on)481aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on)
482aff268cdSMatt Ranostay {
483aff268cdSMatt Ranostay 	struct device *dev = &data->client->dev;
484aff268cdSMatt Ranostay 	int ret = 0;
485aff268cdSMatt Ranostay 
486aff268cdSMatt Ranostay 	mutex_lock(&data->lock);
487aff268cdSMatt Ranostay 
488aff268cdSMatt Ranostay 	if (on) {
489aff268cdSMatt Ranostay 		int suspended;
490aff268cdSMatt Ranostay 
491aff268cdSMatt Ranostay 		suspended = pm_runtime_suspended(dev);
492aff268cdSMatt Ranostay 		ret = pm_runtime_get_sync(dev);
493aff268cdSMatt Ranostay 
494aff268cdSMatt Ranostay 		/* Allow one integration cycle before allowing a reading */
495aff268cdSMatt Ranostay 		if (suspended)
496aff268cdSMatt Ranostay 			usleep_range(data->als_adc_int_us,
497aff268cdSMatt Ranostay 				     APDS9960_MAX_INT_TIME_IN_US);
498aff268cdSMatt Ranostay 	} else {
4998386c275SMatt Ranostay 		pm_runtime_mark_last_busy(dev);
500aff268cdSMatt Ranostay 		ret = pm_runtime_put_autosuspend(dev);
501aff268cdSMatt Ranostay 	}
502aff268cdSMatt Ranostay 
503aff268cdSMatt Ranostay 	mutex_unlock(&data->lock);
504aff268cdSMatt Ranostay 
505aff268cdSMatt Ranostay 	return ret;
506aff268cdSMatt Ranostay }
507aff268cdSMatt Ranostay #else
apds9960_set_power_state(struct apds9960_data * data,bool on)508aff268cdSMatt Ranostay static int apds9960_set_power_state(struct apds9960_data *data, bool on)
509aff268cdSMatt Ranostay {
510aff268cdSMatt Ranostay 	return 0;
511aff268cdSMatt Ranostay }
512aff268cdSMatt Ranostay #endif
513aff268cdSMatt Ranostay 
apds9960_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)514aff268cdSMatt Ranostay static int apds9960_read_raw(struct iio_dev *indio_dev,
515aff268cdSMatt Ranostay 			     struct iio_chan_spec const *chan,
516aff268cdSMatt Ranostay 			     int *val, int *val2, long mask)
517aff268cdSMatt Ranostay {
518aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
519801ab335SJonathan Cameron 	__le16 buf;
520aff268cdSMatt Ranostay 	int ret = -EINVAL;
521aff268cdSMatt Ranostay 
522aff268cdSMatt Ranostay 	if (data->gesture_mode_running)
523aff268cdSMatt Ranostay 		return -EBUSY;
524aff268cdSMatt Ranostay 
525aff268cdSMatt Ranostay 	switch (mask) {
526aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_RAW:
527aff268cdSMatt Ranostay 		apds9960_set_power_state(data, true);
528aff268cdSMatt Ranostay 		switch (chan->type) {
529aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
530aff268cdSMatt Ranostay 			ret = regmap_read(data->regmap, chan->address, val);
531aff268cdSMatt Ranostay 			if (!ret)
532aff268cdSMatt Ranostay 				ret = IIO_VAL_INT;
533aff268cdSMatt Ranostay 			break;
534aff268cdSMatt Ranostay 		case IIO_INTENSITY:
535aff268cdSMatt Ranostay 			ret = regmap_bulk_read(data->regmap, chan->address,
536aff268cdSMatt Ranostay 					       &buf, 2);
537f2112259SDan Carpenter 			if (!ret) {
538aff268cdSMatt Ranostay 				ret = IIO_VAL_INT;
539aff268cdSMatt Ranostay 				*val = le16_to_cpu(buf);
540f2112259SDan Carpenter 			}
541aff268cdSMatt Ranostay 			break;
542aff268cdSMatt Ranostay 		default:
543aff268cdSMatt Ranostay 			ret = -EINVAL;
544aff268cdSMatt Ranostay 		}
545aff268cdSMatt Ranostay 		apds9960_set_power_state(data, false);
546aff268cdSMatt Ranostay 		break;
547aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_INT_TIME:
548aff268cdSMatt Ranostay 		/* RGB + ALS sensors only have integration time */
549aff268cdSMatt Ranostay 		mutex_lock(&data->lock);
550aff268cdSMatt Ranostay 		switch (chan->type) {
551aff268cdSMatt Ranostay 		case IIO_INTENSITY:
552aff268cdSMatt Ranostay 			*val = 0;
553aff268cdSMatt Ranostay 			*val2 = data->als_adc_int_us;
554aff268cdSMatt Ranostay 			ret = IIO_VAL_INT_PLUS_MICRO;
555aff268cdSMatt Ranostay 			break;
556aff268cdSMatt Ranostay 		default:
557aff268cdSMatt Ranostay 			ret = -EINVAL;
558aff268cdSMatt Ranostay 		}
559aff268cdSMatt Ranostay 		mutex_unlock(&data->lock);
560aff268cdSMatt Ranostay 		break;
561aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_SCALE:
562aff268cdSMatt Ranostay 		mutex_lock(&data->lock);
563aff268cdSMatt Ranostay 		switch (chan->type) {
564aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
565aff268cdSMatt Ranostay 			*val = apds9960_pxs_gain_map[data->pxs_gain];
566aff268cdSMatt Ranostay 			ret = IIO_VAL_INT;
567aff268cdSMatt Ranostay 			break;
568aff268cdSMatt Ranostay 		case IIO_INTENSITY:
569aff268cdSMatt Ranostay 			*val = apds9960_als_gain_map[data->als_gain];
570aff268cdSMatt Ranostay 			ret = IIO_VAL_INT;
571aff268cdSMatt Ranostay 			break;
572aff268cdSMatt Ranostay 		default:
573aff268cdSMatt Ranostay 			ret = -EINVAL;
574aff268cdSMatt Ranostay 		}
575aff268cdSMatt Ranostay 		mutex_unlock(&data->lock);
576aff268cdSMatt Ranostay 		break;
577*06e91ad3SAbhash Jha 	case IIO_CHAN_INFO_CALIBBIAS:
578*06e91ad3SAbhash Jha 		mutex_lock(&data->lock);
579*06e91ad3SAbhash Jha 		*val = data->calibbias[chan->channel];
580*06e91ad3SAbhash Jha 		ret = IIO_VAL_INT;
581*06e91ad3SAbhash Jha 		mutex_unlock(&data->lock);
582*06e91ad3SAbhash Jha 		break;
583aff268cdSMatt Ranostay 	}
584aff268cdSMatt Ranostay 
585aff268cdSMatt Ranostay 	return ret;
586aff268cdSMatt Ranostay };
587aff268cdSMatt Ranostay 
apds9960_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)588aff268cdSMatt Ranostay static int apds9960_write_raw(struct iio_dev *indio_dev,
589aff268cdSMatt Ranostay 			     struct iio_chan_spec const *chan,
590aff268cdSMatt Ranostay 			     int val, int val2, long mask)
591aff268cdSMatt Ranostay {
592aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
593aff268cdSMatt Ranostay 
594aff268cdSMatt Ranostay 	switch (mask) {
595aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_INT_TIME:
596aff268cdSMatt Ranostay 		/* RGB + ALS sensors only have int time */
597aff268cdSMatt Ranostay 		switch (chan->type) {
598aff268cdSMatt Ranostay 		case IIO_INTENSITY:
599aff268cdSMatt Ranostay 			if (val != 0)
600aff268cdSMatt Ranostay 				return -EINVAL;
601aff268cdSMatt Ranostay 			return apds9960_set_it_time(data, val2);
602aff268cdSMatt Ranostay 		default:
603aff268cdSMatt Ranostay 			return -EINVAL;
604aff268cdSMatt Ranostay 		}
605aff268cdSMatt Ranostay 	case IIO_CHAN_INFO_SCALE:
606aff268cdSMatt Ranostay 		if (val2 != 0)
607aff268cdSMatt Ranostay 			return -EINVAL;
608aff268cdSMatt Ranostay 		switch (chan->type) {
609aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
610aff268cdSMatt Ranostay 			return apds9960_set_pxs_gain(data, val);
611aff268cdSMatt Ranostay 		case IIO_INTENSITY:
612aff268cdSMatt Ranostay 			return apds9960_set_als_gain(data, val);
613aff268cdSMatt Ranostay 		default:
614aff268cdSMatt Ranostay 			return -EINVAL;
615aff268cdSMatt Ranostay 		}
616*06e91ad3SAbhash Jha 	case IIO_CHAN_INFO_CALIBBIAS:
617*06e91ad3SAbhash Jha 		if (val2 != 0)
618*06e91ad3SAbhash Jha 			return -EINVAL;
619*06e91ad3SAbhash Jha 		return apds9960_set_calibbias(data, chan, val);
620aff268cdSMatt Ranostay 	default:
621aff268cdSMatt Ranostay 		return -EINVAL;
622691f4539STom Rix 	}
623aff268cdSMatt Ranostay 
624aff268cdSMatt Ranostay 	return 0;
625aff268cdSMatt Ranostay }
626aff268cdSMatt Ranostay 
apds9960_get_thres_reg(const struct iio_chan_spec * chan,enum iio_event_direction dir,u8 * reg)627aff268cdSMatt Ranostay static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
628aff268cdSMatt Ranostay 					 enum iio_event_direction dir,
629aff268cdSMatt Ranostay 					 u8 *reg)
630aff268cdSMatt Ranostay {
631aff268cdSMatt Ranostay 	switch (dir) {
632aff268cdSMatt Ranostay 	case IIO_EV_DIR_RISING:
633aff268cdSMatt Ranostay 		switch (chan->type) {
634aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
635aff268cdSMatt Ranostay 			*reg = APDS9960_REG_PIHT;
636aff268cdSMatt Ranostay 			break;
637aff268cdSMatt Ranostay 		case IIO_INTENSITY:
638aff268cdSMatt Ranostay 			*reg = APDS9960_REG_AIHTL;
639aff268cdSMatt Ranostay 			break;
640aff268cdSMatt Ranostay 		default:
641aff268cdSMatt Ranostay 			return -EINVAL;
642aff268cdSMatt Ranostay 		}
643aff268cdSMatt Ranostay 		break;
644aff268cdSMatt Ranostay 	case IIO_EV_DIR_FALLING:
645aff268cdSMatt Ranostay 		switch (chan->type) {
646aff268cdSMatt Ranostay 		case IIO_PROXIMITY:
647aff268cdSMatt Ranostay 			*reg = APDS9960_REG_PILT;
648aff268cdSMatt Ranostay 			break;
649aff268cdSMatt Ranostay 		case IIO_INTENSITY:
650aff268cdSMatt Ranostay 			*reg = APDS9960_REG_AILTL;
651aff268cdSMatt Ranostay 			break;
652aff268cdSMatt Ranostay 		default:
653aff268cdSMatt Ranostay 			return -EINVAL;
654aff268cdSMatt Ranostay 		}
655aff268cdSMatt Ranostay 		break;
656aff268cdSMatt Ranostay 	default:
657aff268cdSMatt Ranostay 		return -EINVAL;
658aff268cdSMatt Ranostay 	}
659aff268cdSMatt Ranostay 
660aff268cdSMatt Ranostay 	return 0;
661aff268cdSMatt Ranostay }
662aff268cdSMatt Ranostay 
apds9960_read_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)663aff268cdSMatt Ranostay static int apds9960_read_event(struct iio_dev *indio_dev,
664aff268cdSMatt Ranostay 			       const struct iio_chan_spec *chan,
665aff268cdSMatt Ranostay 			       enum iio_event_type type,
666aff268cdSMatt Ranostay 			       enum iio_event_direction dir,
667aff268cdSMatt Ranostay 			       enum iio_event_info info,
668aff268cdSMatt Ranostay 			       int *val, int *val2)
669aff268cdSMatt Ranostay {
670aff268cdSMatt Ranostay 	u8 reg;
671801ab335SJonathan Cameron 	__le16 buf;
672aff268cdSMatt Ranostay 	int ret = 0;
673aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
674aff268cdSMatt Ranostay 
675aff268cdSMatt Ranostay 	if (info != IIO_EV_INFO_VALUE)
676aff268cdSMatt Ranostay 		return -EINVAL;
677aff268cdSMatt Ranostay 
678aff268cdSMatt Ranostay 	ret = apds9960_get_thres_reg(chan, dir, &reg);
679aff268cdSMatt Ranostay 	if (ret < 0)
680aff268cdSMatt Ranostay 		return ret;
681aff268cdSMatt Ranostay 
682aff268cdSMatt Ranostay 	if (chan->type == IIO_PROXIMITY) {
683aff268cdSMatt Ranostay 		ret = regmap_read(data->regmap, reg, val);
684aff268cdSMatt Ranostay 		if (ret < 0)
685aff268cdSMatt Ranostay 			return ret;
686aff268cdSMatt Ranostay 	} else if (chan->type == IIO_INTENSITY) {
687aff268cdSMatt Ranostay 		ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
688aff268cdSMatt Ranostay 		if (ret < 0)
689aff268cdSMatt Ranostay 			return ret;
690aff268cdSMatt Ranostay 		*val = le16_to_cpu(buf);
691aff268cdSMatt Ranostay 	} else
692aff268cdSMatt Ranostay 		return -EINVAL;
693aff268cdSMatt Ranostay 
694aff268cdSMatt Ranostay 	*val2 = 0;
695aff268cdSMatt Ranostay 
696aff268cdSMatt Ranostay 	return IIO_VAL_INT;
697aff268cdSMatt Ranostay }
698aff268cdSMatt Ranostay 
apds9960_write_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)699aff268cdSMatt Ranostay static int apds9960_write_event(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 				enum iio_event_info info,
704aff268cdSMatt Ranostay 				int val, int val2)
705aff268cdSMatt Ranostay {
706aff268cdSMatt Ranostay 	u8 reg;
707801ab335SJonathan Cameron 	__le16 buf;
708aff268cdSMatt Ranostay 	int ret = 0;
709aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
710aff268cdSMatt Ranostay 
711aff268cdSMatt Ranostay 	if (info != IIO_EV_INFO_VALUE)
712aff268cdSMatt Ranostay 		return -EINVAL;
713aff268cdSMatt Ranostay 
714aff268cdSMatt Ranostay 	ret = apds9960_get_thres_reg(chan, dir, &reg);
715aff268cdSMatt Ranostay 	if (ret < 0)
716aff268cdSMatt Ranostay 		return ret;
717aff268cdSMatt Ranostay 
718aff268cdSMatt Ranostay 	if (chan->type == IIO_PROXIMITY) {
719aff268cdSMatt Ranostay 		if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
720aff268cdSMatt Ranostay 			return -EINVAL;
721aff268cdSMatt Ranostay 		ret = regmap_write(data->regmap, reg, val);
722aff268cdSMatt Ranostay 		if (ret < 0)
723aff268cdSMatt Ranostay 			return ret;
724aff268cdSMatt Ranostay 	} else if (chan->type == IIO_INTENSITY) {
725aff268cdSMatt Ranostay 		if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
726aff268cdSMatt Ranostay 			return -EINVAL;
727aff268cdSMatt Ranostay 		buf = cpu_to_le16(val);
728aff268cdSMatt Ranostay 		ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
729aff268cdSMatt Ranostay 		if (ret < 0)
730aff268cdSMatt Ranostay 			return ret;
731aff268cdSMatt Ranostay 	} else
732aff268cdSMatt Ranostay 		return -EINVAL;
733aff268cdSMatt Ranostay 
734aff268cdSMatt Ranostay 	return 0;
735aff268cdSMatt Ranostay }
736aff268cdSMatt Ranostay 
apds9960_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)737aff268cdSMatt Ranostay static int apds9960_read_event_config(struct iio_dev *indio_dev,
738aff268cdSMatt Ranostay 				      const struct iio_chan_spec *chan,
739aff268cdSMatt Ranostay 				      enum iio_event_type type,
740aff268cdSMatt Ranostay 				      enum iio_event_direction dir)
741aff268cdSMatt Ranostay {
742aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
743aff268cdSMatt Ranostay 
744aff268cdSMatt Ranostay 	switch (chan->type) {
745aff268cdSMatt Ranostay 	case IIO_PROXIMITY:
746aff268cdSMatt Ranostay 		return data->pxs_int;
747aff268cdSMatt Ranostay 	case IIO_INTENSITY:
748aff268cdSMatt Ranostay 		return data->als_int;
749aff268cdSMatt Ranostay 	default:
750aff268cdSMatt Ranostay 		return -EINVAL;
751aff268cdSMatt Ranostay 	}
752aff268cdSMatt Ranostay 
753aff268cdSMatt Ranostay 	return 0;
754aff268cdSMatt Ranostay }
755aff268cdSMatt Ranostay 
apds9960_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)756aff268cdSMatt Ranostay static int apds9960_write_event_config(struct iio_dev *indio_dev,
757aff268cdSMatt Ranostay 				       const struct iio_chan_spec *chan,
758aff268cdSMatt Ranostay 				       enum iio_event_type type,
759aff268cdSMatt Ranostay 				       enum iio_event_direction dir,
760aff268cdSMatt Ranostay 				       int state)
761aff268cdSMatt Ranostay {
762aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
763aff268cdSMatt Ranostay 	int ret;
764aff268cdSMatt Ranostay 
765aff268cdSMatt Ranostay 	state = !!state;
766aff268cdSMatt Ranostay 
767aff268cdSMatt Ranostay 	switch (chan->type) {
768aff268cdSMatt Ranostay 	case IIO_PROXIMITY:
769aff268cdSMatt Ranostay 		if (data->pxs_int == state)
770aff268cdSMatt Ranostay 			return -EINVAL;
771aff268cdSMatt Ranostay 
772aff268cdSMatt Ranostay 		ret = regmap_field_write(data->reg_int_pxs, state);
773aff268cdSMatt Ranostay 		if (ret)
774aff268cdSMatt Ranostay 			return ret;
775aff268cdSMatt Ranostay 		data->pxs_int = state;
776aff268cdSMatt Ranostay 		apds9960_set_power_state(data, state);
777aff268cdSMatt Ranostay 		break;
778aff268cdSMatt Ranostay 	case IIO_INTENSITY:
779aff268cdSMatt Ranostay 		if (data->als_int == state)
780aff268cdSMatt Ranostay 			return -EINVAL;
781aff268cdSMatt Ranostay 
782aff268cdSMatt Ranostay 		ret = regmap_field_write(data->reg_int_als, state);
783aff268cdSMatt Ranostay 		if (ret)
784aff268cdSMatt Ranostay 			return ret;
785aff268cdSMatt Ranostay 		data->als_int = state;
786aff268cdSMatt Ranostay 		apds9960_set_power_state(data, state);
787aff268cdSMatt Ranostay 		break;
788aff268cdSMatt Ranostay 	default:
789aff268cdSMatt Ranostay 		return -EINVAL;
790aff268cdSMatt Ranostay 	}
791aff268cdSMatt Ranostay 
792aff268cdSMatt Ranostay 	return 0;
793aff268cdSMatt Ranostay }
794aff268cdSMatt Ranostay 
795aff268cdSMatt Ranostay static const struct iio_info apds9960_info = {
796aff268cdSMatt Ranostay 	.attrs = &apds9960_attribute_group,
797aff268cdSMatt Ranostay 	.read_raw = apds9960_read_raw,
798aff268cdSMatt Ranostay 	.write_raw = apds9960_write_raw,
799aff268cdSMatt Ranostay 	.read_event_value = apds9960_read_event,
800aff268cdSMatt Ranostay 	.write_event_value = apds9960_write_event,
801aff268cdSMatt Ranostay 	.read_event_config = apds9960_read_event_config,
802aff268cdSMatt Ranostay 	.write_event_config = apds9960_write_event_config,
803aff268cdSMatt Ranostay 
804aff268cdSMatt Ranostay };
805aff268cdSMatt Ranostay 
apds9660_fifo_is_empty(struct apds9960_data * data)806aff268cdSMatt Ranostay static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
807aff268cdSMatt Ranostay {
808aff268cdSMatt Ranostay 	int cnt;
809aff268cdSMatt Ranostay 	int ret;
810aff268cdSMatt Ranostay 
811aff268cdSMatt Ranostay 	ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
812aff268cdSMatt Ranostay 	if (ret)
813aff268cdSMatt Ranostay 		return ret;
814aff268cdSMatt Ranostay 
815aff268cdSMatt Ranostay 	return cnt;
816aff268cdSMatt Ranostay }
817aff268cdSMatt Ranostay 
apds9960_read_gesture_fifo(struct apds9960_data * data)818aff268cdSMatt Ranostay static void apds9960_read_gesture_fifo(struct apds9960_data *data)
819aff268cdSMatt Ranostay {
820aff268cdSMatt Ranostay 	int ret, cnt = 0;
821aff268cdSMatt Ranostay 
822aff268cdSMatt Ranostay 	mutex_lock(&data->lock);
823aff268cdSMatt Ranostay 	data->gesture_mode_running = 1;
824aff268cdSMatt Ranostay 
825655048a0SMatt Ranostay 	while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
826aff268cdSMatt Ranostay 		ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
827aff268cdSMatt Ranostay 				      &data->buffer, 4);
828aff268cdSMatt Ranostay 
829aff268cdSMatt Ranostay 		if (ret)
830aff268cdSMatt Ranostay 			goto err_read;
831aff268cdSMatt Ranostay 
832aff268cdSMatt Ranostay 		iio_push_to_buffers(data->indio_dev, data->buffer);
833655048a0SMatt Ranostay 		cnt--;
834aff268cdSMatt Ranostay 	}
835aff268cdSMatt Ranostay 
836aff268cdSMatt Ranostay err_read:
837aff268cdSMatt Ranostay 	data->gesture_mode_running = 0;
838aff268cdSMatt Ranostay 	mutex_unlock(&data->lock);
839aff268cdSMatt Ranostay }
840aff268cdSMatt Ranostay 
apds9960_interrupt_handler(int irq,void * private)841aff268cdSMatt Ranostay static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
842aff268cdSMatt Ranostay {
843aff268cdSMatt Ranostay 	struct iio_dev *indio_dev = private;
844aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
845aff268cdSMatt Ranostay 	int ret, status;
846aff268cdSMatt Ranostay 
847aff268cdSMatt Ranostay 	ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
848aff268cdSMatt Ranostay 	if (ret < 0) {
849aff268cdSMatt Ranostay 		dev_err(&data->client->dev, "irq status reg read failed\n");
850aff268cdSMatt Ranostay 		return IRQ_HANDLED;
851aff268cdSMatt Ranostay 	}
852aff268cdSMatt Ranostay 
853aff268cdSMatt Ranostay 	if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
854aff268cdSMatt Ranostay 		iio_push_event(indio_dev,
855aff268cdSMatt Ranostay 			       IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
856aff268cdSMatt Ranostay 						    IIO_EV_TYPE_THRESH,
857aff268cdSMatt Ranostay 						    IIO_EV_DIR_EITHER),
858bc2b7dabSGregor Boirie 			       iio_get_time_ns(indio_dev));
859aff268cdSMatt Ranostay 		regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
860aff268cdSMatt Ranostay 	}
861aff268cdSMatt Ranostay 
862aff268cdSMatt Ranostay 	if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
863aff268cdSMatt Ranostay 		iio_push_event(indio_dev,
864aff268cdSMatt Ranostay 			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
865aff268cdSMatt Ranostay 						    IIO_EV_TYPE_THRESH,
866aff268cdSMatt Ranostay 						    IIO_EV_DIR_EITHER),
867bc2b7dabSGregor Boirie 			       iio_get_time_ns(indio_dev));
868aff268cdSMatt Ranostay 		regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
869aff268cdSMatt Ranostay 	}
870aff268cdSMatt Ranostay 
871aff268cdSMatt Ranostay 	if (status & APDS9960_REG_STATUS_GINT)
872aff268cdSMatt Ranostay 		apds9960_read_gesture_fifo(data);
873aff268cdSMatt Ranostay 
874aff268cdSMatt Ranostay 	return IRQ_HANDLED;
875aff268cdSMatt Ranostay }
876aff268cdSMatt Ranostay 
apds9960_set_powermode(struct apds9960_data * data,bool state)877aff268cdSMatt Ranostay static int apds9960_set_powermode(struct apds9960_data *data, bool state)
878aff268cdSMatt Ranostay {
879aff268cdSMatt Ranostay 	return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
880aff268cdSMatt Ranostay }
881aff268cdSMatt Ranostay 
apds9960_buffer_postenable(struct iio_dev * indio_dev)882aff268cdSMatt Ranostay static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
883aff268cdSMatt Ranostay {
884aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
885aff268cdSMatt Ranostay 	int ret;
886aff268cdSMatt Ranostay 
887aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 1);
888aff268cdSMatt Ranostay 	if (ret)
889aff268cdSMatt Ranostay 		return ret;
890aff268cdSMatt Ranostay 
891aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 1);
892aff268cdSMatt Ranostay 	if (ret)
893aff268cdSMatt Ranostay 		return ret;
894aff268cdSMatt Ranostay 
895aff268cdSMatt Ranostay 	pm_runtime_get_sync(&data->client->dev);
896aff268cdSMatt Ranostay 
897aff268cdSMatt Ranostay 	return 0;
898aff268cdSMatt Ranostay }
899aff268cdSMatt Ranostay 
apds9960_buffer_predisable(struct iio_dev * indio_dev)900aff268cdSMatt Ranostay static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
901aff268cdSMatt Ranostay {
902aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
903aff268cdSMatt Ranostay 	int ret;
904aff268cdSMatt Ranostay 
905aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 0);
906aff268cdSMatt Ranostay 	if (ret)
907aff268cdSMatt Ranostay 		return ret;
908aff268cdSMatt Ranostay 
909aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 0);
910aff268cdSMatt Ranostay 	if (ret)
911aff268cdSMatt Ranostay 		return ret;
912aff268cdSMatt Ranostay 
913aff268cdSMatt Ranostay 	pm_runtime_put_autosuspend(&data->client->dev);
914aff268cdSMatt Ranostay 
915aff268cdSMatt Ranostay 	return 0;
916aff268cdSMatt Ranostay }
917aff268cdSMatt Ranostay 
918aff268cdSMatt Ranostay static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
919aff268cdSMatt Ranostay 	.postenable = apds9960_buffer_postenable,
920aff268cdSMatt Ranostay 	.predisable = apds9960_buffer_predisable,
921aff268cdSMatt Ranostay };
922aff268cdSMatt Ranostay 
apds9960_regfield_init(struct apds9960_data * data)923aff268cdSMatt Ranostay static int apds9960_regfield_init(struct apds9960_data *data)
924aff268cdSMatt Ranostay {
925aff268cdSMatt Ranostay 	struct device *dev = &data->client->dev;
926aff268cdSMatt Ranostay 	struct regmap *regmap = data->regmap;
927aff268cdSMatt Ranostay 
928aff268cdSMatt Ranostay 	data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
929aff268cdSMatt Ranostay 						apds9960_reg_field_int_als);
930aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_als)) {
931aff268cdSMatt Ranostay 		dev_err(dev, "INT ALS reg field init failed\n");
932aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_als);
933aff268cdSMatt Ranostay 	}
934aff268cdSMatt Ranostay 
935aff268cdSMatt Ranostay 	data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
936aff268cdSMatt Ranostay 						apds9960_reg_field_int_ges);
937aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_ges)) {
938aff268cdSMatt Ranostay 		dev_err(dev, "INT gesture reg field init failed\n");
939aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_ges);
940aff268cdSMatt Ranostay 	}
941aff268cdSMatt Ranostay 
942aff268cdSMatt Ranostay 	data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
943aff268cdSMatt Ranostay 						apds9960_reg_field_int_pxs);
944aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_int_pxs)) {
945aff268cdSMatt Ranostay 		dev_err(dev, "INT pxs reg field init failed\n");
946aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_int_pxs);
947aff268cdSMatt Ranostay 	}
948aff268cdSMatt Ranostay 
949aff268cdSMatt Ranostay 	data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
950aff268cdSMatt Ranostay 						apds9960_reg_field_enable_als);
951aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_als)) {
952aff268cdSMatt Ranostay 		dev_err(dev, "Enable ALS reg field init failed\n");
953aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_als);
954aff268cdSMatt Ranostay 	}
955aff268cdSMatt Ranostay 
956aff268cdSMatt Ranostay 	data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
957aff268cdSMatt Ranostay 						apds9960_reg_field_enable_ges);
958aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_ges)) {
959aff268cdSMatt Ranostay 		dev_err(dev, "Enable gesture reg field init failed\n");
960aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_ges);
961aff268cdSMatt Ranostay 	}
962aff268cdSMatt Ranostay 
963aff268cdSMatt Ranostay 	data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
964aff268cdSMatt Ranostay 						apds9960_reg_field_enable_pxs);
965aff268cdSMatt Ranostay 	if (IS_ERR(data->reg_enable_pxs)) {
966aff268cdSMatt Ranostay 		dev_err(dev, "Enable PXS reg field init failed\n");
967aff268cdSMatt Ranostay 		return PTR_ERR(data->reg_enable_pxs);
968aff268cdSMatt Ranostay 	}
969aff268cdSMatt Ranostay 
970aff268cdSMatt Ranostay 	return 0;
971aff268cdSMatt Ranostay }
972aff268cdSMatt Ranostay 
apds9960_chip_init(struct apds9960_data * data)973aff268cdSMatt Ranostay static int apds9960_chip_init(struct apds9960_data *data)
974aff268cdSMatt Ranostay {
975aff268cdSMatt Ranostay 	int ret;
976aff268cdSMatt Ranostay 
977aff268cdSMatt Ranostay 	/* Default IT for ALS of 28 ms */
978aff268cdSMatt Ranostay 	ret = apds9960_set_it_time(data, 28000);
979aff268cdSMatt Ranostay 	if (ret)
980aff268cdSMatt Ranostay 		return ret;
981aff268cdSMatt Ranostay 
982aff268cdSMatt Ranostay 	/* Ensure gesture interrupt is OFF */
983aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_ges, 0);
984aff268cdSMatt Ranostay 	if (ret)
985aff268cdSMatt Ranostay 		return ret;
986aff268cdSMatt Ranostay 
987aff268cdSMatt Ranostay 	/* Disable gesture sensor, since polling is useless from user-space */
988aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_ges, 0);
989aff268cdSMatt Ranostay 	if (ret)
990aff268cdSMatt Ranostay 		return ret;
991aff268cdSMatt Ranostay 
992aff268cdSMatt Ranostay 	/* Ensure proximity interrupt is OFF */
993aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_pxs, 0);
994aff268cdSMatt Ranostay 	if (ret)
995aff268cdSMatt Ranostay 		return ret;
996aff268cdSMatt Ranostay 
997aff268cdSMatt Ranostay 	/* Enable proximity sensor for polling */
998aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_pxs, 1);
999aff268cdSMatt Ranostay 	if (ret)
1000aff268cdSMatt Ranostay 		return ret;
1001aff268cdSMatt Ranostay 
1002aff268cdSMatt Ranostay 	/* Ensure ALS interrupt is OFF */
1003aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_int_als, 0);
1004aff268cdSMatt Ranostay 	if (ret)
1005aff268cdSMatt Ranostay 		return ret;
1006aff268cdSMatt Ranostay 
1007aff268cdSMatt Ranostay 	/* Enable ALS sensor for polling */
1008aff268cdSMatt Ranostay 	ret = regmap_field_write(data->reg_enable_als, 1);
1009aff268cdSMatt Ranostay 	if (ret)
1010aff268cdSMatt Ranostay 		return ret;
1011aff268cdSMatt Ranostay 	/*
1012aff268cdSMatt Ranostay 	 * When enabled trigger an interrupt after 3 readings
1013aff268cdSMatt Ranostay 	 * outside threshold for ALS + PXS
1014aff268cdSMatt Ranostay 	 */
1015aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_PERS,
1016aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_PERS);
1017aff268cdSMatt Ranostay 	if (ret)
1018aff268cdSMatt Ranostay 		return ret;
1019aff268cdSMatt Ranostay 
1020aff268cdSMatt Ranostay 	/*
1021aff268cdSMatt Ranostay 	 * Wait for 4 event outside gesture threshold to prevent interrupt
1022aff268cdSMatt Ranostay 	 * flooding.
1023aff268cdSMatt Ranostay 	 */
1024aff268cdSMatt Ranostay 	ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
1025aff268cdSMatt Ranostay 			APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
1026aff268cdSMatt Ranostay 			BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
1027aff268cdSMatt Ranostay 	if (ret)
1028aff268cdSMatt Ranostay 		return ret;
1029aff268cdSMatt Ranostay 
1030aff268cdSMatt Ranostay 	/* Default ENTER and EXIT thresholds for the GESTURE engine. */
1031aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
1032aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_GPENTH);
1033aff268cdSMatt Ranostay 	if (ret)
1034aff268cdSMatt Ranostay 		return ret;
1035aff268cdSMatt Ranostay 
1036aff268cdSMatt Ranostay 	ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
1037aff268cdSMatt Ranostay 			   APDS9960_DEFAULT_GEXTH);
1038aff268cdSMatt Ranostay 	if (ret)
1039aff268cdSMatt Ranostay 		return ret;
1040aff268cdSMatt Ranostay 
1041ebe5c543Skbuild test robot 	return apds9960_set_powermode(data, 1);
1042aff268cdSMatt Ranostay }
1043aff268cdSMatt Ranostay 
apds9960_probe(struct i2c_client * client)1044783964aeSUwe Kleine-König static int apds9960_probe(struct i2c_client *client)
1045aff268cdSMatt Ranostay {
1046aff268cdSMatt Ranostay 	struct apds9960_data *data;
1047aff268cdSMatt Ranostay 	struct iio_dev *indio_dev;
1048aff268cdSMatt Ranostay 	int ret;
1049aff268cdSMatt Ranostay 
1050aff268cdSMatt Ranostay 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1051aff268cdSMatt Ranostay 	if (!indio_dev)
1052aff268cdSMatt Ranostay 		return -ENOMEM;
1053aff268cdSMatt Ranostay 
1054aff268cdSMatt Ranostay 	indio_dev->info = &apds9960_info;
1055aff268cdSMatt Ranostay 	indio_dev->name = APDS9960_DRV_NAME;
1056aff268cdSMatt Ranostay 	indio_dev->channels = apds9960_channels;
1057aff268cdSMatt Ranostay 	indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
1058aff268cdSMatt Ranostay 	indio_dev->available_scan_masks = apds9960_scan_masks;
105917395ce2SAlexandru Ardelean 	indio_dev->modes = INDIO_DIRECT_MODE;
106017395ce2SAlexandru Ardelean 
106117395ce2SAlexandru Ardelean 	ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev,
106217395ce2SAlexandru Ardelean 					  &apds9960_buffer_setup_ops);
106317395ce2SAlexandru Ardelean 	if (ret)
106417395ce2SAlexandru Ardelean 		return ret;
1065aff268cdSMatt Ranostay 
1066aff268cdSMatt Ranostay 	data = iio_priv(indio_dev);
1067aff268cdSMatt Ranostay 	i2c_set_clientdata(client, indio_dev);
1068aff268cdSMatt Ranostay 
1069aff268cdSMatt Ranostay 	data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config);
1070aff268cdSMatt Ranostay 	if (IS_ERR(data->regmap)) {
1071aff268cdSMatt Ranostay 		dev_err(&client->dev, "regmap initialization failed.\n");
1072aff268cdSMatt Ranostay 		return PTR_ERR(data->regmap);
1073aff268cdSMatt Ranostay 	}
1074aff268cdSMatt Ranostay 
1075aff268cdSMatt Ranostay 	data->client = client;
1076aff268cdSMatt Ranostay 	data->indio_dev = indio_dev;
1077aff268cdSMatt Ranostay 	mutex_init(&data->lock);
1078aff268cdSMatt Ranostay 
1079aff268cdSMatt Ranostay 	ret = pm_runtime_set_active(&client->dev);
1080aff268cdSMatt Ranostay 	if (ret)
1081aff268cdSMatt Ranostay 		goto error_power_down;
1082aff268cdSMatt Ranostay 
1083aff268cdSMatt Ranostay 	pm_runtime_enable(&client->dev);
1084aff268cdSMatt Ranostay 	pm_runtime_set_autosuspend_delay(&client->dev, 5000);
1085aff268cdSMatt Ranostay 	pm_runtime_use_autosuspend(&client->dev);
1086aff268cdSMatt Ranostay 
1087aff268cdSMatt Ranostay 	apds9960_set_power_state(data, true);
1088aff268cdSMatt Ranostay 
1089aff268cdSMatt Ranostay 	ret = apds9960_regfield_init(data);
1090aff268cdSMatt Ranostay 	if (ret)
1091aff268cdSMatt Ranostay 		goto error_power_down;
1092aff268cdSMatt Ranostay 
1093aff268cdSMatt Ranostay 	ret = apds9960_chip_init(data);
1094aff268cdSMatt Ranostay 	if (ret)
1095aff268cdSMatt Ranostay 		goto error_power_down;
1096aff268cdSMatt Ranostay 
1097aff268cdSMatt Ranostay 	if (client->irq <= 0) {
1098aff268cdSMatt Ranostay 		dev_err(&client->dev, "no valid irq defined\n");
1099aff268cdSMatt Ranostay 		ret = -EINVAL;
1100aff268cdSMatt Ranostay 		goto error_power_down;
1101aff268cdSMatt Ranostay 	}
1102aff268cdSMatt Ranostay 	ret = devm_request_threaded_irq(&client->dev, client->irq,
1103aff268cdSMatt Ranostay 					NULL, apds9960_interrupt_handler,
1104aff268cdSMatt Ranostay 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1105aff268cdSMatt Ranostay 					"apds9960_event",
1106aff268cdSMatt Ranostay 					indio_dev);
1107aff268cdSMatt Ranostay 	if (ret) {
1108aff268cdSMatt Ranostay 		dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
1109aff268cdSMatt Ranostay 		goto error_power_down;
1110aff268cdSMatt Ranostay 	}
1111aff268cdSMatt Ranostay 
1112aff268cdSMatt Ranostay 	ret = iio_device_register(indio_dev);
1113aff268cdSMatt Ranostay 	if (ret)
1114aff268cdSMatt Ranostay 		goto error_power_down;
1115aff268cdSMatt Ranostay 
1116aff268cdSMatt Ranostay 	apds9960_set_power_state(data, false);
1117aff268cdSMatt Ranostay 
1118aff268cdSMatt Ranostay 	return 0;
1119aff268cdSMatt Ranostay 
1120aff268cdSMatt Ranostay error_power_down:
1121aff268cdSMatt Ranostay 	apds9960_set_power_state(data, false);
1122aff268cdSMatt Ranostay 
1123aff268cdSMatt Ranostay 	return ret;
1124aff268cdSMatt Ranostay }
1125aff268cdSMatt Ranostay 
apds9960_remove(struct i2c_client * client)1126ed5c2f5fSUwe Kleine-König static void apds9960_remove(struct i2c_client *client)
1127aff268cdSMatt Ranostay {
1128aff268cdSMatt Ranostay 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1129aff268cdSMatt Ranostay 	struct apds9960_data *data = iio_priv(indio_dev);
1130aff268cdSMatt Ranostay 
1131aff268cdSMatt Ranostay 	iio_device_unregister(indio_dev);
1132aff268cdSMatt Ranostay 	pm_runtime_disable(&client->dev);
1133aff268cdSMatt Ranostay 	pm_runtime_set_suspended(&client->dev);
1134aff268cdSMatt Ranostay 	apds9960_set_powermode(data, 0);
1135aff268cdSMatt Ranostay }
1136aff268cdSMatt Ranostay 
1137aff268cdSMatt Ranostay #ifdef CONFIG_PM
apds9960_runtime_suspend(struct device * dev)1138aff268cdSMatt Ranostay static int apds9960_runtime_suspend(struct device *dev)
1139aff268cdSMatt Ranostay {
1140aff268cdSMatt Ranostay 	struct apds9960_data *data =
1141aff268cdSMatt Ranostay 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1142aff268cdSMatt Ranostay 
1143aff268cdSMatt Ranostay 	return apds9960_set_powermode(data, 0);
1144aff268cdSMatt Ranostay }
1145aff268cdSMatt Ranostay 
apds9960_runtime_resume(struct device * dev)1146aff268cdSMatt Ranostay static int apds9960_runtime_resume(struct device *dev)
1147aff268cdSMatt Ranostay {
1148aff268cdSMatt Ranostay 	struct apds9960_data *data =
1149aff268cdSMatt Ranostay 			iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
1150aff268cdSMatt Ranostay 
1151aff268cdSMatt Ranostay 	return apds9960_set_powermode(data, 1);
1152aff268cdSMatt Ranostay }
1153aff268cdSMatt Ranostay #endif
1154aff268cdSMatt Ranostay 
1155aff268cdSMatt Ranostay static const struct dev_pm_ops apds9960_pm_ops = {
1156d454ae2eSMatt Ranostay 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1157d454ae2eSMatt Ranostay 				pm_runtime_force_resume)
1158aff268cdSMatt Ranostay 	SET_RUNTIME_PM_OPS(apds9960_runtime_suspend,
1159aff268cdSMatt Ranostay 			   apds9960_runtime_resume, NULL)
1160aff268cdSMatt Ranostay };
1161aff268cdSMatt Ranostay 
1162aff268cdSMatt Ranostay static const struct i2c_device_id apds9960_id[] = {
11634391affaSUwe Kleine-König 	{ "apds9960" },
1164aff268cdSMatt Ranostay 	{}
1165aff268cdSMatt Ranostay };
1166aff268cdSMatt Ranostay MODULE_DEVICE_TABLE(i2c, apds9960_id);
1167aff268cdSMatt Ranostay 
1168b9968e16SMax Leiter static const struct acpi_device_id apds9960_acpi_match[] = {
1169b9968e16SMax Leiter 	{ "MSHW0184" },
1170b9968e16SMax Leiter 	{ }
1171b9968e16SMax Leiter };
1172b9968e16SMax Leiter MODULE_DEVICE_TABLE(acpi, apds9960_acpi_match);
1173b9968e16SMax Leiter 
1174ae4e825bSJavier Martinez Canillas static const struct of_device_id apds9960_of_match[] = {
1175ae4e825bSJavier Martinez Canillas 	{ .compatible = "avago,apds9960" },
1176ae4e825bSJavier Martinez Canillas 	{ }
1177ae4e825bSJavier Martinez Canillas };
1178ae4e825bSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, apds9960_of_match);
1179ae4e825bSJavier Martinez Canillas 
1180aff268cdSMatt Ranostay static struct i2c_driver apds9960_driver = {
1181aff268cdSMatt Ranostay 	.driver = {
1182aff268cdSMatt Ranostay 		.name	= APDS9960_DRV_NAME,
1183ae4e825bSJavier Martinez Canillas 		.of_match_table = apds9960_of_match,
1184aff268cdSMatt Ranostay 		.pm	= &apds9960_pm_ops,
1185b9968e16SMax Leiter 		.acpi_match_table = apds9960_acpi_match,
1186aff268cdSMatt Ranostay 	},
11877cf15f42SUwe Kleine-König 	.probe		= apds9960_probe,
1188aff268cdSMatt Ranostay 	.remove		= apds9960_remove,
1189aff268cdSMatt Ranostay 	.id_table	= apds9960_id,
1190aff268cdSMatt Ranostay };
1191aff268cdSMatt Ranostay module_i2c_driver(apds9960_driver);
1192aff268cdSMatt Ranostay 
1193d6ad8058SMatt Ranostay MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
119450e5bae0SChristophe JAILLET MODULE_DESCRIPTION("APDS9960 Gesture/RGB/ALS/Proximity sensor");
1195aff268cdSMatt Ranostay MODULE_LICENSE("GPL");
1196