1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * isl29003.c - Linux kernel module for
4 * Intersil ISL29003 ambient light sensor
6 * See file:Documentation/misc-devices/isl29003.rst
8 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
10 * Based on code written by
11 * Rodolfo Giometti <giometti@linux.it>
12 * Eurotech S.p.A. <info@eurotech.it>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
21 #define ISL29003_DRV_NAME "isl29003"
22 #define DRIVER_VERSION "1.0"
24 #define ISL29003_REG_COMMAND 0x00
25 #define ISL29003_ADC_ENABLED (1 << 7)
26 #define ISL29003_ADC_PD (1 << 6)
27 #define ISL29003_TIMING_INT (1 << 5)
28 #define ISL29003_MODE_SHIFT (2)
29 #define ISL29003_MODE_MASK (0x3 << ISL29003_MODE_SHIFT)
30 #define ISL29003_RES_SHIFT (0)
31 #define ISL29003_RES_MASK (0x3 << ISL29003_RES_SHIFT)
33 #define ISL29003_REG_CONTROL 0x01
34 #define ISL29003_INT_FLG (1 << 5)
35 #define ISL29003_RANGE_SHIFT (2)
36 #define ISL29003_RANGE_MASK (0x3 << ISL29003_RANGE_SHIFT)
37 #define ISL29003_INT_PERSISTS_SHIFT (0)
38 #define ISL29003_INT_PERSISTS_MASK (0xf << ISL29003_INT_PERSISTS_SHIFT)
40 #define ISL29003_REG_IRQ_THRESH_HI 0x02
41 #define ISL29003_REG_IRQ_THRESH_LO 0x03
42 #define ISL29003_REG_LSB_SENSOR 0x04
43 #define ISL29003_REG_MSB_SENSOR 0x05
44 #define ISL29003_REG_LSB_TIMER 0x06
45 #define ISL29003_REG_MSB_TIMER 0x07
47 #define ISL29003_NUM_CACHABLE_REGS 4
49 struct isl29003_data
{
50 struct i2c_client
*client
;
52 u8 reg_cache
[ISL29003_NUM_CACHABLE_REGS
];
53 u8 power_state_before_suspend
;
56 static int gain_range
[] = {
57 1000, 4000, 16000, 64000
61 * register access helpers
64 static int __isl29003_read_reg(struct i2c_client
*client
,
65 u32 reg
, u8 mask
, u8 shift
)
67 struct isl29003_data
*data
= i2c_get_clientdata(client
);
69 return (data
->reg_cache
[reg
] & mask
) >> shift
;
72 static int __isl29003_write_reg(struct i2c_client
*client
,
73 u32 reg
, u8 mask
, u8 shift
, u8 val
)
75 struct isl29003_data
*data
= i2c_get_clientdata(client
);
79 if (reg
>= ISL29003_NUM_CACHABLE_REGS
)
82 mutex_lock(&data
->lock
);
84 tmp
= data
->reg_cache
[reg
];
88 ret
= i2c_smbus_write_byte_data(client
, reg
, tmp
);
90 data
->reg_cache
[reg
] = tmp
;
92 mutex_unlock(&data
->lock
);
97 * internally used functions
101 static int isl29003_get_range(struct i2c_client
*client
)
103 return __isl29003_read_reg(client
, ISL29003_REG_CONTROL
,
104 ISL29003_RANGE_MASK
, ISL29003_RANGE_SHIFT
);
107 static int isl29003_set_range(struct i2c_client
*client
, int range
)
109 return __isl29003_write_reg(client
, ISL29003_REG_CONTROL
,
110 ISL29003_RANGE_MASK
, ISL29003_RANGE_SHIFT
, range
);
114 static int isl29003_get_resolution(struct i2c_client
*client
)
116 return __isl29003_read_reg(client
, ISL29003_REG_COMMAND
,
117 ISL29003_RES_MASK
, ISL29003_RES_SHIFT
);
120 static int isl29003_set_resolution(struct i2c_client
*client
, int res
)
122 return __isl29003_write_reg(client
, ISL29003_REG_COMMAND
,
123 ISL29003_RES_MASK
, ISL29003_RES_SHIFT
, res
);
127 static int isl29003_get_mode(struct i2c_client
*client
)
129 return __isl29003_read_reg(client
, ISL29003_REG_COMMAND
,
130 ISL29003_MODE_MASK
, ISL29003_MODE_SHIFT
);
133 static int isl29003_set_mode(struct i2c_client
*client
, int mode
)
135 return __isl29003_write_reg(client
, ISL29003_REG_COMMAND
,
136 ISL29003_MODE_MASK
, ISL29003_MODE_SHIFT
, mode
);
140 static int isl29003_set_power_state(struct i2c_client
*client
, int state
)
142 return __isl29003_write_reg(client
, ISL29003_REG_COMMAND
,
143 ISL29003_ADC_ENABLED
| ISL29003_ADC_PD
, 0,
144 state
? ISL29003_ADC_ENABLED
: ISL29003_ADC_PD
);
147 static int isl29003_get_power_state(struct i2c_client
*client
)
149 struct isl29003_data
*data
= i2c_get_clientdata(client
);
150 u8 cmdreg
= data
->reg_cache
[ISL29003_REG_COMMAND
];
152 return ~cmdreg
& ISL29003_ADC_PD
;
155 static int isl29003_get_adc_value(struct i2c_client
*client
)
157 struct isl29003_data
*data
= i2c_get_clientdata(client
);
158 int lsb
, msb
, range
, bitdepth
;
160 mutex_lock(&data
->lock
);
161 lsb
= i2c_smbus_read_byte_data(client
, ISL29003_REG_LSB_SENSOR
);
164 mutex_unlock(&data
->lock
);
168 msb
= i2c_smbus_read_byte_data(client
, ISL29003_REG_MSB_SENSOR
);
169 mutex_unlock(&data
->lock
);
174 range
= isl29003_get_range(client
);
175 bitdepth
= (4 - isl29003_get_resolution(client
)) * 4;
176 return (((msb
<< 8) | lsb
) * gain_range
[range
]) >> bitdepth
;
184 static ssize_t
isl29003_show_range(struct device
*dev
,
185 struct device_attribute
*attr
, char *buf
)
187 struct i2c_client
*client
= to_i2c_client(dev
);
189 return sprintf(buf
, "%i\n", isl29003_get_range(client
));
192 static ssize_t
isl29003_store_range(struct device
*dev
,
193 struct device_attribute
*attr
,
194 const char *buf
, size_t count
)
196 struct i2c_client
*client
= to_i2c_client(dev
);
200 ret
= kstrtoul(buf
, 10, &val
);
207 ret
= isl29003_set_range(client
, val
);
214 static DEVICE_ATTR(range
, S_IWUSR
| S_IRUGO
,
215 isl29003_show_range
, isl29003_store_range
);
219 static ssize_t
isl29003_show_resolution(struct device
*dev
,
220 struct device_attribute
*attr
,
223 struct i2c_client
*client
= to_i2c_client(dev
);
225 return sprintf(buf
, "%d\n", isl29003_get_resolution(client
));
228 static ssize_t
isl29003_store_resolution(struct device
*dev
,
229 struct device_attribute
*attr
,
230 const char *buf
, size_t count
)
232 struct i2c_client
*client
= to_i2c_client(dev
);
236 ret
= kstrtoul(buf
, 10, &val
);
243 ret
= isl29003_set_resolution(client
, val
);
250 static DEVICE_ATTR(resolution
, S_IWUSR
| S_IRUGO
,
251 isl29003_show_resolution
, isl29003_store_resolution
);
254 static ssize_t
isl29003_show_mode(struct device
*dev
,
255 struct device_attribute
*attr
, char *buf
)
257 struct i2c_client
*client
= to_i2c_client(dev
);
259 return sprintf(buf
, "%d\n", isl29003_get_mode(client
));
262 static ssize_t
isl29003_store_mode(struct device
*dev
,
263 struct device_attribute
*attr
, const char *buf
, size_t count
)
265 struct i2c_client
*client
= to_i2c_client(dev
);
269 ret
= kstrtoul(buf
, 10, &val
);
276 ret
= isl29003_set_mode(client
, val
);
283 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
,
284 isl29003_show_mode
, isl29003_store_mode
);
288 static ssize_t
isl29003_show_power_state(struct device
*dev
,
289 struct device_attribute
*attr
,
292 struct i2c_client
*client
= to_i2c_client(dev
);
294 return sprintf(buf
, "%d\n", isl29003_get_power_state(client
));
297 static ssize_t
isl29003_store_power_state(struct device
*dev
,
298 struct device_attribute
*attr
,
299 const char *buf
, size_t count
)
301 struct i2c_client
*client
= to_i2c_client(dev
);
305 ret
= kstrtoul(buf
, 10, &val
);
312 ret
= isl29003_set_power_state(client
, val
);
313 return ret
? ret
: count
;
316 static DEVICE_ATTR(power_state
, S_IWUSR
| S_IRUGO
,
317 isl29003_show_power_state
, isl29003_store_power_state
);
321 static ssize_t
isl29003_show_lux(struct device
*dev
,
322 struct device_attribute
*attr
, char *buf
)
324 struct i2c_client
*client
= to_i2c_client(dev
);
326 /* No LUX data if not operational */
327 if (!isl29003_get_power_state(client
))
330 return sprintf(buf
, "%d\n", isl29003_get_adc_value(client
));
333 static DEVICE_ATTR(lux
, S_IRUGO
, isl29003_show_lux
, NULL
);
335 static struct attribute
*isl29003_attributes
[] = {
336 &dev_attr_range
.attr
,
337 &dev_attr_resolution
.attr
,
339 &dev_attr_power_state
.attr
,
344 static const struct attribute_group isl29003_attr_group
= {
345 .attrs
= isl29003_attributes
,
348 static int isl29003_init_client(struct i2c_client
*client
)
350 struct isl29003_data
*data
= i2c_get_clientdata(client
);
353 /* read all the registers once to fill the cache.
354 * if one of the reads fails, we consider the init failed */
355 for (i
= 0; i
< ARRAY_SIZE(data
->reg_cache
); i
++) {
356 int v
= i2c_smbus_read_byte_data(client
, i
);
361 data
->reg_cache
[i
] = v
;
365 isl29003_set_range(client
, 0);
366 isl29003_set_resolution(client
, 0);
367 isl29003_set_mode(client
, 0);
368 isl29003_set_power_state(client
, 0);
377 static int isl29003_probe(struct i2c_client
*client
,
378 const struct i2c_device_id
*id
)
380 struct i2c_adapter
*adapter
= client
->adapter
;
381 struct isl29003_data
*data
;
384 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
387 data
= kzalloc(sizeof(struct isl29003_data
), GFP_KERNEL
);
391 data
->client
= client
;
392 i2c_set_clientdata(client
, data
);
393 mutex_init(&data
->lock
);
395 /* initialize the ISL29003 chip */
396 err
= isl29003_init_client(client
);
400 /* register sysfs hooks */
401 err
= sysfs_create_group(&client
->dev
.kobj
, &isl29003_attr_group
);
405 dev_info(&client
->dev
, "driver version %s enabled\n", DRIVER_VERSION
);
413 static int isl29003_remove(struct i2c_client
*client
)
415 sysfs_remove_group(&client
->dev
.kobj
, &isl29003_attr_group
);
416 isl29003_set_power_state(client
, 0);
417 kfree(i2c_get_clientdata(client
));
421 #ifdef CONFIG_PM_SLEEP
422 static int isl29003_suspend(struct device
*dev
)
424 struct i2c_client
*client
= to_i2c_client(dev
);
425 struct isl29003_data
*data
= i2c_get_clientdata(client
);
427 data
->power_state_before_suspend
= isl29003_get_power_state(client
);
428 return isl29003_set_power_state(client
, 0);
431 static int isl29003_resume(struct device
*dev
)
434 struct i2c_client
*client
= to_i2c_client(dev
);
435 struct isl29003_data
*data
= i2c_get_clientdata(client
);
437 /* restore registers from cache */
438 for (i
= 0; i
< ARRAY_SIZE(data
->reg_cache
); i
++)
439 if (i2c_smbus_write_byte_data(client
, i
, data
->reg_cache
[i
]))
442 return isl29003_set_power_state(client
,
443 data
->power_state_before_suspend
);
446 static SIMPLE_DEV_PM_OPS(isl29003_pm_ops
, isl29003_suspend
, isl29003_resume
);
447 #define ISL29003_PM_OPS (&isl29003_pm_ops)
450 #define ISL29003_PM_OPS NULL
451 #endif /* CONFIG_PM_SLEEP */
453 static const struct i2c_device_id isl29003_id
[] = {
457 MODULE_DEVICE_TABLE(i2c
, isl29003_id
);
459 static struct i2c_driver isl29003_driver
= {
461 .name
= ISL29003_DRV_NAME
,
462 .pm
= ISL29003_PM_OPS
,
464 .probe
= isl29003_probe
,
465 .remove
= isl29003_remove
,
466 .id_table
= isl29003_id
,
469 module_i2c_driver(isl29003_driver
);
471 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
472 MODULE_DESCRIPTION("ISL29003 ambient light sensor driver");
473 MODULE_LICENSE("GPL v2");
474 MODULE_VERSION(DRIVER_VERSION
);