hc
2024-08-16 62c46c9150c4afde7e5b25436263fddf79d66f0b
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
/* drivers/input/sensors/lsensor/cm3218.c
 *
 * Copyright (C) 2012-2016 ROCKCHIP.
 * Author: luowei <lw@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#include <linux/of_gpio.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/sensor-dev.h>
 
#define LENSFACTOR 1000
 
/* SMBus ARA Address */
#define    CM3218_ADDR_ARA            0x0C
 
/* CM3218 CMD Registers */
#define    CM3218_REG_ADDR_CMD        0x00
#define    CM3218_CMD_ALS_SD        0x0001
#define    CM3218_CMD_ALS_INT_EN        0x0002
#define    CM3218_CMD_ALS_INT_DISABLE 0x0000
 
#define    CM3218_CMD_ALS_IT_SHIFT        6
#define    CM3218_CMD_ALS_IT_MASK        (3 << CM3218_CMD_ALS_IT_SHIFT)
#define    CM3218_CMD_ALS_IT_05T        (0 << CM3218_CMD_ALS_IT_SHIFT)
#define    CM3218_CMD_ALS_IT_1T        (1 << CM3218_CMD_ALS_IT_SHIFT)
#define    CM3218_CMD_ALS_IT_2T        (2 << CM3218_CMD_ALS_IT_SHIFT)
#define    CM3218_CMD_ALS_IT_4T        (3 << CM3218_CMD_ALS_IT_SHIFT)
#define    CM3218_DEFAULT_CMD        (CM3218_CMD_ALS_IT_1T)
 
#define    CM3218_ALS_PERS 0x0020
 
#define    CM3218_REG_ADDR_ALS_WH        0x01
#define    CM3218_DEFAULT_ALS_WH        0x000C
 
#define    CM3218_REG_ADDR_ALS_WL        0x02
#define    CM3218_DEFAULT_ALS_WL        0x000A
 
#define    CM3218_REG_ADDR_ALS        0x04
 
#define    CM3218_REG_ADDR_STATUS        0x06
 
#define    CM3218_REG_ADDR_ID        0x07
 
/* Software parameters */
#define    CM3218_MAX_CACHE_REGS        (0x03 + 1)    /* Reg.0x00 to 0x03 */
 
static int als_code = 20;
static int change_sensitivity = 50;
static int if_sensor_init;
 
static int cm3218_read_ara(struct i2c_client *client)
{
   int status;
   unsigned short addr;
 
   addr = client->addr;
   client->addr = CM3218_ADDR_ARA;
   status = i2c_smbus_read_byte(client);
   client->addr = addr;
 
   if (status < 0)
       return -ENODEV;
 
   return 0;
}
 
static int cm3218_write(struct i2c_client *client, u8 reg, u16 value)
{
   u16 regval;
   int ret;
 
   dev_dbg(&client->dev,
       "Write to device register 0x%02X with 0x%04X\n", reg, value);
   regval = cpu_to_le16(value);
   ret = i2c_smbus_write_word_data(client, reg, regval);
   if (ret)
       dev_err(&client->dev, "Write to device fails: 0x%x\n", ret);
 
   return ret;
}
 
static int cm3218_read(struct i2c_client *client, u8 reg)
{
   int regval;
   int status;
 
   status = i2c_smbus_read_word_data(client, reg);
   if (status < 0) {
       dev_err(&client->dev,
           "Error in reading Reg.0x%02X\n", reg);
       return status;
   }
   regval = le16_to_cpu(status);
 
   dev_dbg(&client->dev,
       "Read from device register 0x%02X = 0x%04X\n",
       reg, regval);
 
   return regval;
}
 
static int clear_interrupt(struct i2c_client *client)
{
   int status = 0;
 
   status = cm3218_read(client, CM3218_REG_ADDR_STATUS);
   if (status < 0) {
       dev_err(&client->dev, "CM3218: clean interrupt flag fail\n");
       return status;
   }
 
   return status;
}
 
static int enable_interrupt(struct i2c_client *client)
{
   int status = 0;
 
   status = cm3218_write(
       client,
       CM3218_REG_ADDR_CMD,
       CM3218_DEFAULT_CMD |
       CM3218_CMD_ALS_INT_EN |
       CM3218_ALS_PERS);
 
   if (status < 0) {
       dev_err(&client->dev, "CM3128 enable interrut fails\n");
       return status;
   }
 
   return status;
}
 
static int disable_interrupt(struct i2c_client *client)
{
   int status = 0;
 
   status = cm3218_write(
       client,
       CM3218_REG_ADDR_CMD,
       CM3218_DEFAULT_CMD |
       CM3218_CMD_ALS_INT_DISABLE);
 
   if (status < 0) {
       dev_err(&client->dev, "CM3218: disable interrput fail\n");
       return status;
   }
 
   return status;
}
 
static int set_als_int_threshold(struct i2c_client *client)
{
   int threshold_high, threshold_low;
   int status = 0;
 
   /* Set ALS high threshold */
   threshold_high = als_code * (100 + change_sensitivity) / 100;
   if (threshold_high > 65535)
       threshold_high = 65535;
   status = cm3218_write(client, CM3218_REG_ADDR_ALS_WH, threshold_high);
   if (status < 0) {
       dev_err(&client->dev, "Init CM3218 CMD fails, set als wh error\n");
       return status;
   }
 
   /* Set ALS low threshold */
   threshold_low = als_code * (100 - change_sensitivity) / 100;
   status = cm3218_write(client, CM3218_REG_ADDR_ALS_WL, threshold_low);
   if (status < 0) {
       dev_err(&client->dev, "Init CM3218 CMD fails, set als wh error\n");
       return status;
   }
 
   return status;
}
 
/****************operate according to sensor chip:start************/
 
static int sensor_active(struct i2c_client *client, int enable, int rate)
{
   struct sensor_private_data *sensor =
       (struct sensor_private_data *)i2c_get_clientdata(client);
   int result = 0;
   int status = 0;
 
   if (if_sensor_init)
       return 0;
 
   sensor->client->addr = sensor->ops->ctrl_reg;
   sensor->ops->ctrl_data = cm3218_read(client, sensor->client->addr);
 
   /* register setting according to chip datasheet */
   if (!enable) {
       status = CM3218_CMD_ALS_SD;    /* cm3218 */
       sensor->ops->ctrl_data |= status;
   } else {
       status = ~CM3218_CMD_ALS_SD;    /* cm3218 */
       sensor->ops->ctrl_data &= status;
   }
 
   DBG("%s:reg=0x%x, reg_ctrl=0x%x, enable=%d\n",
           __func__,
           sensor->ops->ctrl_reg,
           sensor->ops->ctrl_data,
           enable);
 
   result = cm3218_write(
       client,
       sensor->client->addr,
       sensor->ops->ctrl_data);
 
   if (result)
       dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
 
   return result;
}
 
static int sensor_init(struct i2c_client *client)
{
   int status, i;
   struct sensor_private_data *sensor =
       (struct sensor_private_data *)i2c_get_clientdata(client);
 
   if (if_sensor_init)
       return 0;
 
   for (i = 0; i < 5; i++) {
       status = cm3218_write(
           client, CM3218_REG_ADDR_CMD,
           CM3218_CMD_ALS_SD);
 
       if (status >= 0)
           break;
       cm3218_read_ara(client);
   }
 
   status = cm3218_write(client, CM3218_REG_ADDR_CMD, CM3218_DEFAULT_CMD);
   if (status < 0) {
       dev_err(&client->dev, "Init CM3218 CMD fails\n");
       return status;
   }
 
   if (sensor->pdata->irq_enable) {
       set_als_int_threshold(client);
 
       enable_interrupt(client);
   }
 
   if_sensor_init = 1;
   msleep(250);
 
   return status;
}
 
static int light_report_value(struct input_dev *input, int data)
{
   unsigned char index = 0;
 
   if (data <= 700) {
       index = 0;
   } else if (data <= 1400) {
       index = 1;
   } else if (data <= 2800) {
       index = 2;
   } else if (data <= 5600) {
       index = 3;
   } else if (data <= 11200) {
       index = 4;
   } else if (data <= 22400) {
       index = 5;
   } else if (data <= 44800) {
       index = 6;
   } else {
       index = 7;
   }
 
   input_report_abs(input, ABS_MISC, index);
   input_sync(input);
 
   return index;
}
 
static int cm3218_read_lux(struct i2c_client *client, int *lux)
{
   int lux_data;
 
   lux_data = cm3218_read(client, CM3218_REG_ADDR_ALS);
   if (lux_data < 0) {
       dev_err(&client->dev, "Error in reading Lux DATA\n");
       return lux_data;
   }
 
   dev_vdbg(&client->dev, "lux = %u\n", lux_data);
 
   if (lux_data < 0)
       return lux_data;
 
   *lux  = lux_data * LENSFACTOR;
   *lux /= 1000;
   return 0;
}
 
static int sensor_report_value(struct i2c_client *client)
{
   struct sensor_private_data *sensor =
       (struct sensor_private_data *)i2c_get_clientdata(client);
   int result = 0;
   int index = 0;
 
   /* clean interrupt flag and disable it during reading data */
   if (sensor->pdata->irq_enable) {
       clear_interrupt(client);
       disable_interrupt(client);
   }
 
   cm3218_read_lux(client, &result);
   als_code = result;
   index = light_report_value(sensor->input_dev, 200 * result);
 
   /* enable interrupt after read data */
   if (sensor->pdata->irq_enable) {
       set_als_int_threshold(client);
       enable_interrupt(client);
   }
 
   return 0;
}
 
static struct sensor_operate light_cm3218_ops = {
   .name                = "cm3218",
   /* sensor type and it should be correct */
   .type                = SENSOR_TYPE_LIGHT,
   /* i2c id number */
   .id_i2c                = LIGHT_ID_CM3218,
   /* read data */
   .read_reg            = CM3218_REG_ADDR_ALS,
   .read_len            = 2, /* data length */
   /* read device id from this register */
   .id_reg                = SENSOR_UNKNOW_DATA,
   /* device id */
   .id_data            = SENSOR_UNKNOW_DATA,
   .precision            = 16, /* 8 bits */
   /* enable or disable */
   .ctrl_reg            = CM3218_REG_ADDR_CMD,
   /* intterupt status register */
   .int_status_reg            = CM3218_REG_ADDR_STATUS,
   /* range */
   .range                = {0, 65535},
   /* brightness */
   .brightness            = {10, 255},
   .trig                = SENSOR_UNKNOW_DATA,
   .active                = sensor_active,
   .init                = sensor_init,
   .report                = sensor_report_value,
};
 
/****************operate according to sensor chip:end************/
static int light_cm3218_probe(struct i2c_client *client,
                 const struct i2c_device_id *devid)
{
   return sensor_register_device(client, NULL, devid, &light_cm3218_ops);
}
 
static int light_cm3218_remove(struct i2c_client *client)
{
   return sensor_unregister_device(client, NULL, &light_cm3218_ops);
}
 
static const struct i2c_device_id light_cm3218_id[] = {
   {"light_cm3218", LIGHT_ID_CM3218},
   {}
};
 
static struct i2c_driver light_cm3218_driver = {
   .probe = light_cm3218_probe,
   .remove = light_cm3218_remove,
   .shutdown = sensor_shutdown,
   .id_table = light_cm3218_id,
   .driver = {
       .name = "light_cm3218",
   #ifdef CONFIG_PM
       .pm = &sensor_pm_ops,
   #endif
   },
};
 
module_i2c_driver(light_cm3218_driver);
 
MODULE_AUTHOR("luowei <lw@rock-chips.com>");
MODULE_DESCRIPTION("cm3218 light driver");
MODULE_LICENSE("GPL");