Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/mfd/si476x-prop.c -- Subroutines to access
  4 * properties of si476x chips
  5 *
  6 * Copyright (C) 2012 Innovative Converged Devices(ICD)
  7 * Copyright (C) 2013 Andrey Smirnov
  8 *
  9 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
 
 
 
 
 
 
 
 
 
 10 */
 11#include <linux/module.h>
 12
 13#include <linux/mfd/si476x-core.h>
 14
 15struct si476x_property_range {
 16	u16 low, high;
 17};
 18
 19static bool si476x_core_element_is_in_array(u16 element,
 20					    const u16 array[],
 21					    size_t size)
 22{
 23	int i;
 24
 25	for (i = 0; i < size; i++)
 26		if (element == array[i])
 27			return true;
 28
 29	return false;
 30}
 31
 32static bool si476x_core_element_is_in_range(u16 element,
 33					    const struct si476x_property_range range[],
 34					    size_t size)
 35{
 36	int i;
 37
 38	for (i = 0; i < size; i++)
 39		if (element <= range[i].high && element >= range[i].low)
 40			return true;
 41
 42	return false;
 43}
 44
 45static bool si476x_core_is_valid_property_a10(struct si476x_core *core,
 46					      u16 property)
 47{
 48	static const u16 valid_properties[] = {
 49		0x0000,
 50		0x0500, 0x0501,
 51		0x0600,
 52		0x0709, 0x070C, 0x070D, 0x70E, 0x710,
 53		0x0718,
 54		0x1207, 0x1208,
 55		0x2007,
 56		0x2300,
 57	};
 58
 59	static const struct si476x_property_range valid_ranges[] = {
 60		{ 0x0200, 0x0203 },
 61		{ 0x0300, 0x0303 },
 62		{ 0x0400, 0x0404 },
 63		{ 0x0700, 0x0707 },
 64		{ 0x1100, 0x1102 },
 65		{ 0x1200, 0x1204 },
 66		{ 0x1300, 0x1306 },
 67		{ 0x2000, 0x2005 },
 68		{ 0x2100, 0x2104 },
 69		{ 0x2106, 0x2106 },
 70		{ 0x2200, 0x220E },
 71		{ 0x3100, 0x3104 },
 72		{ 0x3207, 0x320F },
 73		{ 0x3300, 0x3304 },
 74		{ 0x3500, 0x3517 },
 75		{ 0x3600, 0x3617 },
 76		{ 0x3700, 0x3717 },
 77		{ 0x4000, 0x4003 },
 78	};
 79
 80	return	si476x_core_element_is_in_range(property, valid_ranges,
 81						ARRAY_SIZE(valid_ranges)) ||
 82		si476x_core_element_is_in_array(property, valid_properties,
 83						ARRAY_SIZE(valid_properties));
 84}
 85
 86static bool si476x_core_is_valid_property_a20(struct si476x_core *core,
 87					      u16 property)
 88{
 89	static const u16 valid_properties[] = {
 90		0x071B,
 91		0x1006,
 92		0x2210,
 93		0x3401,
 94	};
 95
 96	static const struct si476x_property_range valid_ranges[] = {
 97		{ 0x2215, 0x2219 },
 98	};
 99
100	return	si476x_core_is_valid_property_a10(core, property) ||
101		si476x_core_element_is_in_range(property, valid_ranges,
102						ARRAY_SIZE(valid_ranges))  ||
103		si476x_core_element_is_in_array(property, valid_properties,
104						ARRAY_SIZE(valid_properties));
105}
106
107static bool si476x_core_is_valid_property_a30(struct si476x_core *core,
108					      u16 property)
109{
110	static const u16 valid_properties[] = {
111		0x071C, 0x071D,
112		0x1007, 0x1008,
113		0x220F, 0x2214,
114		0x2301,
115		0x3105, 0x3106,
116		0x3402,
117	};
118
119	static const struct si476x_property_range valid_ranges[] = {
120		{ 0x0405, 0x0411 },
121		{ 0x2008, 0x200B },
122		{ 0x2220, 0x2223 },
123		{ 0x3100, 0x3106 },
124	};
125
126	return	si476x_core_is_valid_property_a20(core, property) ||
127		si476x_core_element_is_in_range(property, valid_ranges,
128						ARRAY_SIZE(valid_ranges)) ||
129		si476x_core_element_is_in_array(property, valid_properties,
130						ARRAY_SIZE(valid_properties));
131}
132
133typedef bool (*valid_property_pred_t) (struct si476x_core *, u16);
134
135static bool si476x_core_is_valid_property(struct si476x_core *core,
136					  u16 property)
137{
138	static const valid_property_pred_t is_valid_property[] = {
139		[SI476X_REVISION_A10] = si476x_core_is_valid_property_a10,
140		[SI476X_REVISION_A20] = si476x_core_is_valid_property_a20,
141		[SI476X_REVISION_A30] = si476x_core_is_valid_property_a30,
142	};
143
144	BUG_ON(core->revision > SI476X_REVISION_A30 ||
145	       core->revision == -1);
146	return is_valid_property[core->revision](core, property);
147}
148
149
150static bool si476x_core_is_readonly_property(struct si476x_core *core,
151					     u16 property)
152{
153	BUG_ON(core->revision > SI476X_REVISION_A30 ||
154	       core->revision == -1);
155
156	switch (core->revision) {
157	case SI476X_REVISION_A10:
158		return (property == 0x3200);
159	case SI476X_REVISION_A20:
160		return (property == 0x1006 ||
161			property == 0x2210 ||
162			property == 0x3200);
163	case SI476X_REVISION_A30:
164		return false;
165	}
166
167	return false;
168}
169
170static bool si476x_core_regmap_readable_register(struct device *dev,
171						 unsigned int reg)
172{
173	struct i2c_client *client = to_i2c_client(dev);
174	struct si476x_core *core = i2c_get_clientdata(client);
175
176	return si476x_core_is_valid_property(core, (u16) reg);
177
178}
179
180static bool si476x_core_regmap_writable_register(struct device *dev,
181						 unsigned int reg)
182{
183	struct i2c_client *client = to_i2c_client(dev);
184	struct si476x_core *core = i2c_get_clientdata(client);
185
186	return si476x_core_is_valid_property(core, (u16) reg) &&
187		!si476x_core_is_readonly_property(core, (u16) reg);
188}
189
190
191static int si476x_core_regmap_write(void *context, unsigned int reg,
192				    unsigned int val)
193{
194	return si476x_core_cmd_set_property(context, reg, val);
195}
196
197static int si476x_core_regmap_read(void *context, unsigned int reg,
198				   unsigned *val)
199{
200	struct si476x_core *core = context;
201	int err;
202
203	err = si476x_core_cmd_get_property(core, reg);
204	if (err < 0)
205		return err;
206
207	*val = err;
208
209	return 0;
210}
211
212
213static const struct regmap_config si476x_regmap_config = {
214	.reg_bits = 16,
215	.val_bits = 16,
216
217	.max_register = 0x4003,
218
219	.writeable_reg = si476x_core_regmap_writable_register,
220	.readable_reg = si476x_core_regmap_readable_register,
221
222	.reg_read = si476x_core_regmap_read,
223	.reg_write = si476x_core_regmap_write,
224
225	.cache_type = REGCACHE_RBTREE,
226};
227
228struct regmap *devm_regmap_init_si476x(struct si476x_core *core)
229{
230	return devm_regmap_init(&core->client->dev, NULL,
231				core, &si476x_regmap_config);
232}
233EXPORT_SYMBOL_GPL(devm_regmap_init_si476x);
v4.10.11
 
  1/*
  2 * drivers/mfd/si476x-prop.c -- Subroutines to access
  3 * properties of si476x chips
  4 *
  5 * Copyright (C) 2012 Innovative Converged Devices(ICD)
  6 * Copyright (C) 2013 Andrey Smirnov
  7 *
  8 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; version 2 of the License.
 13 *
 14 * This program is distributed in the hope that it will be useful, but
 15 * WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * General Public License for more details.
 18 */
 19#include <linux/module.h>
 20
 21#include <linux/mfd/si476x-core.h>
 22
 23struct si476x_property_range {
 24	u16 low, high;
 25};
 26
 27static bool si476x_core_element_is_in_array(u16 element,
 28					    const u16 array[],
 29					    size_t size)
 30{
 31	int i;
 32
 33	for (i = 0; i < size; i++)
 34		if (element == array[i])
 35			return true;
 36
 37	return false;
 38}
 39
 40static bool si476x_core_element_is_in_range(u16 element,
 41					    const struct si476x_property_range range[],
 42					    size_t size)
 43{
 44	int i;
 45
 46	for (i = 0; i < size; i++)
 47		if (element <= range[i].high && element >= range[i].low)
 48			return true;
 49
 50	return false;
 51}
 52
 53static bool si476x_core_is_valid_property_a10(struct si476x_core *core,
 54					      u16 property)
 55{
 56	static const u16 valid_properties[] = {
 57		0x0000,
 58		0x0500, 0x0501,
 59		0x0600,
 60		0x0709, 0x070C, 0x070D, 0x70E, 0x710,
 61		0x0718,
 62		0x1207, 0x1208,
 63		0x2007,
 64		0x2300,
 65	};
 66
 67	static const struct si476x_property_range valid_ranges[] = {
 68		{ 0x0200, 0x0203 },
 69		{ 0x0300, 0x0303 },
 70		{ 0x0400, 0x0404 },
 71		{ 0x0700, 0x0707 },
 72		{ 0x1100, 0x1102 },
 73		{ 0x1200, 0x1204 },
 74		{ 0x1300, 0x1306 },
 75		{ 0x2000, 0x2005 },
 76		{ 0x2100, 0x2104 },
 77		{ 0x2106, 0x2106 },
 78		{ 0x2200, 0x220E },
 79		{ 0x3100, 0x3104 },
 80		{ 0x3207, 0x320F },
 81		{ 0x3300, 0x3304 },
 82		{ 0x3500, 0x3517 },
 83		{ 0x3600, 0x3617 },
 84		{ 0x3700, 0x3717 },
 85		{ 0x4000, 0x4003 },
 86	};
 87
 88	return	si476x_core_element_is_in_range(property, valid_ranges,
 89						ARRAY_SIZE(valid_ranges)) ||
 90		si476x_core_element_is_in_array(property, valid_properties,
 91						ARRAY_SIZE(valid_properties));
 92}
 93
 94static bool si476x_core_is_valid_property_a20(struct si476x_core *core,
 95					      u16 property)
 96{
 97	static const u16 valid_properties[] = {
 98		0x071B,
 99		0x1006,
100		0x2210,
101		0x3401,
102	};
103
104	static const struct si476x_property_range valid_ranges[] = {
105		{ 0x2215, 0x2219 },
106	};
107
108	return	si476x_core_is_valid_property_a10(core, property) ||
109		si476x_core_element_is_in_range(property, valid_ranges,
110						ARRAY_SIZE(valid_ranges))  ||
111		si476x_core_element_is_in_array(property, valid_properties,
112						ARRAY_SIZE(valid_properties));
113}
114
115static bool si476x_core_is_valid_property_a30(struct si476x_core *core,
116					      u16 property)
117{
118	static const u16 valid_properties[] = {
119		0x071C, 0x071D,
120		0x1007, 0x1008,
121		0x220F, 0x2214,
122		0x2301,
123		0x3105, 0x3106,
124		0x3402,
125	};
126
127	static const struct si476x_property_range valid_ranges[] = {
128		{ 0x0405, 0x0411 },
129		{ 0x2008, 0x200B },
130		{ 0x2220, 0x2223 },
131		{ 0x3100, 0x3106 },
132	};
133
134	return	si476x_core_is_valid_property_a20(core, property) ||
135		si476x_core_element_is_in_range(property, valid_ranges,
136						ARRAY_SIZE(valid_ranges)) ||
137		si476x_core_element_is_in_array(property, valid_properties,
138						ARRAY_SIZE(valid_properties));
139}
140
141typedef bool (*valid_property_pred_t) (struct si476x_core *, u16);
142
143static bool si476x_core_is_valid_property(struct si476x_core *core,
144					  u16 property)
145{
146	static const valid_property_pred_t is_valid_property[] = {
147		[SI476X_REVISION_A10] = si476x_core_is_valid_property_a10,
148		[SI476X_REVISION_A20] = si476x_core_is_valid_property_a20,
149		[SI476X_REVISION_A30] = si476x_core_is_valid_property_a30,
150	};
151
152	BUG_ON(core->revision > SI476X_REVISION_A30 ||
153	       core->revision == -1);
154	return is_valid_property[core->revision](core, property);
155}
156
157
158static bool si476x_core_is_readonly_property(struct si476x_core *core,
159					     u16 property)
160{
161	BUG_ON(core->revision > SI476X_REVISION_A30 ||
162	       core->revision == -1);
163
164	switch (core->revision) {
165	case SI476X_REVISION_A10:
166		return (property == 0x3200);
167	case SI476X_REVISION_A20:
168		return (property == 0x1006 ||
169			property == 0x2210 ||
170			property == 0x3200);
171	case SI476X_REVISION_A30:
172		return false;
173	}
174
175	return false;
176}
177
178static bool si476x_core_regmap_readable_register(struct device *dev,
179						 unsigned int reg)
180{
181	struct i2c_client *client = to_i2c_client(dev);
182	struct si476x_core *core = i2c_get_clientdata(client);
183
184	return si476x_core_is_valid_property(core, (u16) reg);
185
186}
187
188static bool si476x_core_regmap_writable_register(struct device *dev,
189						 unsigned int reg)
190{
191	struct i2c_client *client = to_i2c_client(dev);
192	struct si476x_core *core = i2c_get_clientdata(client);
193
194	return si476x_core_is_valid_property(core, (u16) reg) &&
195		!si476x_core_is_readonly_property(core, (u16) reg);
196}
197
198
199static int si476x_core_regmap_write(void *context, unsigned int reg,
200				    unsigned int val)
201{
202	return si476x_core_cmd_set_property(context, reg, val);
203}
204
205static int si476x_core_regmap_read(void *context, unsigned int reg,
206				   unsigned *val)
207{
208	struct si476x_core *core = context;
209	int err;
210
211	err = si476x_core_cmd_get_property(core, reg);
212	if (err < 0)
213		return err;
214
215	*val = err;
216
217	return 0;
218}
219
220
221static const struct regmap_config si476x_regmap_config = {
222	.reg_bits = 16,
223	.val_bits = 16,
224
225	.max_register = 0x4003,
226
227	.writeable_reg = si476x_core_regmap_writable_register,
228	.readable_reg = si476x_core_regmap_readable_register,
229
230	.reg_read = si476x_core_regmap_read,
231	.reg_write = si476x_core_regmap_write,
232
233	.cache_type = REGCACHE_RBTREE,
234};
235
236struct regmap *devm_regmap_init_si476x(struct si476x_core *core)
237{
238	return devm_regmap_init(&core->client->dev, NULL,
239				core, &si476x_regmap_config);
240}
241EXPORT_SYMBOL_GPL(devm_regmap_init_si476x);