xref: /OK3568_Linux_fs/kernel/drivers/misc/isl29003.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  isl29003.c - Linux kernel module for
4*4882a593Smuzhiyun  * 	Intersil ISL29003 ambient light sensor
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  See file:Documentation/misc-devices/isl29003.rst
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  Based on code written by
11*4882a593Smuzhiyun  *  	Rodolfo Giometti <giometti@linux.it>
12*4882a593Smuzhiyun  *  	Eurotech S.p.A. <info@eurotech.it>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/mutex.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define ISL29003_DRV_NAME	"isl29003"
22*4882a593Smuzhiyun #define DRIVER_VERSION		"1.0"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define ISL29003_REG_COMMAND		0x00
25*4882a593Smuzhiyun #define ISL29003_ADC_ENABLED		(1 << 7)
26*4882a593Smuzhiyun #define ISL29003_ADC_PD			(1 << 6)
27*4882a593Smuzhiyun #define ISL29003_TIMING_INT		(1 << 5)
28*4882a593Smuzhiyun #define ISL29003_MODE_SHIFT		(2)
29*4882a593Smuzhiyun #define ISL29003_MODE_MASK		(0x3 << ISL29003_MODE_SHIFT)
30*4882a593Smuzhiyun #define ISL29003_RES_SHIFT		(0)
31*4882a593Smuzhiyun #define ISL29003_RES_MASK		(0x3 << ISL29003_RES_SHIFT)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define ISL29003_REG_CONTROL		0x01
34*4882a593Smuzhiyun #define ISL29003_INT_FLG		(1 << 5)
35*4882a593Smuzhiyun #define ISL29003_RANGE_SHIFT		(2)
36*4882a593Smuzhiyun #define ISL29003_RANGE_MASK		(0x3 << ISL29003_RANGE_SHIFT)
37*4882a593Smuzhiyun #define ISL29003_INT_PERSISTS_SHIFT	(0)
38*4882a593Smuzhiyun #define ISL29003_INT_PERSISTS_MASK	(0xf << ISL29003_INT_PERSISTS_SHIFT)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define ISL29003_REG_IRQ_THRESH_HI	0x02
41*4882a593Smuzhiyun #define ISL29003_REG_IRQ_THRESH_LO	0x03
42*4882a593Smuzhiyun #define ISL29003_REG_LSB_SENSOR		0x04
43*4882a593Smuzhiyun #define ISL29003_REG_MSB_SENSOR		0x05
44*4882a593Smuzhiyun #define ISL29003_REG_LSB_TIMER		0x06
45*4882a593Smuzhiyun #define ISL29003_REG_MSB_TIMER		0x07
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define ISL29003_NUM_CACHABLE_REGS	4
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun struct isl29003_data {
50*4882a593Smuzhiyun 	struct i2c_client *client;
51*4882a593Smuzhiyun 	struct mutex lock;
52*4882a593Smuzhiyun 	u8 reg_cache[ISL29003_NUM_CACHABLE_REGS];
53*4882a593Smuzhiyun 	u8 power_state_before_suspend;
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static int gain_range[] = {
57*4882a593Smuzhiyun 	1000, 4000, 16000, 64000
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun  * register access helpers
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
__isl29003_read_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift)64*4882a593Smuzhiyun static int __isl29003_read_reg(struct i2c_client *client,
65*4882a593Smuzhiyun 			       u32 reg, u8 mask, u8 shift)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return (data->reg_cache[reg] & mask) >> shift;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
__isl29003_write_reg(struct i2c_client * client,u32 reg,u8 mask,u8 shift,u8 val)72*4882a593Smuzhiyun static int __isl29003_write_reg(struct i2c_client *client,
73*4882a593Smuzhiyun 				u32 reg, u8 mask, u8 shift, u8 val)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
76*4882a593Smuzhiyun 	int ret = 0;
77*4882a593Smuzhiyun 	u8 tmp;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (reg >= ISL29003_NUM_CACHABLE_REGS)
80*4882a593Smuzhiyun 		return -EINVAL;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	mutex_lock(&data->lock);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	tmp = data->reg_cache[reg];
85*4882a593Smuzhiyun 	tmp &= ~mask;
86*4882a593Smuzhiyun 	tmp |= val << shift;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	ret = i2c_smbus_write_byte_data(client, reg, tmp);
89*4882a593Smuzhiyun 	if (!ret)
90*4882a593Smuzhiyun 		data->reg_cache[reg] = tmp;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	mutex_unlock(&data->lock);
93*4882a593Smuzhiyun 	return ret;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * internally used functions
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /* range */
isl29003_get_range(struct i2c_client * client)101*4882a593Smuzhiyun static int isl29003_get_range(struct i2c_client *client)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	return __isl29003_read_reg(client, ISL29003_REG_CONTROL,
104*4882a593Smuzhiyun 		ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
isl29003_set_range(struct i2c_client * client,int range)107*4882a593Smuzhiyun static int isl29003_set_range(struct i2c_client *client, int range)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	return __isl29003_write_reg(client, ISL29003_REG_CONTROL,
110*4882a593Smuzhiyun 		ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT, range);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* resolution */
isl29003_get_resolution(struct i2c_client * client)114*4882a593Smuzhiyun static int isl29003_get_resolution(struct i2c_client *client)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
117*4882a593Smuzhiyun 		ISL29003_RES_MASK, ISL29003_RES_SHIFT);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
isl29003_set_resolution(struct i2c_client * client,int res)120*4882a593Smuzhiyun static int isl29003_set_resolution(struct i2c_client *client, int res)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
123*4882a593Smuzhiyun 		ISL29003_RES_MASK, ISL29003_RES_SHIFT, res);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /* mode */
isl29003_get_mode(struct i2c_client * client)127*4882a593Smuzhiyun static int isl29003_get_mode(struct i2c_client *client)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
130*4882a593Smuzhiyun 		ISL29003_RES_MASK, ISL29003_RES_SHIFT);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
isl29003_set_mode(struct i2c_client * client,int mode)133*4882a593Smuzhiyun static int isl29003_set_mode(struct i2c_client *client, int mode)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
136*4882a593Smuzhiyun 		ISL29003_RES_MASK, ISL29003_RES_SHIFT, mode);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* power_state */
isl29003_set_power_state(struct i2c_client * client,int state)140*4882a593Smuzhiyun static int isl29003_set_power_state(struct i2c_client *client, int state)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
143*4882a593Smuzhiyun 				ISL29003_ADC_ENABLED | ISL29003_ADC_PD, 0,
144*4882a593Smuzhiyun 				state ? ISL29003_ADC_ENABLED : ISL29003_ADC_PD);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
isl29003_get_power_state(struct i2c_client * client)147*4882a593Smuzhiyun static int isl29003_get_power_state(struct i2c_client *client)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
150*4882a593Smuzhiyun 	u8 cmdreg = data->reg_cache[ISL29003_REG_COMMAND];
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	return ~cmdreg & ISL29003_ADC_PD;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
isl29003_get_adc_value(struct i2c_client * client)155*4882a593Smuzhiyun static int isl29003_get_adc_value(struct i2c_client *client)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
158*4882a593Smuzhiyun 	int lsb, msb, range, bitdepth;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	mutex_lock(&data->lock);
161*4882a593Smuzhiyun 	lsb = i2c_smbus_read_byte_data(client, ISL29003_REG_LSB_SENSOR);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (lsb < 0) {
164*4882a593Smuzhiyun 		mutex_unlock(&data->lock);
165*4882a593Smuzhiyun 		return lsb;
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	msb = i2c_smbus_read_byte_data(client, ISL29003_REG_MSB_SENSOR);
169*4882a593Smuzhiyun 	mutex_unlock(&data->lock);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	if (msb < 0)
172*4882a593Smuzhiyun 		return msb;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	range = isl29003_get_range(client);
175*4882a593Smuzhiyun 	bitdepth = (4 - isl29003_get_resolution(client)) * 4;
176*4882a593Smuzhiyun 	return (((msb << 8) | lsb) * gain_range[range]) >> bitdepth;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun  * sysfs layer
181*4882a593Smuzhiyun  */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /* range */
isl29003_show_range(struct device * dev,struct device_attribute * attr,char * buf)184*4882a593Smuzhiyun static ssize_t isl29003_show_range(struct device *dev,
185*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return sprintf(buf, "%i\n", isl29003_get_range(client));
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
isl29003_store_range(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)192*4882a593Smuzhiyun static ssize_t isl29003_store_range(struct device *dev,
193*4882a593Smuzhiyun 				    struct device_attribute *attr,
194*4882a593Smuzhiyun 				    const char *buf, size_t count)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
197*4882a593Smuzhiyun 	unsigned long val;
198*4882a593Smuzhiyun 	int ret;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	ret = kstrtoul(buf, 10, &val);
201*4882a593Smuzhiyun 	if (ret)
202*4882a593Smuzhiyun 		return ret;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (val > 3)
205*4882a593Smuzhiyun 		return -EINVAL;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	ret = isl29003_set_range(client, val);
208*4882a593Smuzhiyun 	if (ret < 0)
209*4882a593Smuzhiyun 		return ret;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	return count;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun static DEVICE_ATTR(range, S_IWUSR | S_IRUGO,
215*4882a593Smuzhiyun 		   isl29003_show_range, isl29003_store_range);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /* resolution */
isl29003_show_resolution(struct device * dev,struct device_attribute * attr,char * buf)219*4882a593Smuzhiyun static ssize_t isl29003_show_resolution(struct device *dev,
220*4882a593Smuzhiyun 					struct device_attribute *attr,
221*4882a593Smuzhiyun 					char *buf)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", isl29003_get_resolution(client));
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
isl29003_store_resolution(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)228*4882a593Smuzhiyun static ssize_t isl29003_store_resolution(struct device *dev,
229*4882a593Smuzhiyun 					 struct device_attribute *attr,
230*4882a593Smuzhiyun 					 const char *buf, size_t count)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
233*4882a593Smuzhiyun 	unsigned long val;
234*4882a593Smuzhiyun 	int ret;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	ret = kstrtoul(buf, 10, &val);
237*4882a593Smuzhiyun 	if (ret)
238*4882a593Smuzhiyun 		return ret;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (val > 3)
241*4882a593Smuzhiyun 		return -EINVAL;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	ret = isl29003_set_resolution(client, val);
244*4882a593Smuzhiyun 	if (ret < 0)
245*4882a593Smuzhiyun 		return ret;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return count;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun static DEVICE_ATTR(resolution, S_IWUSR | S_IRUGO,
251*4882a593Smuzhiyun 		   isl29003_show_resolution, isl29003_store_resolution);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /* mode */
isl29003_show_mode(struct device * dev,struct device_attribute * attr,char * buf)254*4882a593Smuzhiyun static ssize_t isl29003_show_mode(struct device *dev,
255*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", isl29003_get_mode(client));
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
isl29003_store_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)262*4882a593Smuzhiyun static ssize_t isl29003_store_mode(struct device *dev,
263*4882a593Smuzhiyun 		struct device_attribute *attr, const char *buf, size_t count)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
266*4882a593Smuzhiyun 	unsigned long val;
267*4882a593Smuzhiyun 	int ret;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	ret = kstrtoul(buf, 10, &val);
270*4882a593Smuzhiyun 	if (ret)
271*4882a593Smuzhiyun 		return ret;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (val > 2)
274*4882a593Smuzhiyun 		return -EINVAL;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	ret = isl29003_set_mode(client, val);
277*4882a593Smuzhiyun 	if (ret < 0)
278*4882a593Smuzhiyun 		return ret;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	return count;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
284*4882a593Smuzhiyun 		   isl29003_show_mode, isl29003_store_mode);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /* power state */
isl29003_show_power_state(struct device * dev,struct device_attribute * attr,char * buf)288*4882a593Smuzhiyun static ssize_t isl29003_show_power_state(struct device *dev,
289*4882a593Smuzhiyun 					 struct device_attribute *attr,
290*4882a593Smuzhiyun 					 char *buf)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", isl29003_get_power_state(client));
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
isl29003_store_power_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)297*4882a593Smuzhiyun static ssize_t isl29003_store_power_state(struct device *dev,
298*4882a593Smuzhiyun 					  struct device_attribute *attr,
299*4882a593Smuzhiyun 					  const char *buf, size_t count)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
302*4882a593Smuzhiyun 	unsigned long val;
303*4882a593Smuzhiyun 	int ret;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	ret = kstrtoul(buf, 10, &val);
306*4882a593Smuzhiyun 	if (ret)
307*4882a593Smuzhiyun 		return ret;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	if (val > 1)
310*4882a593Smuzhiyun 		return -EINVAL;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	ret = isl29003_set_power_state(client, val);
313*4882a593Smuzhiyun 	return ret ? ret : count;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
317*4882a593Smuzhiyun 		   isl29003_show_power_state, isl29003_store_power_state);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /* lux */
isl29003_show_lux(struct device * dev,struct device_attribute * attr,char * buf)321*4882a593Smuzhiyun static ssize_t isl29003_show_lux(struct device *dev,
322*4882a593Smuzhiyun 				 struct device_attribute *attr, char *buf)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	/* No LUX data if not operational */
327*4882a593Smuzhiyun 	if (!isl29003_get_power_state(client))
328*4882a593Smuzhiyun 		return -EBUSY;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", isl29003_get_adc_value(client));
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun static DEVICE_ATTR(lux, S_IRUGO, isl29003_show_lux, NULL);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun static struct attribute *isl29003_attributes[] = {
336*4882a593Smuzhiyun 	&dev_attr_range.attr,
337*4882a593Smuzhiyun 	&dev_attr_resolution.attr,
338*4882a593Smuzhiyun 	&dev_attr_mode.attr,
339*4882a593Smuzhiyun 	&dev_attr_power_state.attr,
340*4882a593Smuzhiyun 	&dev_attr_lux.attr,
341*4882a593Smuzhiyun 	NULL
342*4882a593Smuzhiyun };
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun static const struct attribute_group isl29003_attr_group = {
345*4882a593Smuzhiyun 	.attrs = isl29003_attributes,
346*4882a593Smuzhiyun };
347*4882a593Smuzhiyun 
isl29003_init_client(struct i2c_client * client)348*4882a593Smuzhiyun static int isl29003_init_client(struct i2c_client *client)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
351*4882a593Smuzhiyun 	int i;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	/* read all the registers once to fill the cache.
354*4882a593Smuzhiyun 	 * if one of the reads fails, we consider the init failed */
355*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) {
356*4882a593Smuzhiyun 		int v = i2c_smbus_read_byte_data(client, i);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		if (v < 0)
359*4882a593Smuzhiyun 			return -ENODEV;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 		data->reg_cache[i] = v;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	/* set defaults */
365*4882a593Smuzhiyun 	isl29003_set_range(client, 0);
366*4882a593Smuzhiyun 	isl29003_set_resolution(client, 0);
367*4882a593Smuzhiyun 	isl29003_set_mode(client, 0);
368*4882a593Smuzhiyun 	isl29003_set_power_state(client, 0);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	return 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /*
374*4882a593Smuzhiyun  * I2C layer
375*4882a593Smuzhiyun  */
376*4882a593Smuzhiyun 
isl29003_probe(struct i2c_client * client,const struct i2c_device_id * id)377*4882a593Smuzhiyun static int isl29003_probe(struct i2c_client *client,
378*4882a593Smuzhiyun 				    const struct i2c_device_id *id)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct i2c_adapter *adapter = client->adapter;
381*4882a593Smuzhiyun 	struct isl29003_data *data;
382*4882a593Smuzhiyun 	int err = 0;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
385*4882a593Smuzhiyun 		return -EIO;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	data = kzalloc(sizeof(struct isl29003_data), GFP_KERNEL);
388*4882a593Smuzhiyun 	if (!data)
389*4882a593Smuzhiyun 		return -ENOMEM;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	data->client = client;
392*4882a593Smuzhiyun 	i2c_set_clientdata(client, data);
393*4882a593Smuzhiyun 	mutex_init(&data->lock);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	/* initialize the ISL29003 chip */
396*4882a593Smuzhiyun 	err = isl29003_init_client(client);
397*4882a593Smuzhiyun 	if (err)
398*4882a593Smuzhiyun 		goto exit_kfree;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* register sysfs hooks */
401*4882a593Smuzhiyun 	err = sysfs_create_group(&client->dev.kobj, &isl29003_attr_group);
402*4882a593Smuzhiyun 	if (err)
403*4882a593Smuzhiyun 		goto exit_kfree;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	dev_info(&client->dev, "driver version %s enabled\n", DRIVER_VERSION);
406*4882a593Smuzhiyun 	return 0;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun exit_kfree:
409*4882a593Smuzhiyun 	kfree(data);
410*4882a593Smuzhiyun 	return err;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
isl29003_remove(struct i2c_client * client)413*4882a593Smuzhiyun static int isl29003_remove(struct i2c_client *client)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	sysfs_remove_group(&client->dev.kobj, &isl29003_attr_group);
416*4882a593Smuzhiyun 	isl29003_set_power_state(client, 0);
417*4882a593Smuzhiyun 	kfree(i2c_get_clientdata(client));
418*4882a593Smuzhiyun 	return 0;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
isl29003_suspend(struct device * dev)422*4882a593Smuzhiyun static int isl29003_suspend(struct device *dev)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
425*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	data->power_state_before_suspend = isl29003_get_power_state(client);
428*4882a593Smuzhiyun 	return isl29003_set_power_state(client, 0);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
isl29003_resume(struct device * dev)431*4882a593Smuzhiyun static int isl29003_resume(struct device *dev)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun 	int i;
434*4882a593Smuzhiyun 	struct i2c_client *client = to_i2c_client(dev);
435*4882a593Smuzhiyun 	struct isl29003_data *data = i2c_get_clientdata(client);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/* restore registers from cache */
438*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++)
439*4882a593Smuzhiyun 		if (i2c_smbus_write_byte_data(client, i, data->reg_cache[i]))
440*4882a593Smuzhiyun 			return -EIO;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	return isl29003_set_power_state(client,
443*4882a593Smuzhiyun 		data->power_state_before_suspend);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(isl29003_pm_ops, isl29003_suspend, isl29003_resume);
447*4882a593Smuzhiyun #define ISL29003_PM_OPS (&isl29003_pm_ops)
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun #else
450*4882a593Smuzhiyun #define ISL29003_PM_OPS NULL
451*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun static const struct i2c_device_id isl29003_id[] = {
454*4882a593Smuzhiyun 	{ "isl29003", 0 },
455*4882a593Smuzhiyun 	{}
456*4882a593Smuzhiyun };
457*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, isl29003_id);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun static struct i2c_driver isl29003_driver = {
460*4882a593Smuzhiyun 	.driver = {
461*4882a593Smuzhiyun 		.name	= ISL29003_DRV_NAME,
462*4882a593Smuzhiyun 		.pm	= ISL29003_PM_OPS,
463*4882a593Smuzhiyun 	},
464*4882a593Smuzhiyun 	.probe	= isl29003_probe,
465*4882a593Smuzhiyun 	.remove	= isl29003_remove,
466*4882a593Smuzhiyun 	.id_table = isl29003_id,
467*4882a593Smuzhiyun };
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun module_i2c_driver(isl29003_driver);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
472*4882a593Smuzhiyun MODULE_DESCRIPTION("ISL29003 ambient light sensor driver");
473*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
474*4882a593Smuzhiyun MODULE_VERSION(DRIVER_VERSION);
475