summaryrefslogtreecommitdiff
path: root/drivers/media/radio/stfm1000/stfm1000-i2c.c
blob: 5ddeb3a5fd3a71d85e2f3d7ac7225c5c9318e948 (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
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
/*
 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/io.h>
#include <linux/videodev2.h>
#include <media/v4l2-common.h>
#include <linux/i2c.h>
#include <linux/delay.h>

#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */

#include "stfm1000.h"

#define stfm1000_i2c_debug(p, fmt, arg...) \
	do { \
		if ((p)->dbgflg & STFM1000_DBGFLG_I2C) \
			printk(KERN_INFO "stfm1000: " fmt, ##arg); \
	} while (0)

static const char *reg_names[STFM1000_NUM_REGS] = {
#undef REGNAME
#define REGNAME(x) \
	[STFM1000_ ## x / 4] = #x ""

	REGNAME(TUNE1),
	REGNAME(SDNOMINAL),
	REGNAME(PILOTTRACKING),
	REGNAME(INITIALIZATION1),
	REGNAME(INITIALIZATION2),
	REGNAME(INITIALIZATION3),
	REGNAME(INITIALIZATION4),
	REGNAME(INITIALIZATION5),
	REGNAME(INITIALIZATION6),
	REGNAME(REF),
	REGNAME(LNA),
	REGNAME(MIXFILT),
	REGNAME(CLK1),
	REGNAME(CLK2),
	REGNAME(ADC),
	REGNAME(AGC_CONTROL1),
	REGNAME(AGC_CONTROL2),
	REGNAME(DATAPATH),
	REGNAME(RMS),
	REGNAME(AGC_STAT),
	REGNAME(SIGNALQUALITY),
	REGNAME(DCEST),
	REGNAME(RSSI_TONE),
	REGNAME(PILOTCORRECTION),
	REGNAME(ATTENTION),
	REGNAME(CLK3),
	REGNAME(CHIPID),
#undef REGNAME
};

static const int stfm1000_rw_regs[] = {
	STFM1000_TUNE1,
	STFM1000_SDNOMINAL,
	STFM1000_PILOTTRACKING,
	STFM1000_INITIALIZATION1,
	STFM1000_INITIALIZATION2,
	STFM1000_INITIALIZATION3,
	STFM1000_INITIALIZATION4,
	STFM1000_INITIALIZATION5,
	STFM1000_INITIALIZATION6,
	STFM1000_REF,
	STFM1000_LNA,
	STFM1000_MIXFILT,
	STFM1000_CLK1,
	STFM1000_CLK2,
	STFM1000_ADC,
	STFM1000_AGC_CONTROL1,
	STFM1000_AGC_CONTROL2,
	STFM1000_DATAPATH,
	STFM1000_ATTENTION,	/* it's both WR/RD */
};

static const int stfm1000_ra_regs[] = {
	STFM1000_RMS,
	STFM1000_AGC_STAT,
	STFM1000_SIGNALQUALITY,
	STFM1000_DCEST,
	STFM1000_RSSI_TONE,
	STFM1000_PILOTCORRECTION,
	STFM1000_ATTENTION,	/* it's both WR/RD - always read */
	STFM1000_CLK3,
	STFM1000_CHIPID
};

static int verify_writes;

void stfm1000_setup_reg_set(struct stfm1000 *stfm1000)
{
	int i, reg;

	/* set up register sets (read/write) */
	for (i = 0; i < ARRAY_SIZE(stfm1000_rw_regs); i++) {
		reg = stfm1000_rw_regs[i] / 4;
		stfm1000->reg_rw_set[reg / 32] |= 1U << (reg & 31);
		/* printk(KERN_INFO "STFM1000: rw <= %d\n", reg); */
	}

	/* for (i = 0; i < ARRAY_SIZE(stfm1000->reg_rw_set); i++)
		printk("RW[%d] = 0x%08x\n", i, stfm1000->reg_rw_set[i]); */

	/* set up register sets (read only) */
	for (i = 0; i < ARRAY_SIZE(stfm1000_ra_regs); i++) {
		reg = stfm1000_ra_regs[i] / 4;
		stfm1000->reg_ra_set[reg / 32] |= 1U << (reg & 31);
		/* printk(KERN_INFO "STFM1000: rw <= %d\n", reg); */
	}
	/* for (i = 0; i < ARRAY_SIZE(stfm1000->reg_ra_set); i++)
		printk("RO[%d] = 0x%08x\n", i, stfm1000->reg_ra_set[i]); */

	/* clear dirty */
	memset(stfm1000->reg_dirty_set, 0, sizeof(stfm1000->reg_dirty_set));
}

static int stfm1000_reg_is_rw(struct stfm1000 *stfm1000, int reg)
{
	reg >>= 2;
	return !!(stfm1000->reg_rw_set[reg / 32] & (1 << (reg & 31)));
}

static int stfm1000_reg_is_ra(struct stfm1000 *stfm1000, int reg)
{
	reg >>= 2;
	return !!(stfm1000->reg_ra_set[reg / 32] & (1 << (reg & 31)));
}

static int stfm1000_reg_is_dirty(struct stfm1000 *stfm1000, int reg)
{
	reg >>= 2;
	return !!(stfm1000->reg_dirty_set[reg / 32] & (1 << (reg & 31)));
}

static void stfm1000_reg_set_dirty(struct stfm1000 *stfm1000, int reg)
{
	reg >>= 2;
	stfm1000->reg_dirty_set[reg / 32] |= 1 << (reg & 31);
}

static inline int stfm1000_reg_is_writeable(struct stfm1000 *stfm1000, int reg)
{
	return stfm1000_reg_is_rw(stfm1000, reg);
}

static inline int stfm1000_reg_is_readable(struct stfm1000 *stfm1000, int reg)
{
	return stfm1000_reg_is_rw(stfm1000, reg) ||
	       stfm1000_reg_is_ra(stfm1000, reg);
}

/********************************************************/

static int write_reg_internal(struct stfm1000 *stfm1000, int reg, u32 value)
{
	u8 values[5];
	int ret;

	stfm1000_i2c_debug(stfm1000, "%s(%s - 0x%02x, 0x%08x)\n", __func__,
		reg_names[reg / 4], reg, value);

	values[0] = (u8)reg;
	values[1] = (u8)value;
	values[2] = (u8)(value >> 8);
	values[3] = (u8)(value >> 16);
	values[4] = (u8)(value >> 24);
	ret = i2c_master_send(stfm1000->client, values, 5);
	if (ret < 0)
		return ret;
	return 0;
}

static int read_reg_internal(struct stfm1000 *stfm1000, int reg, u32 *value)
{
	u8 regb = reg;
	u8 values[4];
	int ret;

	ret = i2c_master_send(stfm1000->client, &regb, 1);
	if (ret < 0)
		goto out;
	ret = i2c_master_recv(stfm1000->client, values, 4);
	if (ret < 0)
		goto out;
	*value = (u32)values[0] | ((u32)values[1] << 8) |
		 ((u32)values[2] << 16) | ((u32)values[3] << 24);
	ret = 0;

	stfm1000_i2c_debug(stfm1000, "%s(%s - 0x%02x, 0x%08x)\n", __func__,
		reg_names[reg / 4], reg, *value);
out:
	return ret;
}

int stfm1000_raw_write(struct stfm1000 *stfm1000, int reg, u32 value)
{
	int ret;

	mutex_lock(&stfm1000->xfer_lock);
	ret = write_reg_internal(stfm1000, reg, value);
	mutex_unlock(&stfm1000->xfer_lock);

	if (ret < 0)
		dev_err(&stfm1000->client->dev, "%s: failed", __func__);

	return ret;
}

int stfm1000_raw_read(struct stfm1000 *stfm1000, int reg, u32 *value)
{
	int ret;

	mutex_lock(&stfm1000->xfer_lock);
	ret = read_reg_internal(stfm1000, reg, value);
	mutex_unlock(&stfm1000->xfer_lock);

	if (ret < 0)
		dev_err(&stfm1000->client->dev, "%s: failed", __func__);

	return ret;
}

static inline void stfm1000_set_shadow_reg(struct stfm1000 *stfm1000,
	int reg, u32 val)
{
	stfm1000->shadow_regs[reg / 4] = val;
}

static inline u32 stfm1000_get_shadow_reg(struct stfm1000 *stfm1000, int reg)
{
	return stfm1000->shadow_regs[reg / 4];
}

int stfm1000_write(struct stfm1000 *stfm1000, int reg, u32 value)
{
	int ret;

	if (!stfm1000_reg_is_writeable(stfm1000, reg)) {
		ret = -EINVAL;
		goto out;
	}

	mutex_lock(&stfm1000->xfer_lock);

	/* same value as last one written? */
	if (stfm1000_reg_is_dirty(stfm1000, reg) &&
		stfm1000_get_shadow_reg(stfm1000, reg) == value) {
		ret = 0;

		stfm1000_i2c_debug(stfm1000, "%s - HIT "
			"(%s - 0x%02x, 0x%08x)\n", __func__,
			reg_names[reg / 4], reg, value);

		goto out_unlock;
	}

	/* actually write the register */
	ret = write_reg_internal(stfm1000, reg, value);
	if (ret < 0)
		goto out_unlock;

	/* update shadow register & mark it as dirty */
	/* only if register is not read always */
	if (!stfm1000_reg_is_ra(stfm1000, reg)) {
		stfm1000_set_shadow_reg(stfm1000, reg, value);
		stfm1000_reg_set_dirty(stfm1000, reg);
	}

out_unlock:
	mutex_unlock(&stfm1000->xfer_lock);

out:
	if (ret < 0)
		dev_err(&stfm1000->client->dev, "%s: failed", __func__);

	if (verify_writes) {
		u32 value2 = ~0;

		stfm1000_raw_read(stfm1000, reg, &value2);

		stfm1000_i2c_debug(stfm1000, "%s - VER "
			"(%s - 0x%02x, W=0x%08x V=0x%08x) %s\n", __func__,
			reg_names[reg / 4], reg, value, value2,
			value == value2 ? "OK" : "** differs **");
	}

	return ret;
}

int stfm1000_read(struct stfm1000 *stfm1000, int reg, u32 *value)
{
	int ret = 0;

	if (!stfm1000_reg_is_readable(stfm1000, reg)) {
		ret = -EINVAL;
		printk(KERN_INFO "%s: !readable(%d)\n", __func__, reg);
		goto out;
	}

	mutex_lock(&stfm1000->xfer_lock);

	/* if the register can be written & is dirty, use the shadow */
	if (stfm1000_reg_is_writeable(stfm1000, reg) &&
		stfm1000_reg_is_dirty(stfm1000, reg)) {

		*value = stfm1000_get_shadow_reg(stfm1000, reg);
		ret = 0;

		stfm1000_i2c_debug(stfm1000, "%s - HIT "
			"(%s - 0x%02x, 0x%08x)\n", __func__,
			reg_names[reg / 4], reg, *value);

		goto out_unlock;
	}

	/* register must be read */
	ret = read_reg_internal(stfm1000, reg, value);
	if (ret < 0)
		goto out;

	/* if the register is writeable, update shadow */
	if (stfm1000_reg_is_writeable(stfm1000, reg)) {
		stfm1000_set_shadow_reg(stfm1000, reg, *value);
		stfm1000_reg_set_dirty(stfm1000, reg);
	}

out_unlock:
	mutex_unlock(&stfm1000->xfer_lock);

out:
	if (ret < 0)
		dev_err(&stfm1000->client->dev, "%s: failed", __func__);

	return ret;
}

int stfm1000_write_masked(struct stfm1000 *stfm1000, int reg, u32 value,
	u32 mask)
{
	int ret = 0;
	u32 old_value;

	if (!stfm1000_reg_is_writeable(stfm1000, reg)) {
		ret = -EINVAL;
		printk(KERN_ERR "%s: !writeable(%d)\n", __func__, reg);
		goto out;
	}

	mutex_lock(&stfm1000->xfer_lock);

	/* if the register wasn't written before, read it */
	if (!stfm1000_reg_is_dirty(stfm1000, reg)) {
		ret = read_reg_internal(stfm1000, reg, &old_value);
		if (ret != 0)
			goto out_unlock;
	} else	/* register was written, use the last value */
		old_value = stfm1000_get_shadow_reg(stfm1000, reg);

	/* perform masking */
	value = (old_value & ~mask) | (value & mask);

	/* if we write the same value, don't bother */
	if (stfm1000_reg_is_dirty(stfm1000, reg) && value == old_value) {
		ret = 0;

		stfm1000_i2c_debug(stfm1000, "%s - HIT "
			"(%s - 0x%02x, 0x%08x)\n", __func__,
			reg_names[reg / 4], reg, value);

		goto out_unlock;
	}

	/* actually write the register to the chip */
	ret = write_reg_internal(stfm1000, reg, value);
	if (ret < 0)
		goto out_unlock;

	/* if no error, update the shadow register and mark it as dirty */
	stfm1000_set_shadow_reg(stfm1000, reg, value);
	stfm1000_reg_set_dirty(stfm1000, reg);

out_unlock:
	mutex_unlock(&stfm1000->xfer_lock);

out:
	if (ret < 0)
		dev_err(&stfm1000->client->dev, "%s: failed", __func__);

	if (verify_writes) {
		u32 value2 = ~0;

		stfm1000_raw_read(stfm1000, reg, &value2);

		stfm1000_i2c_debug(stfm1000, "%s - VER "
			"(%s - 0x%02x, W=0x%08x V=0x%08x) %s\n", __func__,
			reg_names[reg / 4], reg, value, value2,
			value == value2 ? "OK" : "** differs **");
	}

	return ret;
}

int stfm1000_set_bits(struct stfm1000 *stfm1000, int reg, u32 value)
{
	return stfm1000_write_masked(stfm1000, reg, value, value);
}

int stfm1000_clear_bits(struct stfm1000 *stfm1000, int reg, u32 value)
{
	return stfm1000_write_masked(stfm1000, reg, ~value, value);
}

int stfm1000_write_regs(struct stfm1000 *stfm1000,
	const struct stfm1000_reg *reg)
{
	int ret;

	for (; reg && reg->regno != STFM1000_REG_END; reg++) {

		if (reg->regno == STFM1000_REG_DELAY) {
			msleep(reg->value);
			continue;
		}

		if (reg->regno & STFM1000_REG_SET_BITS_MASK)
			ret = stfm1000_set_bits(stfm1000, reg->regno & 0xff,
				reg->value);
		else if (reg->regno & STFM1000_REG_CLEAR_BITS_MASK)
			ret = stfm1000_clear_bits(stfm1000, reg->regno & 0xff,
				reg->value);
		else
			ret = stfm1000_write(stfm1000, reg->regno, reg->value);

		if (ret != 0) {
			printk(KERN_ERR "%s: failed to write reg 0x%x "
				"with 0x%08x\n",
				__func__, reg->regno, reg->value);
			return ret;
		}
	}
	return 0;
}