-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl29033.c
648 lines (525 loc) · 15.2 KB
/
isl29033.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* isl29033.c - Linux kernel module for
* Intersil ISL29033 ambient light sensor
*
* Based on code written by, (for isl29003.c)
* Daniel Mack <daniel@caiaq.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <asm/errno.h>
#define ISL29033_DRV_NAME "isl29033"
#define DRIVER_VERSION "1.0"
#define ISL29033_REG_CMD1 0x00
#define ISL29033_REG_CMD2 0x01
#define ISL29033_REG_DATA_LSB 0x02
#define ISL29033_REG_DATA_MSB 0x03
#define ISL29033_REG_INT_LT_LSB 0x04
#define ISL29033_REG_INT_LT_MSB 0x05
#define ISL29033_REG_INT_HT_LSB 0x06
#define ISL29033_REG_INT_HT_MSB 0x07
#define ISL29033_ADC_ENABLED (1 << 7)
#define ISL29033_ADC_PD (1 << 6)
#define ISL29033_INT_FLG (1 << 5)
#define ISL29033_RES_SHIFT (2)
#define ISL29033_RES_MASK (0x3 << ISL29033_RES_SHIFT)
#define ISL29033_MODE_SHIFT (5)
#define ISL29033_MODE_MASK (0x7 << ISL29033_MODE_SHIFT)
#define ISL29033_RANGE_SHIFT (0)
#define ISL29033_RANGE_MASK (0x3 << ISL29033_RANGE_SHIFT)
#define ISL29033_PERSISTS_SHIFT (0)
#define ISL29033_PERSISTS_MASK (0x3 << ISL29033_PERSISTS_SHIFT)
#define ISL29033_NUM_CACHABLE_REGS 4
#define TH_MAX_VAL 65535
#define TH_MIN_VAL 0
struct isl29033_data {
struct i2c_client *client;
struct mutex lock;
u8 reg_cache[ISL29033_NUM_CACHABLE_REGS];
u8 power_state_before_suspend;
};
static int gain_range[] = {
125, 500, 2000, 8000
};
/*
* register access helpers
*/
static int __isl29033_read_reg(struct i2c_client *client,
u32 reg, u8 mask, u8 shift)
{
struct isl29033_data *data = i2c_get_clientdata(client);
return (data->reg_cache[reg] & mask) >> shift;
}
static int __isl29033_write_reg(struct i2c_client *client,
u32 reg, u8 mask, u8 shift, u8 val)
{
struct isl29033_data *data = i2c_get_clientdata(client);
int ret = 0;
u8 tmp;
if (reg >= ISL29033_NUM_CACHABLE_REGS)
return -EINVAL;
mutex_lock(&data->lock);
tmp = data->reg_cache[reg];
tmp &= ~mask;
tmp |= val << shift;
ret = i2c_smbus_write_byte_data(client, reg, tmp);
if (!ret)
data->reg_cache[reg] = tmp;
mutex_unlock(&data->lock);
return ret;
}
/*
* internally used functions
*/
/* range */
static int isl29033_get_range(struct i2c_client *client)
{
return __isl29033_read_reg(client, ISL29033_REG_CMD2,
ISL29033_RANGE_MASK, ISL29033_RANGE_SHIFT);
}
static int isl29033_set_range(struct i2c_client *client, int range)
{
return __isl29033_write_reg(client, ISL29033_REG_CMD2,
ISL29033_RANGE_MASK, ISL29033_RANGE_SHIFT, range);
}
/* resolution */
static int isl29033_get_resolution(struct i2c_client *client)
{
return __isl29033_read_reg(client, ISL29033_REG_CMD2,
ISL29033_RES_MASK, ISL29033_RES_SHIFT);
}
static int isl29033_set_resolution(struct i2c_client *client, int res)
{
return __isl29033_write_reg(client, ISL29033_REG_CMD2,
ISL29033_RES_MASK, ISL29033_RES_SHIFT, res);
}
static int isl29033_set_persist(struct i2c_client *client, int res)
{
return __isl29033_write_reg(client, ISL29033_REG_CMD1,
ISL29033_PERSISTS_MASK, ISL29033_PERSISTS_SHIFT, res);
}
/* mode */
static int isl29033_get_mode(struct i2c_client *client)
{
return __isl29033_read_reg(client, ISL29033_REG_CMD1,
ISL29033_RES_MASK, ISL29033_RES_SHIFT);
}
static int isl29033_set_mode(struct i2c_client *client, int mode)
{
return __isl29033_write_reg(client, ISL29033_REG_CMD1,
ISL29033_MODE_MASK, ISL29033_MODE_SHIFT, mode);
}
/* power_state */
static int isl29033_set_power_state(struct i2c_client *client, int state)
{
return __isl29033_write_reg(client, ISL29033_REG_CMD1,
ISL29033_ADC_ENABLED | ISL29033_ADC_PD, 0,
state ? ISL29033_ADC_ENABLED : ISL29033_ADC_PD);
}
static int isl29033_get_power_state(struct i2c_client *client)
{
//fix
return 0x01;
}
/* HT */
static int isl29033_set_ht(struct i2c_client *client, uint16_t val)
{
int8_t msb = ((val >> 8) & 0xff);
int8_t lsb = ((val >> 0) & 0xff);
int ret;
ret = i2c_smbus_write_byte_data(client, ISL29033_REG_INT_HT_LSB, lsb);
if(ret != 0) {
printk("Failed to set ht value\n");
return ret;
}
ret = i2c_smbus_write_byte_data(client, ISL29033_REG_INT_HT_MSB, msb);
if(ret != 0) {
printk("Failed to set ht value\n");
return ret;
}
return ret;
}
static int isl29033_get_ht(struct i2c_client *client)
{
int msb, lsb;
struct isl29033_data *data = i2c_get_clientdata(client);
mutex_lock(&data->lock);
msb = i2c_smbus_read_byte_data(client, ISL29033_REG_INT_HT_MSB);
if(msb < 0) {
mutex_unlock(&data->lock);
return msb;
}
lsb = i2c_smbus_read_byte_data(client, ISL29033_REG_INT_HT_LSB);
if(lsb < 0) {
mutex_unlock(&data->lock);
return lsb;
}
mutex_unlock(&data->lock);
return (msb << 8) | lsb;
}
/* LT */
static int isl29033_set_lt(struct i2c_client *client, uint16_t val)
{
int8_t msb = ((val >> 8) & 0xff);
int8_t lsb = ((val >> 0) & 0xff);
int ret;
ret = i2c_smbus_write_byte_data(client, ISL29033_REG_INT_LT_LSB, lsb);
if(ret != 0) {
printk("Failed to set ht value\n");
return ret;
}
ret = i2c_smbus_write_byte_data(client, ISL29033_REG_INT_LT_MSB, msb);
if(ret != 0) {
printk("Failed to set ht value\n");
return ret;
}
return ret;
}
static int isl29033_get_lt(struct i2c_client *client)
{
int msb, lsb;
struct isl29033_data *data = i2c_get_clientdata(client);
mutex_lock(&data->lock);
msb = i2c_smbus_read_byte_data(client, ISL29033_REG_INT_LT_MSB);
if(msb < 0) {
mutex_unlock(&data->lock);
return msb;
}
lsb = i2c_smbus_read_byte_data(client, ISL29033_REG_INT_LT_LSB);
if(lsb < 0) {
mutex_unlock(&data->lock);
return lsb;
}
mutex_unlock(&data->lock);
return (msb << 8) | lsb;
}
static int isl29033_get_data(struct i2c_client *client)
{
int msb, lsb;
struct isl29033_data *data = i2c_get_clientdata(client);
mutex_lock(&data->lock);
msb = i2c_smbus_read_byte_data(client, ISL29033_REG_DATA_MSB);
if(msb < 0) {
mutex_unlock(&data->lock);
return msb;
}
lsb = i2c_smbus_read_byte_data(client, ISL29033_REG_DATA_LSB);
if(lsb < 0) {
mutex_unlock(&data->lock);
return lsb;
}
mutex_unlock(&data->lock);
return (msb << 8) | lsb;
}
static int isl29033_get_adc_value(struct i2c_client *client)
{
struct isl29033_data *data = i2c_get_clientdata(client);
int lsb, msb, range, bitdepth;
mutex_lock(&data->lock);
lsb = i2c_smbus_read_byte_data(client, ISL29033_REG_DATA_LSB);
if (lsb < 0) {
mutex_unlock(&data->lock);
return lsb;
}
msb = i2c_smbus_read_byte_data(client, ISL29033_REG_DATA_MSB);
mutex_unlock(&data->lock);
if (msb < 0)
return msb;
range = isl29033_get_range(client);
bitdepth = (4 - isl29033_get_resolution(client)) * 4;
return (((msb << 8) | lsb) * gain_range[range]) >> bitdepth;
}
/*
* sysfs layer
*/
/* range */
static ssize_t isl29033_show_range(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%i\n", isl29033_get_range(client));
}
static ssize_t isl29033_store_range(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) || (val > 3))
return -EINVAL;
ret = isl29033_set_range(client, val);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(range, S_IWUSR | S_IRUGO,
isl29033_show_range, isl29033_store_range);
/* resolution */
static ssize_t isl29033_show_resolution(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_resolution(client));
}
static ssize_t isl29033_store_resolution(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) || (val > 3))
return -EINVAL;
ret = isl29033_set_resolution(client, val);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(resolution, S_IWUSR | S_IRUGO,
isl29033_show_resolution, isl29033_store_resolution);
/* mode */
static ssize_t isl29033_show_mode(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_mode(client));
}
static ssize_t isl29033_store_mode(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) || (val > 2))
return -EINVAL;
ret = isl29033_set_mode(client, val);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
isl29033_show_mode, isl29033_store_mode);
/* Higher Threshold */
static ssize_t isl29033_show_ht(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_ht(client));
}
static ssize_t isl29033_store_ht(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) ||
(val > TH_MAX_VAL) ||
(val < TH_MIN_VAL) )
return -EINVAL;
ret = isl29033_set_ht(client, val);
return ret ? ret : count;
}
static DEVICE_ATTR(ht, S_IWUSR | S_IRUGO,
isl29033_show_ht, isl29033_store_ht);
/* Lower Threshold */
static ssize_t isl29033_show_lt(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_lt(client));
}
static ssize_t isl29033_store_lt(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) ||
(val > TH_MAX_VAL) ||
(val < TH_MIN_VAL) )
return -EINVAL;
ret = isl29033_set_lt(client, val);
return ret ? ret : count;
}
static DEVICE_ATTR(lt, S_IWUSR | S_IRUGO,
isl29033_show_lt, isl29033_store_lt);
/* Data Register */
static ssize_t isl29033_show_data(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_data(client));
}
static DEVICE_ATTR(data, S_IWUSR | S_IRUGO, isl29033_show_data, NULL);
/* power state */
static ssize_t isl29033_show_power_state(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", isl29033_get_power_state(client));
}
static ssize_t isl29033_store_power_state(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
unsigned long val;
int ret;
if ((strict_strtoul(buf, 10, &val) < 0) || (val > 1))
return -EINVAL;
ret = isl29033_set_power_state(client, val);
return ret ? ret : count;
}
static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
isl29033_show_power_state, isl29033_store_power_state);
/* lux */
static ssize_t isl29033_show_lux(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
/* No LUX data if not operational */
if (!isl29033_get_power_state(client))
return -EBUSY;
return sprintf(buf, "%d\n", isl29033_get_adc_value(client));
}
static DEVICE_ATTR(lux, S_IRUGO, isl29033_show_lux, NULL);
static struct attribute *isl29033_attributes[] = {
&dev_attr_range.attr,
&dev_attr_resolution.attr,
&dev_attr_mode.attr,
&dev_attr_power_state.attr,
&dev_attr_lux.attr,
&dev_attr_ht.attr,
&dev_attr_lt.attr,
&dev_attr_data.attr,
NULL
};
static const struct attribute_group isl29033_attr_group = {
.attrs = isl29033_attributes,
};
static int isl29033_init_client(struct i2c_client *client)
{
struct isl29033_data *data = i2c_get_clientdata(client);
int i;
/* read all the registers once to fill the cache.
* if one of the reads fails, we consider the init failed */
for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) {
int v = i2c_smbus_read_byte_data(client, i);
if (v < 0)
return -ENODEV;
data->reg_cache[i] = v;
}
/* set defaults */
isl29033_set_range(client, 0x03);
isl29033_set_resolution(client, 0x00);
isl29033_set_persist(client, 0x03);
isl29033_set_mode(client, 0x05);
return 0;
}
/*
* I2C layer
*/
static int __devinit isl29033_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
struct isl29033_data *data;
int err = 0;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
return -EIO;
data = kzalloc(sizeof(struct isl29033_data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->client = client;
i2c_set_clientdata(client, data);
mutex_init(&data->lock);
/* initialize the ISL29033 chip */
err = isl29033_init_client(client);
if (err)
goto exit_kfree;
/* register sysfs hooks */
err = sysfs_create_group(&client->dev.kobj, &isl29033_attr_group);
if (err)
goto exit_kfree;
dev_info(&client->dev, "driver version %s enabled\n", DRIVER_VERSION);
return 0;
exit_kfree:
kfree(data);
return err;
}
static int __devexit isl29033_remove(struct i2c_client *client)
{
sysfs_remove_group(&client->dev.kobj, &isl29033_attr_group);
isl29033_set_power_state(client, 0);
kfree(i2c_get_clientdata(client));
return 0;
}
#ifdef CONFIG_PM
static int isl29033_suspend(struct i2c_client *client, pm_message_t mesg)
{
struct isl29033_data *data = i2c_get_clientdata(client);
data->power_state_before_suspend = isl29033_get_power_state(client);
return isl29033_set_power_state(client, 0);
}
static int isl29033_resume(struct i2c_client *client)
{
int i;
struct isl29033_data *data = i2c_get_clientdata(client);
/* restore registers from cache */
for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++)
if (i2c_smbus_write_byte_data(client, i, data->reg_cache[i]))
return -EIO;
return isl29033_set_power_state(client,
data->power_state_before_suspend);
}
#else
#define isl29033_suspend NULL
#define isl29033_resume NULL
#endif /* CONFIG_PM */
static const struct i2c_device_id isl29033_id[] = {
{ "isl29033", 0 },
{}
};
MODULE_DEVICE_TABLE(i2c, isl29033_id);
static struct i2c_driver isl29033_driver = {
.driver = {
.name = ISL29033_DRV_NAME,
.owner = THIS_MODULE,
},
.suspend = isl29033_suspend,
.resume = isl29033_resume,
.probe = isl29033_probe,
.remove = __devexit_p(isl29033_remove),
.id_table = isl29033_id,
};
module_i2c_driver(isl29033_driver);
MODULE_AUTHOR("Saiyam Doshi");
MODULE_DESCRIPTION("ISL29033 ambient light sensor driver");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DRIVER_VERSION);