aboutsummaryrefslogtreecommitdiff
path: root/drivers/mfd/si476x-prop.c
blob: 3d5c118888b26211650bbe8d43b458350a1ba278 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * drivers/mfd/si476x-prop.c -- Subroutines to access
 * properties of si476x chips
 *
 * Copyright (C) 2012 Innovative Converged Devices(ICD)
 * Copyright (C) 2013 Andrey Smirnov
 *
 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
 */
#include <linux/module.h>

#include <linux/mfd/si476x-core.h>

struct si476x_property_range {
	u16 low, high;
};

static bool si476x_core_element_is_in_array(u16 element,
					    const u16 array[],
					    size_t size)
{
	int i;

	for (i = 0; i < size; i++)
		if (element == array[i])
			return true;

	return false;
}

static bool si476x_core_element_is_in_range(u16 element,
					    const struct si476x_property_range range[],
					    size_t size)
{
	int i;

	for (i = 0; i < size; i++)
		if (element <= range[i].high && element >= range[i].low)
			return true;

	return false;
}

static bool si476x_core_is_valid_property_a10(struct si476x_core *core,
					      u16 property)
{
	static const u16 valid_properties[] = {
		0x0000,
		0x0500, 0x0501,
		0x0600,
		0x0709, 0x070C, 0x070D, 0x70E, 0x710,
		0x0718,
		0x1207, 0x1208,
		0x2007,
		0x2300,
	};

	static const struct si476x_property_range valid_ranges[] = {
		{ 0x0200, 0x0203 },
		{ 0x0300, 0x0303 },
		{ 0x0400, 0x0404 },
		{ 0x0700, 0x0707 },
		{ 0x1100, 0x1102 },
		{ 0x1200, 0x1204 },
		{ 0x1300, 0x1306 },
		{ 0x2000, 0x2005 },
		{ 0x2100, 0x2104 },
		{ 0x2106, 0x2106 },
		{ 0x2200, 0x220E },
		{ 0x3100, 0x3104 },
		{ 0x3207, 0x320F },
		{ 0x3300, 0x3304 },
		{ 0x3500, 0x3517 },
		{ 0x3600, 0x3617 },
		{ 0x3700, 0x3717 },
		{ 0x4000, 0x4003 },
	};

	return	si476x_core_element_is_in_range(property, valid_ranges,
						ARRAY_SIZE(valid_ranges)) ||
		si476x_core_element_is_in_array(property, valid_properties,
						ARRAY_SIZE(valid_properties));
}

static bool si476x_core_is_valid_property_a20(struct si476x_core *core,
					      u16 property)
{
	static const u16 valid_properties[] = {
		0x071B,
		0x1006,
		0x2210,
		0x3401,
	};

	static const struct si476x_property_range valid_ranges[] = {
		{ 0x2215, 0x2219 },
	};

	return	si476x_core_is_valid_property_a10(core, property) ||
		si476x_core_element_is_in_range(property, valid_ranges,
						ARRAY_SIZE(valid_ranges))  ||
		si476x_core_element_is_in_array(property, valid_properties,
						ARRAY_SIZE(valid_properties));
}

static bool si476x_core_is_valid_property_a30(struct si476x_core *core,
					      u16 property)
{
	static const u16 valid_properties[] = {
		0x071C, 0x071D,
		0x1007, 0x1008,
		0x220F, 0x2214,
		0x2301,
		0x3105, 0x3106,
		0x3402,
	};

	static const struct si476x_property_range valid_ranges[] = {
		{ 0x0405, 0x0411 },
		{ 0x2008, 0x200B },
		{ 0x2220, 0x2223 },
		{ 0x3100, 0x3106 },
	};

	return	si476x_core_is_valid_property_a20(core, property) ||
		si476x_core_element_is_in_range(property, valid_ranges,
						ARRAY_SIZE(valid_ranges)) ||
		si476x_core_element_is_in_array(property, valid_properties,
						ARRAY_SIZE(valid_properties));
}

typedef bool (*valid_property_pred_t) (struct si476x_core *, u16);

static bool si476x_core_is_valid_property(struct si476x_core *core,
					  u16 property)
{
	static const valid_property_pred_t is_valid_property[] = {
		[SI476X_REVISION_A10] = si476x_core_is_valid_property_a10,
		[SI476X_REVISION_A20] = si476x_core_is_valid_property_a20,
		[SI476X_REVISION_A30] = si476x_core_is_valid_property_a30,
	};

	BUG_ON(core->revision > SI476X_REVISION_A30 ||
	       core->revision == -1);
	return is_valid_property[core->revision](core, property);
}


static bool si476x_core_is_readonly_property(struct si476x_core *core,
					     u16 property)
{
	BUG_ON(core->revision > SI476X_REVISION_A30 ||
	       core->revision == -1);

	switch (core->revision) {
	case SI476X_REVISION_A10:
		return (property == 0x3200);
	case SI476X_REVISION_A20:
		return (property == 0x1006 ||
			property == 0x2210 ||
			property == 0x3200);
	case SI476X_REVISION_A30:
		return false;
	}

	return false;
}

static bool si476x_core_regmap_readable_register(struct device *dev,
						 unsigned int reg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct si476x_core *core = i2c_get_clientdata(client);

	return si476x_core_is_valid_property(core, (u16) reg);

}

static bool si476x_core_regmap_writable_register(struct device *dev,
						 unsigned int reg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct si476x_core *core = i2c_get_clientdata(client);

	return si476x_core_is_valid_property(core, (u16) reg) &&
		!si476x_core_is_readonly_property(core, (u16) reg);
}


static int si476x_core_regmap_write(void *context, unsigned int reg,
				    unsigned int val)
{
	return si476x_core_cmd_set_property(context, reg, val);
}

static int si476x_core_regmap_read(void *context, unsigned int reg,
				   unsigned *val)
{
	struct si476x_core *core = context;
	int err;

	err = si476x_core_cmd_get_property(core, reg);
	if (err < 0)
		return err;

	*val = err;

	return 0;
}


static const struct regmap_config si476x_regmap_config = {
	.reg_bits = 16,
	.val_bits = 16,

	.max_register = 0x4003,

	.writeable_reg = si476x_core_regmap_writable_register,
	.readable_reg = si476x_core_regmap_readable_register,

	.reg_read = si476x_core_regmap_read,
	.reg_write = si476x_core_regmap_write,

	.cache_type = REGCACHE_MAPLE,
};

struct regmap *devm_regmap_init_si476x(struct si476x_core *core)
{
	return devm_regmap_init(&core->client->dev, NULL,
				core, &si476x_regmap_config);
}
EXPORT_SYMBOL_GPL(devm_regmap_init_si476x);