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
|
/*
* Copyright (C) 2013 Samsung Electronics. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/errno.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include "sensors_core.h"
#define VENDOR_NAME "CAPELLA"
#define MODEL_NAME "CM3323"
#define MODULE_NAME "light_sensor"
#define I2C_M_WR 0 /* for i2c Write */
#define I2c_M_RD 1 /* for i2c Read */
#define REL_RED REL_HWHEEL
#define REL_GREEN REL_DIAL
#define REL_BLUE REL_WHEEL
#define REL_WHITE REL_MISC
/* register addresses */
/* Ambient light sensor */
#define REG_CS_CONF1 0x00
#define REG_RED 0x08
#define REG_GREEN 0x09
#define REG_BLUE 0x0A
#define REG_WHITE 0x0B
#define LIGHT_LOG_TIME 15 /* 15 sec */
#define ALS_REG_NUM 2
enum {
LIGHT_ENABLED = BIT(0),
PROXIMITY_ENABLED = BIT(1),
};
/* register settings */
static const u8 als_reg_setting[ALS_REG_NUM][2] = {
{REG_CS_CONF1, 0x00}, /* enable */
{REG_CS_CONF1, 0x01}, /* disable */
};
#define CM3323_DEFAULT_DELAY 200000000LL
/* driver data */
struct cm3323_p {
struct i2c_client *i2c_client;
struct input_dev *input;
struct device *light_dev;
struct delayed_work work;
atomic_t delay;
u8 power_state;
u16 color[4];
int irq;
int time_count;
#ifdef CONFIG_SENSORS_ESD_DEFENCE
int zero_cnt;
int reset_cnt;
#endif
};
static int cm3323_i2c_read_word(struct cm3323_p *data,
unsigned char reg_addr, u16 *buf)
{
int ret;
struct i2c_msg msg[2];
unsigned char r_buf[2];
msg[0].addr = data->i2c_client->addr;
msg[0].flags = I2C_M_WR;
msg[0].len = 1;
msg[0].buf = ®_addr;
msg[1].addr = data->i2c_client->addr;
msg[1].flags = I2C_M_RD;
msg[1].len = 2;
msg[1].buf = r_buf;
ret = i2c_transfer(data->i2c_client->adapter, msg, 2);
if (ret < 0) {
pr_err("[SENSOR]: %s - i2c read error %d\n", __func__, ret);
return ret;
}
*buf = (u16)r_buf[1];
*buf = (*buf << 8) | (u16)r_buf[0];
return 0;
}
static int cm3323_i2c_write(struct cm3323_p *data,
unsigned char reg_addr, unsigned char buf)
{
int ret = 0;
struct i2c_msg msg;
unsigned char w_buf[2];
w_buf[0] = reg_addr;
w_buf[1] = buf;
/* send slave address & command */
msg.addr = data->i2c_client->addr;
msg.flags = I2C_M_WR;
msg.len = 2;
msg.buf = (char *)w_buf;
ret = i2c_transfer(data->i2c_client->adapter, &msg, 1);
if (ret < 0) {
pr_err("[SENSOR]: %s - i2c write error %d\n", __func__, ret);
return ret;
}
return 0;
}
static void cm3323_light_enable(struct cm3323_p *data)
{
cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[0][1]);
schedule_delayed_work(&data->work,
nsecs_to_jiffies(atomic_read(&data->delay)));
}
static void cm3323_light_disable(struct cm3323_p *data)
{
/* disable setting */
cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[1][1]);
cancel_delayed_work_sync(&data->work);
}
static void cm3323_work_func_light(struct work_struct *work)
{
struct cm3323_p *data = container_of((struct delayed_work *)work,
struct cm3323_p, work);
unsigned long delay = nsecs_to_jiffies(atomic_read(&data->delay));
cm3323_i2c_read_word(data, REG_RED, &data->color[0]);
cm3323_i2c_read_word(data, REG_GREEN, &data->color[1]);
cm3323_i2c_read_word(data, REG_BLUE, &data->color[2]);
cm3323_i2c_read_word(data, REG_WHITE, &data->color[3]);
input_report_rel(data->input, REL_RED, data->color[0] + 1);
input_report_rel(data->input, REL_GREEN, data->color[1] + 1);
input_report_rel(data->input, REL_BLUE, data->color[2] + 1);
input_report_rel(data->input, REL_WHITE, data->color[3] + 1);
input_sync(data->input);
#ifdef CONFIG_SENSORS_ESD_DEFENCE
if ((data->color[0] == 0) && (data->color[1] == 0)
&& (data->color[3] == 0) && (data->color[2] == 0)
&& (data->reset_cnt < 20))
data->zero_cnt++;
else
data->zero_cnt = 0;
if (data->zero_cnt >= 25) {
pr_info("[SENSOR]: %s - ESD Defence Reset!\n", __func__);
cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[1][1]);
usleep_range(1000, 10000);
cm3323_i2c_write(data, REG_CS_CONF1, als_reg_setting[0][1]);
data->zero_cnt = 0;
data->reset_cnt++;
}
#endif
if (((int64_t)atomic_read(&data->delay) * (int64_t)data->time_count)
>= ((int64_t)LIGHT_LOG_TIME * NSEC_PER_SEC)) {
pr_info("[SENSOR]: %s - r = %u g = %u b = %u w = %u\n",
__func__, data->color[0], data->color[1],
data->color[2], data->color[3]);
data->time_count = 0;
} else {
data->time_count++;
}
schedule_delayed_work(&data->work, delay);
}
/* sysfs */
static ssize_t cm3323_poll_delay_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct cm3323_p *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->delay));
}
static ssize_t cm3323_poll_delay_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct cm3323_p *data = dev_get_drvdata(dev);
int64_t new_delay;
int ret;
ret = kstrtoll(buf, 10, &new_delay);
if (ret) {
pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
return ret;
}
atomic_set(&data->delay, new_delay);
pr_info("[SENSOR]: %s - poll_delay = %lld\n", __func__, new_delay);
return size;
}
static ssize_t light_enable_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
u8 enable;
int ret;
struct cm3323_p *data = dev_get_drvdata(dev);
ret = kstrtou8(buf, 2, &enable);
if (ret) {
pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
return ret;
}
pr_info("[SENSOR]: %s - new_value = %u\n", __func__, enable);
if (enable && !(data->power_state & LIGHT_ENABLED)) {
data->power_state |= LIGHT_ENABLED;
cm3323_light_enable(data);
#ifdef CONFIG_SENSORS_ESD_DEFENCE
data->zero_cnt = 0;
data->reset_cnt = 0;
#endif
} else if (!enable && (data->power_state & LIGHT_ENABLED)) {
cm3323_light_disable(data);
data->power_state &= ~LIGHT_ENABLED;
}
return size;
}
static ssize_t light_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct cm3323_p *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n",
(data->power_state & LIGHT_ENABLED) ? 1 : 0);
}
static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
cm3323_poll_delay_show, cm3323_poll_delay_store);
static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
light_enable_show, light_enable_store);
static struct attribute *light_sysfs_attrs[] = {
&dev_attr_enable.attr,
&dev_attr_poll_delay.attr,
NULL,
};
static struct attribute_group light_attribute_group = {
.attrs = light_sysfs_attrs,
};
/* light sysfs */
static ssize_t cm3323_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
}
static ssize_t cm3323_vendor_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
}
static ssize_t light_lux_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct cm3323_p *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u,%u,%u,%u\n",
data->color[0], data->color[1],
data->color[2], data->color[3]);
}
static ssize_t light_data_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct cm3323_p *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u,%u,%u,%u\n",
data->color[0], data->color[1],
data->color[2], data->color[3]);
}
static DEVICE_ATTR(name, S_IRUGO, cm3323_name_show, NULL);
static DEVICE_ATTR(vendor, S_IRUGO, cm3323_vendor_show, NULL);
static DEVICE_ATTR(lux, S_IRUGO, light_lux_show, NULL);
static DEVICE_ATTR(raw_data, S_IRUGO, light_data_show, NULL);
static struct device_attribute *sensor_attrs[] = {
&dev_attr_name,
&dev_attr_vendor,
&dev_attr_lux,
&dev_attr_raw_data,
NULL,
};
#ifdef CONFIG_OF
static int cm3223_parse_dt(struct cm3323_p *data, struct device *dev)
{
if (data == NULL)
return -ENODEV;
return 0;
}
#else
static int cm3223_parse_dt(struct bma255_p *data, struct device *dev)
{
return -ENODEV;
}
#endif
static int cm3323_setup_reg(struct cm3323_p *data)
{
int ret = 0;
/* ALS initialization */
ret = cm3323_i2c_write(data,
als_reg_setting[0][0],
als_reg_setting[0][1]);
if (ret < 0) {
pr_err("[SENSOR]: %s - cm3323_als_reg is failed. %d\n",
__func__, ret);
return ret;
}
/* turn off */
cm3323_i2c_write(data, REG_CS_CONF1, 0x01);
return ret;
}
static int cm3323_input_init(struct cm3323_p *data)
{
int ret = 0;
struct input_dev *dev;
/* allocate lightsensor input_device */
dev = input_allocate_device();
if (!dev)
return -ENOMEM;
dev->name = MODULE_NAME;
dev->id.bustype = BUS_I2C;
input_set_capability(dev, EV_REL, REL_RED);
input_set_capability(dev, EV_REL, REL_GREEN);
input_set_capability(dev, EV_REL, REL_BLUE);
input_set_capability(dev, EV_REL, REL_WHITE);
input_set_drvdata(dev, data);
ret = input_register_device(dev);
if (ret < 0) {
input_free_device(dev);
return ret;
}
ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
if (ret < 0) {
input_unregister_device(dev);
return ret;
}
ret = sysfs_create_group(&dev->dev.kobj, &light_attribute_group);
if (ret < 0) {
sensors_remove_symlink(&dev->dev.kobj, dev->name);
input_unregister_device(dev);
return ret;
}
data->input = dev;
return 0;
}
static int cm3323_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret = -ENODEV;
struct cm3323_p *data = NULL;
pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
__func__);
goto exit;
}
data = kzalloc(sizeof(struct cm3323_p), GFP_KERNEL);
if (data == NULL) {
pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
ret = -ENOMEM;
goto exit_kzalloc;
}
ret = cm3223_parse_dt(data, &client->dev);
if (ret < 0) {
pr_err("[SENSOR]: %s - of_node error\n", __func__);
ret = -ENODEV;
goto exit_of_node;
}
data->i2c_client = client;
i2c_set_clientdata(client, data);
/* Check if the device is there or not. */
ret = cm3323_setup_reg(data);
if (ret < 0) {
pr_err("[SENSOR]: %s - could not setup regs\n", __func__);
goto exit_setup_reg;
}
/* input device init */
ret = cm3323_input_init(data);
if (ret < 0)
goto exit_input_init;
atomic_set(&data->delay, CM3323_DEFAULT_DELAY);
data->time_count = 0;
INIT_DELAYED_WORK(&data->work, cm3323_work_func_light);
/* set sysfs for light sensor */
sensors_register(data->light_dev, data, sensor_attrs, MODULE_NAME);
pr_info("[SENSOR]: %s - Probe done!\n", __func__);
return 0;
exit_input_init:
exit_setup_reg:
kfree(data);
exit_of_node:
exit_kzalloc:
exit:
pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
return ret;
}
static void cm3323_shutdown(struct i2c_client *client)
{
struct cm3323_p *data = i2c_get_clientdata(client);
pr_info("[SENSOR]: %s\n", __func__);
if (data->power_state & LIGHT_ENABLED)
cm3323_light_disable(data);
}
static int __devexit cm3323_remove(struct i2c_client *client)
{
struct cm3323_p *data = i2c_get_clientdata(client);
/* device off */
if (data->power_state & LIGHT_ENABLED)
cm3323_light_disable(data);
/* sysfs destroy */
sensors_unregister(data->light_dev, sensor_attrs);
sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
/* input device destroy */
sysfs_remove_group(&data->input->dev.kobj, &light_attribute_group);
input_unregister_device(data->input);
kfree(data);
return 0;
}
static int cm3323_suspend(struct device *dev)
{
struct cm3323_p *data = dev_get_drvdata(dev);
if (data->power_state & LIGHT_ENABLED) {
pr_info("[SENSOR]: %s\n", __func__);
cm3323_light_disable(data);
}
return 0;
}
static int cm3323_resume(struct device *dev)
{
struct cm3323_p *data = dev_get_drvdata(dev);
if (data->power_state & LIGHT_ENABLED) {
pr_info("[SENSOR]: %s\n", __func__);
cm3323_light_enable(data);
}
return 0;
}
#ifdef CONFIG_OF
static struct of_device_id cm3323_match_table[] = {
{ .compatible = "light_sensor,cm3323",},
{ },
};
#else
#define cm3323_match_table NULL
#endif
static const struct i2c_device_id cm3323_device_id[] = {
{ "cm3323_match_table", 0 },
{ }
};
static const struct dev_pm_ops cm3323_pm_ops = {
.suspend = cm3323_suspend,
.resume = cm3323_resume
};
static struct i2c_driver cm3323_i2c_driver = {
.driver = {
.name = MODEL_NAME,
.owner = THIS_MODULE,
.pm = &cm3323_pm_ops,
#ifdef CONFIG_OF
.of_match_table = of_match_ptr(cm3323_match_table),
#endif
},
.probe = cm3323_probe,
.shutdown = cm3323_shutdown,
.remove = __devexit_p(cm3323_remove),
.id_table = cm3323_device_id,
};
static int __init cm3323_init(void)
{
return i2c_add_driver(&cm3323_i2c_driver);
}
static void __exit cm3323_exit(void)
{
i2c_del_driver(&cm3323_i2c_driver);
}
module_init(cm3323_init);
module_exit(cm3323_exit);
MODULE_DESCRIPTION("RGB Sensor device driver for cm3323");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");
|