summaryrefslogtreecommitdiff
path: root/drivers/media/platform/starfive/sc2235.c
blob: a13f56f928e7d9e19918702eeb18e9778c0194f0 (plain)
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
/*
 * Copyright (C) 2020 StarFive Technology Co., Ltd. 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.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/clk.h>
#include <linux/delay.h>


#define SC2235_CHIP_ID_H	(0x22)
#define SC2235_CHIP_ID_L	(0x35)
#define SC2235_REG_END		0xffff
#define SC2235_REG_DELAY	0xfffe

struct regval_list {
	uint16_t reg_num;
	unsigned char value;
};

/* sc2235 initial register */
static struct regval_list sc2235_init_regs_tbl_1080[] = {
	{0x0103, 0x01},
	{0x0100, 0x00},
	{0x3039, 0x80},
	{0x3621, 0x28},

	{0x3309, 0x60},
	{0x331f, 0x4d},
	{0x3321, 0x4f},
	{0x33b5, 0x10},

	{0x3303, 0x20},
	{0x331e, 0x0d},
	{0x3320, 0x0f},

	{0x3622, 0x02},
	{0x3633, 0x42},
	{0x3634, 0x42},

	{0x3306, 0x66},
	{0x330b, 0xd1},

	{0x3301, 0x0e},

	{0x320c, 0x08},
	{0x320d, 0x98},

	{0x3364, 0x05},		// [2] 1: write at sampling ending

	{0x363c, 0x28},		//bypass nvdd
	{0x363b, 0x0a},		//HVDD
	{0x3635, 0xa0},		//TXVDD

	{0x4500, 0x59},
	{0x3d08, 0x02},
	{0x3908, 0x11},

	{0x363c, 0x08},

	{0x3e03, 0x03},
	{0x3e01, 0x46},

	//0703
	{0x3381, 0x0a},
	{0x3348, 0x09},
	{0x3349, 0x50},
	{0x334a, 0x02},
	{0x334b, 0x60},

	{0x3380, 0x04},
	{0x3340, 0x06},
	{0x3341, 0x50},
	{0x3342, 0x02},
	{0x3343, 0x60},

	//0707

	{0x3632, 0x88},		//anti sm
	{0x3309, 0xa0},
	{0x331f, 0x8d},
	{0x3321, 0x8f},

	{0x335e, 0x01},		//ana dithering
	{0x335f, 0x03},
	{0x337c, 0x04},
	{0x337d, 0x06},
	{0x33a0, 0x05},
	{0x3301, 0x05},

	{0x337f, 0x03},		//new auto precharge  330e in 3372   [7:6] 11: close div_rst 00:open div_rst
	{0x3368, 0x02},
	{0x3369, 0x00},
	{0x336a, 0x00},
	{0x336b, 0x00},
	{0x3367, 0x08},
	{0x330e, 0x30},

	{0x3366, 0x7c},		// div_rst gap

	{0x3635, 0xc1},
	{0x363b, 0x09},
	{0x363c, 0x07},

	{0x391e, 0x00},

	{0x3637, 0x14},		//fullwell 7K

	{0x3306, 0x54},
	{0x330b, 0xd8},
	{0x366e, 0x08},		// ofs auto en [3]
	{0x366f, 0x2f},		// ofs+finegain  real ofs in { 0x3687[4:0]

	{0x3631, 0x84},
	{0x3630, 0x48},
	{0x3622, 0x06},

	//ramp by sc
	{0x3638, 0x1f},
	{0x3625, 0x02},
	{0x3636, 0x24},

	//0714
	{0x3348, 0x08},
	{0x3e03, 0x0b},

	//7.17 fpn
	{0x3342, 0x03},
	{0x3343, 0xa0},
	{0x334a, 0x03},
	{0x334b, 0xa0},

	//0718
	{0x3343, 0xb0},
	{0x334b, 0xb0},

	//0720
	//digital ctrl
	{0x3802, 0x01},
	{0x3235, 0x04},
	{0x3236, 0x63},		// vts-2

	//fpn
	{0x3343, 0xd0},
	{0x334b, 0xd0},
	{0x3348, 0x07},
	{0x3349, 0x80},

	//0724
	{0x391b, 0x4d},

	{0x3342, 0x04},
	{0x3343, 0x20},
	{0x334a, 0x04},
	{0x334b, 0x20},

	//0804
	{0x3222, 0x29},
	{0x3901, 0x02},

	//0808

	//digital ctrl
	{0x3f00, 0x07},		// bit[2] = 1
	{0x3f04, 0x08},
	{0x3f05, 0x74},		// hts - { 0x24

	//0809
	{0x330b, 0xc8},

	//0817
	{0x3306, 0x4a},
	{0x330b, 0xca},
	{0x3639, 0x09},

	//manual DPC
	{0x5780, 0xff},
	{0x5781, 0x04},
	{0x5785, 0x18},

	//0822
	{0x3039, 0x35},		//fps
	{0x303a, 0x2e},
	{0x3034, 0x05},
	{0x3035, 0x2a},

	{0x320c, 0x08},
	{0x320d, 0xca},
	{0x320e, 0x04},
	{0x320f, 0xb0},

	{0x3f04, 0x08},
	{0x3f05, 0xa6},		// hts - { 0x24

	{0x3235, 0x04},
	{0x3236, 0xae},		// vts-2

	//0825
	{0x3313, 0x05},
	{0x3678, 0x42},

	//for AE control per frame
	{0x3670, 0x00},
	{0x3633, 0x42},

	{0x3802, 0x00},

	//20180126
	{0x3677, 0x3f},
	{0x3306, 0x44},		//20180126[3c },4a]
	{0x330b, 0xca},		//20180126[c2 },d3]

	//20180202
	{0x3237, 0x08},
	{0x3238, 0x9a},		//hts-0x30

	//20180417
	{0x3640, 0x01},
	{0x3641, 0x02},

	{0x3301, 0x12},		//[8 },15]20180126
	{0x3631, 0x84},
	{0x366f, 0x2f},
	{0x3622, 0xc6},		//20180117
	{0x0100, 0x01},
	//{ 0x4501, 0xc8 },	//bar testing
	//{ 0x3902, 0x45 },
	{SC2235_REG_END, 0x00},
};

int sc2235_read(struct i2c_client *client, uint16_t reg, unsigned char *value)
{
	int ret;
	unsigned char buf[2] = { reg >> 8, reg & 0xff };
	struct i2c_msg msg[2] = {
		[0] = {
		       .addr = client->addr,
		       .flags = 0,
		       .len = 2,
		       .buf = buf,
		       },
		[1] = {
		       .addr = client->addr,
		       .flags = I2C_M_RD,
		       .len = 1,
		       .buf = value,
		       }
	};

	ret = i2c_transfer(client->adapter, msg, 2);
	if (ret > 0)
		ret = 0;

	return ret;
}

int sc2235_write(struct i2c_client *client, uint16_t reg, unsigned char value)
{
	int ret;
	uint8_t buf[3] = { (reg >> 8) & 0xff, reg & 0xff, value };
	struct i2c_msg msg = {
		.addr = client->addr,
		.flags = 0,
		.len = 3,
		.buf = buf,
	};

	ret = i2c_transfer(client->adapter, &msg, 1);
	if (ret > 0)
		ret = 0;

	return ret;
}

/*
static int sc2235_read_array(struct i2c_client *client,
			     struct regval_list *vals)
{
	int ret;
	unsigned char val;
	while (vals->reg_num != SC2235_REG_END) {
		if (vals->reg_num == SC2235_REG_DELAY) {
			msleep(vals->value);
		} else {
			ret = sc2235_read(client, vals->reg_num, &val);
			if (ret < 0)
				return ret;
		}
		vals++;
	}
	return 0;
}
*/

static int sc2235_write_array(struct i2c_client *client,
			      struct regval_list *vals)
{
	int ret;
	while (vals->reg_num != SC2235_REG_END) {
		if (vals->reg_num == SC2235_REG_DELAY) {
			msleep(vals->value);
		} else {
			ret = sc2235_write(client, vals->reg_num, vals->value);
			if (ret < 0)
				return ret;
		}
		vals++;
	}
	return 0;
}


/*
 *  sc2235 chip detect
 *
 *  @return   0: success, others: fail
 *
 */
static int sc2235_detect(struct i2c_client *client, unsigned int *ident)
{
	int ret;
	unsigned char v;

	ret = sc2235_read(client, 0x3107, &v);
	if (ret < 0)
		return ret;
	if (v != SC2235_CHIP_ID_H)
		return -ENODEV;
	*ident = v;

	ret = sc2235_read(client, 0x3108, &v);
	if (ret < 0)
		return ret;

	if (v != SC2235_CHIP_ID_L)
		return -ENODEV;

	*ident = (*ident << 8) | v;
	return 0;
}

static int sc2235_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	int ret;
	unsigned int chipid;
	dev_info(&client->dev, "sc2235_probe enter\n");

	ret = sc2235_detect(client, &chipid);
	if (ret < 0) {
		dev_err(&client->dev, "cannot detect sc2235 chip\n");
		return -ENODEV;
	}

	dev_info(&client->dev, "sc2235 id = 0x%x\n", chipid);

	ret = sc2235_write_array(client, sc2235_init_regs_tbl_1080);
	if (ret < 0) {
		dev_err(&client->dev, " sc2235 init failed\n");
		return -EINVAL;
	}

	return 0;
}

static int sc2235_remove(struct i2c_client *client)
{
	return 0;
}

static const struct i2c_device_id sc2235_id[] = {
	{"sc2235", 0},
	{}
};

static const struct of_device_id sc2235_sensor_dt_ids[] = {
	{.compatible = "sc2235",},
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, sc2235_sensor_dt_ids);

static struct i2c_driver sc2235_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = "sc2235",
		   .of_match_table = sc2235_sensor_dt_ids,
		   },
	.probe = sc2235_probe,
	.remove = sc2235_remove,
	.id_table = sc2235_id,
};

//module_i2c_driver(sc2235_driver);
static __init int init_sc2235(void)
{
    int err;

	err = i2c_add_driver(&sc2235_driver);
    if (err != 0)
		printk("i2c driver registration failed, error=%d\n", err);

	return err;
}

static __exit void exit_sc2235(void)
{
	i2c_del_driver(&sc2235_driver);
}

fs_initcall(init_sc2235);
module_exit(exit_sc2235);

MODULE_DESCRIPTION("A low-level driver for SmartSens sc2235 sensors");
MODULE_LICENSE("GPL");