2 * drivers/mfd/si476x-prop.c -- Subroutines to access
3 * properties of si476x chips
5 * Copyright (C) 2012 Innovative Converged Devices(ICD)
6 * Copyright (C) 2013 Andrey Smirnov
8 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
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.
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.
19 #include <linux/module.h>
21 #include <linux/mfd/si476x-core.h>
23 struct si476x_property_range
{
27 static bool si476x_core_element_is_in_array(u16 element
,
33 for (i
= 0; i
< size
; i
++)
34 if (element
== array
[i
])
40 static bool si476x_core_element_is_in_range(u16 element
,
41 const struct si476x_property_range range
[],
46 for (i
= 0; i
< size
; i
++)
47 if (element
<= range
[i
].high
&& element
>= range
[i
].low
)
53 static bool si476x_core_is_valid_property_a10(struct si476x_core
*core
,
56 static const u16 valid_properties
[] = {
60 0x0709, 0x070C, 0x070D, 0x70E, 0x710,
67 static const struct si476x_property_range valid_ranges
[] = {
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
));
94 static bool si476x_core_is_valid_property_a20(struct si476x_core
*core
,
97 static const u16 valid_properties
[] = {
104 static const struct si476x_property_range valid_ranges
[] = {
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
));
115 static bool si476x_core_is_valid_property_a30(struct si476x_core
*core
,
118 static const u16 valid_properties
[] = {
127 static const struct si476x_property_range valid_ranges
[] = {
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
));
141 typedef bool (*valid_property_pred_t
) (struct si476x_core
*, u16
);
143 static bool si476x_core_is_valid_property(struct si476x_core
*core
,
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
,
152 BUG_ON(core
->revision
> SI476X_REVISION_A30
||
153 core
->revision
== -1);
154 return is_valid_property
[core
->revision
](core
, property
);
158 static bool si476x_core_is_readonly_property(struct si476x_core
*core
,
161 BUG_ON(core
->revision
> SI476X_REVISION_A30
||
162 core
->revision
== -1);
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 ||
171 case SI476X_REVISION_A30
:
178 static bool si476x_core_regmap_readable_register(struct device
*dev
,
181 struct i2c_client
*client
= to_i2c_client(dev
);
182 struct si476x_core
*core
= i2c_get_clientdata(client
);
184 return si476x_core_is_valid_property(core
, (u16
) reg
);
188 static bool si476x_core_regmap_writable_register(struct device
*dev
,
191 struct i2c_client
*client
= to_i2c_client(dev
);
192 struct si476x_core
*core
= i2c_get_clientdata(client
);
194 return si476x_core_is_valid_property(core
, (u16
) reg
) &&
195 !si476x_core_is_readonly_property(core
, (u16
) reg
);
199 static int si476x_core_regmap_write(void *context
, unsigned int reg
,
202 return si476x_core_cmd_set_property(context
, reg
, val
);
205 static int si476x_core_regmap_read(void *context
, unsigned int reg
,
208 struct si476x_core
*core
= context
;
211 err
= si476x_core_cmd_get_property(core
, reg
);
221 static const struct regmap_config si476x_regmap_config
= {
225 .max_register
= 0x4003,
227 .writeable_reg
= si476x_core_regmap_writable_register
,
228 .readable_reg
= si476x_core_regmap_readable_register
,
230 .reg_read
= si476x_core_regmap_read
,
231 .reg_write
= si476x_core_regmap_write
,
233 .cache_type
= REGCACHE_RBTREE
,
236 struct regmap
*devm_regmap_init_si476x(struct si476x_core
*core
)
238 return devm_regmap_init(&core
->client
->dev
, NULL
,
239 core
, &si476x_regmap_config
);
241 EXPORT_SYMBOL_GPL(devm_regmap_init_si476x
);