28 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
35 #define ISL29003_DRV_NAME "isl29003"
36 #define DRIVER_VERSION "1.0"
38 #define ISL29003_REG_COMMAND 0x00
39 #define ISL29003_ADC_ENABLED (1 << 7)
40 #define ISL29003_ADC_PD (1 << 6)
41 #define ISL29003_TIMING_INT (1 << 5)
42 #define ISL29003_MODE_SHIFT (2)
43 #define ISL29003_MODE_MASK (0x3 << ISL29003_MODE_SHIFT)
44 #define ISL29003_RES_SHIFT (0)
45 #define ISL29003_RES_MASK (0x3 << ISL29003_RES_SHIFT)
47 #define ISL29003_REG_CONTROL 0x01
48 #define ISL29003_INT_FLG (1 << 5)
49 #define ISL29003_RANGE_SHIFT (2)
50 #define ISL29003_RANGE_MASK (0x3 << ISL29003_RANGE_SHIFT)
51 #define ISL29003_INT_PERSISTS_SHIFT (0)
52 #define ISL29003_INT_PERSISTS_MASK (0xf << ISL29003_INT_PERSISTS_SHIFT)
54 #define ISL29003_REG_IRQ_THRESH_HI 0x02
55 #define ISL29003_REG_IRQ_THRESH_LO 0x03
56 #define ISL29003_REG_LSB_SENSOR 0x04
57 #define ISL29003_REG_MSB_SENSOR 0x05
58 #define ISL29003_REG_LSB_TIMER 0x06
59 #define ISL29003_REG_MSB_TIMER 0x07
61 #define ISL29003_NUM_CACHABLE_REGS 4
70 static int gain_range[] = {
71 1000, 4000, 16000, 64000
82 return (data->
reg_cache[reg] & mask) >> shift;
85 static int __isl29003_write_reg(
struct i2c_client *client,
114 static int isl29003_get_range(
struct i2c_client *client)
127 static int isl29003_get_resolution(
struct i2c_client *client)
133 static int isl29003_set_resolution(
struct i2c_client *client,
int res)
140 static int isl29003_get_mode(
struct i2c_client *client)
153 static int isl29003_set_power_state(
struct i2c_client *client,
int state)
160 static int isl29003_get_power_state(
struct i2c_client *client)
167 static int isl29003_get_adc_value(
struct i2c_client *client)
186 range = isl29003_get_range(client);
187 bitdepth = (4 - isl29003_get_resolution(client)) * 4;
188 return (((msb << 8) | lsb) * gain_range[
range]) >> bitdepth;
200 return sprintf(buf,
"%i\n", isl29003_get_range(client));
205 const char *buf,
size_t count)
214 ret = isl29003_set_range(client, val);
222 isl29003_show_range, isl29003_store_range);
231 return sprintf(buf,
"%d\n", isl29003_get_resolution(client));
236 const char *buf,
size_t count)
245 ret = isl29003_set_resolution(client, val);
253 isl29003_show_resolution, isl29003_store_resolution);
260 return sprintf(buf,
"%d\n", isl29003_get_mode(client));
273 ret = isl29003_set_mode(client, val);
281 isl29003_show_mode, isl29003_store_mode);
290 return sprintf(buf,
"%d\n", isl29003_get_power_state(client));
293 static ssize_t isl29003_store_power_state(
struct device *dev,
295 const char *buf,
size_t count)
304 ret = isl29003_set_power_state(client, val);
305 return ret ? ret :
count;
309 isl29003_show_power_state, isl29003_store_power_state);
319 if (!isl29003_get_power_state(client))
322 return sprintf(buf,
"%d\n", isl29003_get_adc_value(client));
327 static struct attribute *isl29003_attributes[] = {
328 &dev_attr_range.attr,
329 &dev_attr_resolution.attr,
331 &dev_attr_power_state.attr,
337 .attrs = isl29003_attributes,
340 static int isl29003_init_client(
struct i2c_client *client)
356 isl29003_set_range(client, 0);
357 isl29003_set_resolution(client, 0);
358 isl29003_set_mode(client, 0);
359 isl29003_set_power_state(client, 0);
383 i2c_set_clientdata(client, data);
387 err = isl29003_init_client(client);
407 isl29003_set_power_state(client, 0);
408 kfree(i2c_get_clientdata(client));
418 return isl29003_set_power_state(client, 0);
431 return isl29003_set_power_state(client,
436 #define isl29003_suspend NULL
437 #define isl29003_resume NULL
453 .probe = isl29003_probe,
455 .id_table = isl29003_id,