1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Register map access API
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright 2011 Wolfson Microelectronics plc
6*4882a593Smuzhiyun //
7*4882a593Smuzhiyun // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/export.h>
12*4882a593Smuzhiyun #include <linux/mutex.h>
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/property.h>
15*4882a593Smuzhiyun #include <linux/rbtree.h>
16*4882a593Smuzhiyun #include <linux/sched.h>
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/log2.h>
19*4882a593Smuzhiyun #include <linux/hwspinlock.h>
20*4882a593Smuzhiyun #include <asm/unaligned.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
23*4882a593Smuzhiyun #include "trace.h"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include "internal.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Sometimes for failures during very early init the trace
29*4882a593Smuzhiyun * infrastructure isn't available early enough to be used. For this
30*4882a593Smuzhiyun * sort of problem defining LOG_DEVICE will add printks for basic
31*4882a593Smuzhiyun * register I/O on a specific device.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #undef LOG_DEVICE
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #ifdef LOG_DEVICE
regmap_should_log(struct regmap * map)36*4882a593Smuzhiyun static inline bool regmap_should_log(struct regmap *map)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun return (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0);
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun #else
regmap_should_log(struct regmap * map)41*4882a593Smuzhiyun static inline bool regmap_should_log(struct regmap *map) { return false; }
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static int _regmap_update_bits(struct regmap *map, unsigned int reg,
46*4882a593Smuzhiyun unsigned int mask, unsigned int val,
47*4882a593Smuzhiyun bool *change, bool force_write);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun static int _regmap_bus_reg_read(void *context, unsigned int reg,
50*4882a593Smuzhiyun unsigned int *val);
51*4882a593Smuzhiyun static int _regmap_bus_read(void *context, unsigned int reg,
52*4882a593Smuzhiyun unsigned int *val);
53*4882a593Smuzhiyun static int _regmap_bus_formatted_write(void *context, unsigned int reg,
54*4882a593Smuzhiyun unsigned int val);
55*4882a593Smuzhiyun static int _regmap_bus_reg_write(void *context, unsigned int reg,
56*4882a593Smuzhiyun unsigned int val);
57*4882a593Smuzhiyun static int _regmap_bus_raw_write(void *context, unsigned int reg,
58*4882a593Smuzhiyun unsigned int val);
59*4882a593Smuzhiyun
regmap_reg_in_ranges(unsigned int reg,const struct regmap_range * ranges,unsigned int nranges)60*4882a593Smuzhiyun bool regmap_reg_in_ranges(unsigned int reg,
61*4882a593Smuzhiyun const struct regmap_range *ranges,
62*4882a593Smuzhiyun unsigned int nranges)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun const struct regmap_range *r;
65*4882a593Smuzhiyun int i;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun for (i = 0, r = ranges; i < nranges; i++, r++)
68*4882a593Smuzhiyun if (regmap_reg_in_range(reg, r))
69*4882a593Smuzhiyun return true;
70*4882a593Smuzhiyun return false;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
73*4882a593Smuzhiyun
regmap_check_range_table(struct regmap * map,unsigned int reg,const struct regmap_access_table * table)74*4882a593Smuzhiyun bool regmap_check_range_table(struct regmap *map, unsigned int reg,
75*4882a593Smuzhiyun const struct regmap_access_table *table)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun /* Check "no ranges" first */
78*4882a593Smuzhiyun if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
79*4882a593Smuzhiyun return false;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* In case zero "yes ranges" are supplied, any reg is OK */
82*4882a593Smuzhiyun if (!table->n_yes_ranges)
83*4882a593Smuzhiyun return true;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun return regmap_reg_in_ranges(reg, table->yes_ranges,
86*4882a593Smuzhiyun table->n_yes_ranges);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_check_range_table);
89*4882a593Smuzhiyun
regmap_writeable(struct regmap * map,unsigned int reg)90*4882a593Smuzhiyun bool regmap_writeable(struct regmap *map, unsigned int reg)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun if (map->max_register && reg > map->max_register)
93*4882a593Smuzhiyun return false;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (map->writeable_reg)
96*4882a593Smuzhiyun return map->writeable_reg(map->dev, reg);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (map->wr_table)
99*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->wr_table);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return true;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
regmap_cached(struct regmap * map,unsigned int reg)104*4882a593Smuzhiyun bool regmap_cached(struct regmap *map, unsigned int reg)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun int ret;
107*4882a593Smuzhiyun unsigned int val;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (map->cache_type == REGCACHE_NONE)
110*4882a593Smuzhiyun return false;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (!map->cache_ops)
113*4882a593Smuzhiyun return false;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun if (map->max_register && reg > map->max_register)
116*4882a593Smuzhiyun return false;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun map->lock(map->lock_arg);
119*4882a593Smuzhiyun ret = regcache_read(map, reg, &val);
120*4882a593Smuzhiyun map->unlock(map->lock_arg);
121*4882a593Smuzhiyun if (ret)
122*4882a593Smuzhiyun return false;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return true;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
regmap_readable(struct regmap * map,unsigned int reg)127*4882a593Smuzhiyun bool regmap_readable(struct regmap *map, unsigned int reg)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun if (!map->reg_read)
130*4882a593Smuzhiyun return false;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun if (map->max_register && reg > map->max_register)
133*4882a593Smuzhiyun return false;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (map->format.format_write)
136*4882a593Smuzhiyun return false;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (map->readable_reg)
139*4882a593Smuzhiyun return map->readable_reg(map->dev, reg);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (map->rd_table)
142*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->rd_table);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun return true;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
regmap_volatile(struct regmap * map,unsigned int reg)147*4882a593Smuzhiyun bool regmap_volatile(struct regmap *map, unsigned int reg)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun if (!map->format.format_write && !regmap_readable(map, reg))
150*4882a593Smuzhiyun return false;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (map->volatile_reg)
153*4882a593Smuzhiyun return map->volatile_reg(map->dev, reg);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (map->volatile_table)
156*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->volatile_table);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun if (map->cache_ops)
159*4882a593Smuzhiyun return false;
160*4882a593Smuzhiyun else
161*4882a593Smuzhiyun return true;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
regmap_precious(struct regmap * map,unsigned int reg)164*4882a593Smuzhiyun bool regmap_precious(struct regmap *map, unsigned int reg)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun if (!regmap_readable(map, reg))
167*4882a593Smuzhiyun return false;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (map->precious_reg)
170*4882a593Smuzhiyun return map->precious_reg(map->dev, reg);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (map->precious_table)
173*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->precious_table);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return false;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
regmap_writeable_noinc(struct regmap * map,unsigned int reg)178*4882a593Smuzhiyun bool regmap_writeable_noinc(struct regmap *map, unsigned int reg)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun if (map->writeable_noinc_reg)
181*4882a593Smuzhiyun return map->writeable_noinc_reg(map->dev, reg);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (map->wr_noinc_table)
184*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->wr_noinc_table);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return true;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
regmap_readable_noinc(struct regmap * map,unsigned int reg)189*4882a593Smuzhiyun bool regmap_readable_noinc(struct regmap *map, unsigned int reg)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun if (map->readable_noinc_reg)
192*4882a593Smuzhiyun return map->readable_noinc_reg(map->dev, reg);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (map->rd_noinc_table)
195*4882a593Smuzhiyun return regmap_check_range_table(map, reg, map->rd_noinc_table);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun return true;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
regmap_volatile_range(struct regmap * map,unsigned int reg,size_t num)200*4882a593Smuzhiyun static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
201*4882a593Smuzhiyun size_t num)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun unsigned int i;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun for (i = 0; i < num; i++)
206*4882a593Smuzhiyun if (!regmap_volatile(map, reg + regmap_get_offset(map, i)))
207*4882a593Smuzhiyun return false;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun return true;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
regmap_format_12_20_write(struct regmap * map,unsigned int reg,unsigned int val)212*4882a593Smuzhiyun static void regmap_format_12_20_write(struct regmap *map,
213*4882a593Smuzhiyun unsigned int reg, unsigned int val)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun u8 *out = map->work_buf;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun out[0] = reg >> 4;
218*4882a593Smuzhiyun out[1] = (reg << 4) | (val >> 16);
219*4882a593Smuzhiyun out[2] = val >> 8;
220*4882a593Smuzhiyun out[3] = val;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun
regmap_format_2_6_write(struct regmap * map,unsigned int reg,unsigned int val)224*4882a593Smuzhiyun static void regmap_format_2_6_write(struct regmap *map,
225*4882a593Smuzhiyun unsigned int reg, unsigned int val)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun u8 *out = map->work_buf;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun *out = (reg << 6) | val;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
regmap_format_4_12_write(struct regmap * map,unsigned int reg,unsigned int val)232*4882a593Smuzhiyun static void regmap_format_4_12_write(struct regmap *map,
233*4882a593Smuzhiyun unsigned int reg, unsigned int val)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun __be16 *out = map->work_buf;
236*4882a593Smuzhiyun *out = cpu_to_be16((reg << 12) | val);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
regmap_format_7_9_write(struct regmap * map,unsigned int reg,unsigned int val)239*4882a593Smuzhiyun static void regmap_format_7_9_write(struct regmap *map,
240*4882a593Smuzhiyun unsigned int reg, unsigned int val)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun __be16 *out = map->work_buf;
243*4882a593Smuzhiyun *out = cpu_to_be16((reg << 9) | val);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
regmap_format_10_14_write(struct regmap * map,unsigned int reg,unsigned int val)246*4882a593Smuzhiyun static void regmap_format_10_14_write(struct regmap *map,
247*4882a593Smuzhiyun unsigned int reg, unsigned int val)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun u8 *out = map->work_buf;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun out[2] = val;
252*4882a593Smuzhiyun out[1] = (val >> 8) | (reg << 6);
253*4882a593Smuzhiyun out[0] = reg >> 2;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
regmap_format_8(void * buf,unsigned int val,unsigned int shift)256*4882a593Smuzhiyun static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun u8 *b = buf;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun b[0] = val << shift;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
regmap_format_16_be(void * buf,unsigned int val,unsigned int shift)263*4882a593Smuzhiyun static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun put_unaligned_be16(val << shift, buf);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
regmap_format_16_le(void * buf,unsigned int val,unsigned int shift)268*4882a593Smuzhiyun static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun put_unaligned_le16(val << shift, buf);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
regmap_format_16_native(void * buf,unsigned int val,unsigned int shift)273*4882a593Smuzhiyun static void regmap_format_16_native(void *buf, unsigned int val,
274*4882a593Smuzhiyun unsigned int shift)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun u16 v = val << shift;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
regmap_format_24(void * buf,unsigned int val,unsigned int shift)281*4882a593Smuzhiyun static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun u8 *b = buf;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun val <<= shift;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun b[0] = val >> 16;
288*4882a593Smuzhiyun b[1] = val >> 8;
289*4882a593Smuzhiyun b[2] = val;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
regmap_format_32_be(void * buf,unsigned int val,unsigned int shift)292*4882a593Smuzhiyun static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun put_unaligned_be32(val << shift, buf);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
regmap_format_32_le(void * buf,unsigned int val,unsigned int shift)297*4882a593Smuzhiyun static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun put_unaligned_le32(val << shift, buf);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
regmap_format_32_native(void * buf,unsigned int val,unsigned int shift)302*4882a593Smuzhiyun static void regmap_format_32_native(void *buf, unsigned int val,
303*4882a593Smuzhiyun unsigned int shift)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun u32 v = val << shift;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun #ifdef CONFIG_64BIT
regmap_format_64_be(void * buf,unsigned int val,unsigned int shift)311*4882a593Smuzhiyun static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun put_unaligned_be64((u64) val << shift, buf);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
regmap_format_64_le(void * buf,unsigned int val,unsigned int shift)316*4882a593Smuzhiyun static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun put_unaligned_le64((u64) val << shift, buf);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
regmap_format_64_native(void * buf,unsigned int val,unsigned int shift)321*4882a593Smuzhiyun static void regmap_format_64_native(void *buf, unsigned int val,
322*4882a593Smuzhiyun unsigned int shift)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun u64 v = (u64) val << shift;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun #endif
329*4882a593Smuzhiyun
regmap_parse_inplace_noop(void * buf)330*4882a593Smuzhiyun static void regmap_parse_inplace_noop(void *buf)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
regmap_parse_8(const void * buf)334*4882a593Smuzhiyun static unsigned int regmap_parse_8(const void *buf)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun const u8 *b = buf;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun return b[0];
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
regmap_parse_16_be(const void * buf)341*4882a593Smuzhiyun static unsigned int regmap_parse_16_be(const void *buf)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun return get_unaligned_be16(buf);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
regmap_parse_16_le(const void * buf)346*4882a593Smuzhiyun static unsigned int regmap_parse_16_le(const void *buf)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun return get_unaligned_le16(buf);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
regmap_parse_16_be_inplace(void * buf)351*4882a593Smuzhiyun static void regmap_parse_16_be_inplace(void *buf)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun u16 v = get_unaligned_be16(buf);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
regmap_parse_16_le_inplace(void * buf)358*4882a593Smuzhiyun static void regmap_parse_16_le_inplace(void *buf)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun u16 v = get_unaligned_le16(buf);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
regmap_parse_16_native(const void * buf)365*4882a593Smuzhiyun static unsigned int regmap_parse_16_native(const void *buf)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun u16 v;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun memcpy(&v, buf, sizeof(v));
370*4882a593Smuzhiyun return v;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
regmap_parse_24(const void * buf)373*4882a593Smuzhiyun static unsigned int regmap_parse_24(const void *buf)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun const u8 *b = buf;
376*4882a593Smuzhiyun unsigned int ret = b[2];
377*4882a593Smuzhiyun ret |= ((unsigned int)b[1]) << 8;
378*4882a593Smuzhiyun ret |= ((unsigned int)b[0]) << 16;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun return ret;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
regmap_parse_32_be(const void * buf)383*4882a593Smuzhiyun static unsigned int regmap_parse_32_be(const void *buf)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun return get_unaligned_be32(buf);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
regmap_parse_32_le(const void * buf)388*4882a593Smuzhiyun static unsigned int regmap_parse_32_le(const void *buf)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun return get_unaligned_le32(buf);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
regmap_parse_32_be_inplace(void * buf)393*4882a593Smuzhiyun static void regmap_parse_32_be_inplace(void *buf)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun u32 v = get_unaligned_be32(buf);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
regmap_parse_32_le_inplace(void * buf)400*4882a593Smuzhiyun static void regmap_parse_32_le_inplace(void *buf)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun u32 v = get_unaligned_le32(buf);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
regmap_parse_32_native(const void * buf)407*4882a593Smuzhiyun static unsigned int regmap_parse_32_native(const void *buf)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun u32 v;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun memcpy(&v, buf, sizeof(v));
412*4882a593Smuzhiyun return v;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun #ifdef CONFIG_64BIT
regmap_parse_64_be(const void * buf)416*4882a593Smuzhiyun static unsigned int regmap_parse_64_be(const void *buf)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun return get_unaligned_be64(buf);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
regmap_parse_64_le(const void * buf)421*4882a593Smuzhiyun static unsigned int regmap_parse_64_le(const void *buf)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun return get_unaligned_le64(buf);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
regmap_parse_64_be_inplace(void * buf)426*4882a593Smuzhiyun static void regmap_parse_64_be_inplace(void *buf)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun u64 v = get_unaligned_be64(buf);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
regmap_parse_64_le_inplace(void * buf)433*4882a593Smuzhiyun static void regmap_parse_64_le_inplace(void *buf)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun u64 v = get_unaligned_le64(buf);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun memcpy(buf, &v, sizeof(v));
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun
regmap_parse_64_native(const void * buf)440*4882a593Smuzhiyun static unsigned int regmap_parse_64_native(const void *buf)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun u64 v;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun memcpy(&v, buf, sizeof(v));
445*4882a593Smuzhiyun return v;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun #endif
448*4882a593Smuzhiyun
regmap_lock_hwlock(void * __map)449*4882a593Smuzhiyun static void regmap_lock_hwlock(void *__map)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun struct regmap *map = __map;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun hwspin_lock_timeout(map->hwlock, UINT_MAX);
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
regmap_lock_hwlock_irq(void * __map)456*4882a593Smuzhiyun static void regmap_lock_hwlock_irq(void *__map)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun struct regmap *map = __map;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun hwspin_lock_timeout_irq(map->hwlock, UINT_MAX);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
regmap_lock_hwlock_irqsave(void * __map)463*4882a593Smuzhiyun static void regmap_lock_hwlock_irqsave(void *__map)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun struct regmap *map = __map;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun hwspin_lock_timeout_irqsave(map->hwlock, UINT_MAX,
468*4882a593Smuzhiyun &map->spinlock_flags);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
regmap_unlock_hwlock(void * __map)471*4882a593Smuzhiyun static void regmap_unlock_hwlock(void *__map)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun struct regmap *map = __map;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun hwspin_unlock(map->hwlock);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
regmap_unlock_hwlock_irq(void * __map)478*4882a593Smuzhiyun static void regmap_unlock_hwlock_irq(void *__map)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct regmap *map = __map;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun hwspin_unlock_irq(map->hwlock);
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
regmap_unlock_hwlock_irqrestore(void * __map)485*4882a593Smuzhiyun static void regmap_unlock_hwlock_irqrestore(void *__map)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun struct regmap *map = __map;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun hwspin_unlock_irqrestore(map->hwlock, &map->spinlock_flags);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
regmap_lock_unlock_none(void * __map)492*4882a593Smuzhiyun static void regmap_lock_unlock_none(void *__map)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
regmap_lock_mutex(void * __map)497*4882a593Smuzhiyun static void regmap_lock_mutex(void *__map)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun struct regmap *map = __map;
500*4882a593Smuzhiyun mutex_lock(&map->mutex);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
regmap_unlock_mutex(void * __map)503*4882a593Smuzhiyun static void regmap_unlock_mutex(void *__map)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun struct regmap *map = __map;
506*4882a593Smuzhiyun mutex_unlock(&map->mutex);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
regmap_lock_spinlock(void * __map)509*4882a593Smuzhiyun static void regmap_lock_spinlock(void *__map)
510*4882a593Smuzhiyun __acquires(&map->spinlock)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct regmap *map = __map;
513*4882a593Smuzhiyun unsigned long flags;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun spin_lock_irqsave(&map->spinlock, flags);
516*4882a593Smuzhiyun map->spinlock_flags = flags;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
regmap_unlock_spinlock(void * __map)519*4882a593Smuzhiyun static void regmap_unlock_spinlock(void *__map)
520*4882a593Smuzhiyun __releases(&map->spinlock)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun struct regmap *map = __map;
523*4882a593Smuzhiyun spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
dev_get_regmap_release(struct device * dev,void * res)526*4882a593Smuzhiyun static void dev_get_regmap_release(struct device *dev, void *res)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun /*
529*4882a593Smuzhiyun * We don't actually have anything to do here; the goal here
530*4882a593Smuzhiyun * is not to manage the regmap but to provide a simple way to
531*4882a593Smuzhiyun * get the regmap back given a struct device.
532*4882a593Smuzhiyun */
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
_regmap_range_add(struct regmap * map,struct regmap_range_node * data)535*4882a593Smuzhiyun static bool _regmap_range_add(struct regmap *map,
536*4882a593Smuzhiyun struct regmap_range_node *data)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun struct rb_root *root = &map->range_tree;
539*4882a593Smuzhiyun struct rb_node **new = &(root->rb_node), *parent = NULL;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun while (*new) {
542*4882a593Smuzhiyun struct regmap_range_node *this =
543*4882a593Smuzhiyun rb_entry(*new, struct regmap_range_node, node);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun parent = *new;
546*4882a593Smuzhiyun if (data->range_max < this->range_min)
547*4882a593Smuzhiyun new = &((*new)->rb_left);
548*4882a593Smuzhiyun else if (data->range_min > this->range_max)
549*4882a593Smuzhiyun new = &((*new)->rb_right);
550*4882a593Smuzhiyun else
551*4882a593Smuzhiyun return false;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun rb_link_node(&data->node, parent, new);
555*4882a593Smuzhiyun rb_insert_color(&data->node, root);
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun return true;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun
_regmap_range_lookup(struct regmap * map,unsigned int reg)560*4882a593Smuzhiyun static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
561*4882a593Smuzhiyun unsigned int reg)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun struct rb_node *node = map->range_tree.rb_node;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun while (node) {
566*4882a593Smuzhiyun struct regmap_range_node *this =
567*4882a593Smuzhiyun rb_entry(node, struct regmap_range_node, node);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (reg < this->range_min)
570*4882a593Smuzhiyun node = node->rb_left;
571*4882a593Smuzhiyun else if (reg > this->range_max)
572*4882a593Smuzhiyun node = node->rb_right;
573*4882a593Smuzhiyun else
574*4882a593Smuzhiyun return this;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun return NULL;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
regmap_range_exit(struct regmap * map)580*4882a593Smuzhiyun static void regmap_range_exit(struct regmap *map)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun struct rb_node *next;
583*4882a593Smuzhiyun struct regmap_range_node *range_node;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun next = rb_first(&map->range_tree);
586*4882a593Smuzhiyun while (next) {
587*4882a593Smuzhiyun range_node = rb_entry(next, struct regmap_range_node, node);
588*4882a593Smuzhiyun next = rb_next(&range_node->node);
589*4882a593Smuzhiyun rb_erase(&range_node->node, &map->range_tree);
590*4882a593Smuzhiyun kfree(range_node);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun kfree(map->selector_work_buf);
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
regmap_set_name(struct regmap * map,const struct regmap_config * config)596*4882a593Smuzhiyun static int regmap_set_name(struct regmap *map, const struct regmap_config *config)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun if (config->name) {
599*4882a593Smuzhiyun const char *name = kstrdup_const(config->name, GFP_KERNEL);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun if (!name)
602*4882a593Smuzhiyun return -ENOMEM;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun kfree_const(map->name);
605*4882a593Smuzhiyun map->name = name;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun return 0;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
regmap_attach_dev(struct device * dev,struct regmap * map,const struct regmap_config * config)611*4882a593Smuzhiyun int regmap_attach_dev(struct device *dev, struct regmap *map,
612*4882a593Smuzhiyun const struct regmap_config *config)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun struct regmap **m;
615*4882a593Smuzhiyun int ret;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun map->dev = dev;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun ret = regmap_set_name(map, config);
620*4882a593Smuzhiyun if (ret)
621*4882a593Smuzhiyun return ret;
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun regmap_debugfs_exit(map);
624*4882a593Smuzhiyun regmap_debugfs_init(map);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /* Add a devres resource for dev_get_regmap() */
627*4882a593Smuzhiyun m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
628*4882a593Smuzhiyun if (!m) {
629*4882a593Smuzhiyun regmap_debugfs_exit(map);
630*4882a593Smuzhiyun return -ENOMEM;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun *m = map;
633*4882a593Smuzhiyun devres_add(dev, m);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun return 0;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_attach_dev);
638*4882a593Smuzhiyun
regmap_get_reg_endian(const struct regmap_bus * bus,const struct regmap_config * config)639*4882a593Smuzhiyun static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
640*4882a593Smuzhiyun const struct regmap_config *config)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun enum regmap_endian endian;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /* Retrieve the endianness specification from the regmap config */
645*4882a593Smuzhiyun endian = config->reg_format_endian;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun /* If the regmap config specified a non-default value, use that */
648*4882a593Smuzhiyun if (endian != REGMAP_ENDIAN_DEFAULT)
649*4882a593Smuzhiyun return endian;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun /* Retrieve the endianness specification from the bus config */
652*4882a593Smuzhiyun if (bus && bus->reg_format_endian_default)
653*4882a593Smuzhiyun endian = bus->reg_format_endian_default;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun /* If the bus specified a non-default value, use that */
656*4882a593Smuzhiyun if (endian != REGMAP_ENDIAN_DEFAULT)
657*4882a593Smuzhiyun return endian;
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /* Use this if no other value was found */
660*4882a593Smuzhiyun return REGMAP_ENDIAN_BIG;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun
regmap_get_val_endian(struct device * dev,const struct regmap_bus * bus,const struct regmap_config * config)663*4882a593Smuzhiyun enum regmap_endian regmap_get_val_endian(struct device *dev,
664*4882a593Smuzhiyun const struct regmap_bus *bus,
665*4882a593Smuzhiyun const struct regmap_config *config)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
668*4882a593Smuzhiyun enum regmap_endian endian;
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun /* Retrieve the endianness specification from the regmap config */
671*4882a593Smuzhiyun endian = config->val_format_endian;
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun /* If the regmap config specified a non-default value, use that */
674*4882a593Smuzhiyun if (endian != REGMAP_ENDIAN_DEFAULT)
675*4882a593Smuzhiyun return endian;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /* If the firmware node exist try to get endianness from it */
678*4882a593Smuzhiyun if (fwnode_property_read_bool(fwnode, "big-endian"))
679*4882a593Smuzhiyun endian = REGMAP_ENDIAN_BIG;
680*4882a593Smuzhiyun else if (fwnode_property_read_bool(fwnode, "little-endian"))
681*4882a593Smuzhiyun endian = REGMAP_ENDIAN_LITTLE;
682*4882a593Smuzhiyun else if (fwnode_property_read_bool(fwnode, "native-endian"))
683*4882a593Smuzhiyun endian = REGMAP_ENDIAN_NATIVE;
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /* If the endianness was specified in fwnode, use that */
686*4882a593Smuzhiyun if (endian != REGMAP_ENDIAN_DEFAULT)
687*4882a593Smuzhiyun return endian;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun /* Retrieve the endianness specification from the bus config */
690*4882a593Smuzhiyun if (bus && bus->val_format_endian_default)
691*4882a593Smuzhiyun endian = bus->val_format_endian_default;
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /* If the bus specified a non-default value, use that */
694*4882a593Smuzhiyun if (endian != REGMAP_ENDIAN_DEFAULT)
695*4882a593Smuzhiyun return endian;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /* Use this if no other value was found */
698*4882a593Smuzhiyun return REGMAP_ENDIAN_BIG;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_val_endian);
701*4882a593Smuzhiyun
__regmap_init(struct device * dev,const struct regmap_bus * bus,void * bus_context,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)702*4882a593Smuzhiyun struct regmap *__regmap_init(struct device *dev,
703*4882a593Smuzhiyun const struct regmap_bus *bus,
704*4882a593Smuzhiyun void *bus_context,
705*4882a593Smuzhiyun const struct regmap_config *config,
706*4882a593Smuzhiyun struct lock_class_key *lock_key,
707*4882a593Smuzhiyun const char *lock_name)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun struct regmap *map;
710*4882a593Smuzhiyun int ret = -EINVAL;
711*4882a593Smuzhiyun enum regmap_endian reg_endian, val_endian;
712*4882a593Smuzhiyun int i, j;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun if (!config)
715*4882a593Smuzhiyun goto err;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun map = kzalloc(sizeof(*map), GFP_KERNEL);
718*4882a593Smuzhiyun if (map == NULL) {
719*4882a593Smuzhiyun ret = -ENOMEM;
720*4882a593Smuzhiyun goto err;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun ret = regmap_set_name(map, config);
724*4882a593Smuzhiyun if (ret)
725*4882a593Smuzhiyun goto err_map;
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun ret = -EINVAL; /* Later error paths rely on this */
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun if (config->disable_locking) {
730*4882a593Smuzhiyun map->lock = map->unlock = regmap_lock_unlock_none;
731*4882a593Smuzhiyun map->can_sleep = config->can_sleep;
732*4882a593Smuzhiyun regmap_debugfs_disable(map);
733*4882a593Smuzhiyun } else if (config->lock && config->unlock) {
734*4882a593Smuzhiyun map->lock = config->lock;
735*4882a593Smuzhiyun map->unlock = config->unlock;
736*4882a593Smuzhiyun map->lock_arg = config->lock_arg;
737*4882a593Smuzhiyun map->can_sleep = config->can_sleep;
738*4882a593Smuzhiyun } else if (config->use_hwlock) {
739*4882a593Smuzhiyun map->hwlock = hwspin_lock_request_specific(config->hwlock_id);
740*4882a593Smuzhiyun if (!map->hwlock) {
741*4882a593Smuzhiyun ret = -ENXIO;
742*4882a593Smuzhiyun goto err_name;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun switch (config->hwlock_mode) {
746*4882a593Smuzhiyun case HWLOCK_IRQSTATE:
747*4882a593Smuzhiyun map->lock = regmap_lock_hwlock_irqsave;
748*4882a593Smuzhiyun map->unlock = regmap_unlock_hwlock_irqrestore;
749*4882a593Smuzhiyun break;
750*4882a593Smuzhiyun case HWLOCK_IRQ:
751*4882a593Smuzhiyun map->lock = regmap_lock_hwlock_irq;
752*4882a593Smuzhiyun map->unlock = regmap_unlock_hwlock_irq;
753*4882a593Smuzhiyun break;
754*4882a593Smuzhiyun default:
755*4882a593Smuzhiyun map->lock = regmap_lock_hwlock;
756*4882a593Smuzhiyun map->unlock = regmap_unlock_hwlock;
757*4882a593Smuzhiyun break;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun map->lock_arg = map;
761*4882a593Smuzhiyun } else {
762*4882a593Smuzhiyun if ((bus && bus->fast_io) ||
763*4882a593Smuzhiyun config->fast_io) {
764*4882a593Smuzhiyun spin_lock_init(&map->spinlock);
765*4882a593Smuzhiyun map->lock = regmap_lock_spinlock;
766*4882a593Smuzhiyun map->unlock = regmap_unlock_spinlock;
767*4882a593Smuzhiyun lockdep_set_class_and_name(&map->spinlock,
768*4882a593Smuzhiyun lock_key, lock_name);
769*4882a593Smuzhiyun } else {
770*4882a593Smuzhiyun mutex_init(&map->mutex);
771*4882a593Smuzhiyun map->lock = regmap_lock_mutex;
772*4882a593Smuzhiyun map->unlock = regmap_unlock_mutex;
773*4882a593Smuzhiyun map->can_sleep = true;
774*4882a593Smuzhiyun lockdep_set_class_and_name(&map->mutex,
775*4882a593Smuzhiyun lock_key, lock_name);
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun map->lock_arg = map;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun /*
781*4882a593Smuzhiyun * When we write in fast-paths with regmap_bulk_write() don't allocate
782*4882a593Smuzhiyun * scratch buffers with sleeping allocations.
783*4882a593Smuzhiyun */
784*4882a593Smuzhiyun if ((bus && bus->fast_io) || config->fast_io)
785*4882a593Smuzhiyun map->alloc_flags = GFP_ATOMIC;
786*4882a593Smuzhiyun else
787*4882a593Smuzhiyun map->alloc_flags = GFP_KERNEL;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
790*4882a593Smuzhiyun map->format.pad_bytes = config->pad_bits / 8;
791*4882a593Smuzhiyun map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
792*4882a593Smuzhiyun map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
793*4882a593Smuzhiyun config->val_bits + config->pad_bits, 8);
794*4882a593Smuzhiyun map->reg_shift = config->pad_bits % 8;
795*4882a593Smuzhiyun if (config->reg_stride)
796*4882a593Smuzhiyun map->reg_stride = config->reg_stride;
797*4882a593Smuzhiyun else
798*4882a593Smuzhiyun map->reg_stride = 1;
799*4882a593Smuzhiyun if (is_power_of_2(map->reg_stride))
800*4882a593Smuzhiyun map->reg_stride_order = ilog2(map->reg_stride);
801*4882a593Smuzhiyun else
802*4882a593Smuzhiyun map->reg_stride_order = -1;
803*4882a593Smuzhiyun map->use_single_read = config->use_single_read || !bus || !bus->read;
804*4882a593Smuzhiyun map->use_single_write = config->use_single_write || !bus || !bus->write;
805*4882a593Smuzhiyun map->can_multi_write = config->can_multi_write && bus && bus->write;
806*4882a593Smuzhiyun if (bus) {
807*4882a593Smuzhiyun map->max_raw_read = bus->max_raw_read;
808*4882a593Smuzhiyun map->max_raw_write = bus->max_raw_write;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun map->dev = dev;
811*4882a593Smuzhiyun map->bus = bus;
812*4882a593Smuzhiyun map->bus_context = bus_context;
813*4882a593Smuzhiyun map->max_register = config->max_register;
814*4882a593Smuzhiyun map->wr_table = config->wr_table;
815*4882a593Smuzhiyun map->rd_table = config->rd_table;
816*4882a593Smuzhiyun map->volatile_table = config->volatile_table;
817*4882a593Smuzhiyun map->precious_table = config->precious_table;
818*4882a593Smuzhiyun map->wr_noinc_table = config->wr_noinc_table;
819*4882a593Smuzhiyun map->rd_noinc_table = config->rd_noinc_table;
820*4882a593Smuzhiyun map->writeable_reg = config->writeable_reg;
821*4882a593Smuzhiyun map->readable_reg = config->readable_reg;
822*4882a593Smuzhiyun map->volatile_reg = config->volatile_reg;
823*4882a593Smuzhiyun map->precious_reg = config->precious_reg;
824*4882a593Smuzhiyun map->writeable_noinc_reg = config->writeable_noinc_reg;
825*4882a593Smuzhiyun map->readable_noinc_reg = config->readable_noinc_reg;
826*4882a593Smuzhiyun map->cache_type = config->cache_type;
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun spin_lock_init(&map->async_lock);
829*4882a593Smuzhiyun INIT_LIST_HEAD(&map->async_list);
830*4882a593Smuzhiyun INIT_LIST_HEAD(&map->async_free);
831*4882a593Smuzhiyun init_waitqueue_head(&map->async_waitq);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun if (config->read_flag_mask ||
834*4882a593Smuzhiyun config->write_flag_mask ||
835*4882a593Smuzhiyun config->zero_flag_mask) {
836*4882a593Smuzhiyun map->read_flag_mask = config->read_flag_mask;
837*4882a593Smuzhiyun map->write_flag_mask = config->write_flag_mask;
838*4882a593Smuzhiyun } else if (bus) {
839*4882a593Smuzhiyun map->read_flag_mask = bus->read_flag_mask;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun if (!bus) {
843*4882a593Smuzhiyun map->reg_read = config->reg_read;
844*4882a593Smuzhiyun map->reg_write = config->reg_write;
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun map->defer_caching = false;
847*4882a593Smuzhiyun goto skip_format_initialization;
848*4882a593Smuzhiyun } else if (!bus->read || !bus->write) {
849*4882a593Smuzhiyun map->reg_read = _regmap_bus_reg_read;
850*4882a593Smuzhiyun map->reg_write = _regmap_bus_reg_write;
851*4882a593Smuzhiyun map->reg_update_bits = bus->reg_update_bits;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun map->defer_caching = false;
854*4882a593Smuzhiyun goto skip_format_initialization;
855*4882a593Smuzhiyun } else {
856*4882a593Smuzhiyun map->reg_read = _regmap_bus_read;
857*4882a593Smuzhiyun map->reg_update_bits = bus->reg_update_bits;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun reg_endian = regmap_get_reg_endian(bus, config);
861*4882a593Smuzhiyun val_endian = regmap_get_val_endian(dev, bus, config);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun switch (config->reg_bits + map->reg_shift) {
864*4882a593Smuzhiyun case 2:
865*4882a593Smuzhiyun switch (config->val_bits) {
866*4882a593Smuzhiyun case 6:
867*4882a593Smuzhiyun map->format.format_write = regmap_format_2_6_write;
868*4882a593Smuzhiyun break;
869*4882a593Smuzhiyun default:
870*4882a593Smuzhiyun goto err_hwlock;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun break;
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun case 4:
875*4882a593Smuzhiyun switch (config->val_bits) {
876*4882a593Smuzhiyun case 12:
877*4882a593Smuzhiyun map->format.format_write = regmap_format_4_12_write;
878*4882a593Smuzhiyun break;
879*4882a593Smuzhiyun default:
880*4882a593Smuzhiyun goto err_hwlock;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun break;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun case 7:
885*4882a593Smuzhiyun switch (config->val_bits) {
886*4882a593Smuzhiyun case 9:
887*4882a593Smuzhiyun map->format.format_write = regmap_format_7_9_write;
888*4882a593Smuzhiyun break;
889*4882a593Smuzhiyun default:
890*4882a593Smuzhiyun goto err_hwlock;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun break;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun case 10:
895*4882a593Smuzhiyun switch (config->val_bits) {
896*4882a593Smuzhiyun case 14:
897*4882a593Smuzhiyun map->format.format_write = regmap_format_10_14_write;
898*4882a593Smuzhiyun break;
899*4882a593Smuzhiyun default:
900*4882a593Smuzhiyun goto err_hwlock;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun break;
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun case 12:
905*4882a593Smuzhiyun switch (config->val_bits) {
906*4882a593Smuzhiyun case 20:
907*4882a593Smuzhiyun map->format.format_write = regmap_format_12_20_write;
908*4882a593Smuzhiyun break;
909*4882a593Smuzhiyun default:
910*4882a593Smuzhiyun goto err_hwlock;
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun break;
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun case 8:
915*4882a593Smuzhiyun map->format.format_reg = regmap_format_8;
916*4882a593Smuzhiyun break;
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun case 16:
919*4882a593Smuzhiyun switch (reg_endian) {
920*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
921*4882a593Smuzhiyun map->format.format_reg = regmap_format_16_be;
922*4882a593Smuzhiyun break;
923*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
924*4882a593Smuzhiyun map->format.format_reg = regmap_format_16_le;
925*4882a593Smuzhiyun break;
926*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
927*4882a593Smuzhiyun map->format.format_reg = regmap_format_16_native;
928*4882a593Smuzhiyun break;
929*4882a593Smuzhiyun default:
930*4882a593Smuzhiyun goto err_hwlock;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun break;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun case 24:
935*4882a593Smuzhiyun if (reg_endian != REGMAP_ENDIAN_BIG)
936*4882a593Smuzhiyun goto err_hwlock;
937*4882a593Smuzhiyun map->format.format_reg = regmap_format_24;
938*4882a593Smuzhiyun break;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun case 32:
941*4882a593Smuzhiyun switch (reg_endian) {
942*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
943*4882a593Smuzhiyun map->format.format_reg = regmap_format_32_be;
944*4882a593Smuzhiyun break;
945*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
946*4882a593Smuzhiyun map->format.format_reg = regmap_format_32_le;
947*4882a593Smuzhiyun break;
948*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
949*4882a593Smuzhiyun map->format.format_reg = regmap_format_32_native;
950*4882a593Smuzhiyun break;
951*4882a593Smuzhiyun default:
952*4882a593Smuzhiyun goto err_hwlock;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun break;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun #ifdef CONFIG_64BIT
957*4882a593Smuzhiyun case 64:
958*4882a593Smuzhiyun switch (reg_endian) {
959*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
960*4882a593Smuzhiyun map->format.format_reg = regmap_format_64_be;
961*4882a593Smuzhiyun break;
962*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
963*4882a593Smuzhiyun map->format.format_reg = regmap_format_64_le;
964*4882a593Smuzhiyun break;
965*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
966*4882a593Smuzhiyun map->format.format_reg = regmap_format_64_native;
967*4882a593Smuzhiyun break;
968*4882a593Smuzhiyun default:
969*4882a593Smuzhiyun goto err_hwlock;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun break;
972*4882a593Smuzhiyun #endif
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun default:
975*4882a593Smuzhiyun goto err_hwlock;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun if (val_endian == REGMAP_ENDIAN_NATIVE)
979*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_inplace_noop;
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun switch (config->val_bits) {
982*4882a593Smuzhiyun case 8:
983*4882a593Smuzhiyun map->format.format_val = regmap_format_8;
984*4882a593Smuzhiyun map->format.parse_val = regmap_parse_8;
985*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_inplace_noop;
986*4882a593Smuzhiyun break;
987*4882a593Smuzhiyun case 16:
988*4882a593Smuzhiyun switch (val_endian) {
989*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
990*4882a593Smuzhiyun map->format.format_val = regmap_format_16_be;
991*4882a593Smuzhiyun map->format.parse_val = regmap_parse_16_be;
992*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_16_be_inplace;
993*4882a593Smuzhiyun break;
994*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
995*4882a593Smuzhiyun map->format.format_val = regmap_format_16_le;
996*4882a593Smuzhiyun map->format.parse_val = regmap_parse_16_le;
997*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_16_le_inplace;
998*4882a593Smuzhiyun break;
999*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
1000*4882a593Smuzhiyun map->format.format_val = regmap_format_16_native;
1001*4882a593Smuzhiyun map->format.parse_val = regmap_parse_16_native;
1002*4882a593Smuzhiyun break;
1003*4882a593Smuzhiyun default:
1004*4882a593Smuzhiyun goto err_hwlock;
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun break;
1007*4882a593Smuzhiyun case 24:
1008*4882a593Smuzhiyun if (val_endian != REGMAP_ENDIAN_BIG)
1009*4882a593Smuzhiyun goto err_hwlock;
1010*4882a593Smuzhiyun map->format.format_val = regmap_format_24;
1011*4882a593Smuzhiyun map->format.parse_val = regmap_parse_24;
1012*4882a593Smuzhiyun break;
1013*4882a593Smuzhiyun case 32:
1014*4882a593Smuzhiyun switch (val_endian) {
1015*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
1016*4882a593Smuzhiyun map->format.format_val = regmap_format_32_be;
1017*4882a593Smuzhiyun map->format.parse_val = regmap_parse_32_be;
1018*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_32_be_inplace;
1019*4882a593Smuzhiyun break;
1020*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
1021*4882a593Smuzhiyun map->format.format_val = regmap_format_32_le;
1022*4882a593Smuzhiyun map->format.parse_val = regmap_parse_32_le;
1023*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_32_le_inplace;
1024*4882a593Smuzhiyun break;
1025*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
1026*4882a593Smuzhiyun map->format.format_val = regmap_format_32_native;
1027*4882a593Smuzhiyun map->format.parse_val = regmap_parse_32_native;
1028*4882a593Smuzhiyun break;
1029*4882a593Smuzhiyun default:
1030*4882a593Smuzhiyun goto err_hwlock;
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun break;
1033*4882a593Smuzhiyun #ifdef CONFIG_64BIT
1034*4882a593Smuzhiyun case 64:
1035*4882a593Smuzhiyun switch (val_endian) {
1036*4882a593Smuzhiyun case REGMAP_ENDIAN_BIG:
1037*4882a593Smuzhiyun map->format.format_val = regmap_format_64_be;
1038*4882a593Smuzhiyun map->format.parse_val = regmap_parse_64_be;
1039*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_64_be_inplace;
1040*4882a593Smuzhiyun break;
1041*4882a593Smuzhiyun case REGMAP_ENDIAN_LITTLE:
1042*4882a593Smuzhiyun map->format.format_val = regmap_format_64_le;
1043*4882a593Smuzhiyun map->format.parse_val = regmap_parse_64_le;
1044*4882a593Smuzhiyun map->format.parse_inplace = regmap_parse_64_le_inplace;
1045*4882a593Smuzhiyun break;
1046*4882a593Smuzhiyun case REGMAP_ENDIAN_NATIVE:
1047*4882a593Smuzhiyun map->format.format_val = regmap_format_64_native;
1048*4882a593Smuzhiyun map->format.parse_val = regmap_parse_64_native;
1049*4882a593Smuzhiyun break;
1050*4882a593Smuzhiyun default:
1051*4882a593Smuzhiyun goto err_hwlock;
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun break;
1054*4882a593Smuzhiyun #endif
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun if (map->format.format_write) {
1058*4882a593Smuzhiyun if ((reg_endian != REGMAP_ENDIAN_BIG) ||
1059*4882a593Smuzhiyun (val_endian != REGMAP_ENDIAN_BIG))
1060*4882a593Smuzhiyun goto err_hwlock;
1061*4882a593Smuzhiyun map->use_single_write = true;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun if (!map->format.format_write &&
1065*4882a593Smuzhiyun !(map->format.format_reg && map->format.format_val))
1066*4882a593Smuzhiyun goto err_hwlock;
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
1069*4882a593Smuzhiyun if (map->work_buf == NULL) {
1070*4882a593Smuzhiyun ret = -ENOMEM;
1071*4882a593Smuzhiyun goto err_hwlock;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun if (map->format.format_write) {
1075*4882a593Smuzhiyun map->defer_caching = false;
1076*4882a593Smuzhiyun map->reg_write = _regmap_bus_formatted_write;
1077*4882a593Smuzhiyun } else if (map->format.format_val) {
1078*4882a593Smuzhiyun map->defer_caching = true;
1079*4882a593Smuzhiyun map->reg_write = _regmap_bus_raw_write;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun skip_format_initialization:
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun map->range_tree = RB_ROOT;
1085*4882a593Smuzhiyun for (i = 0; i < config->num_ranges; i++) {
1086*4882a593Smuzhiyun const struct regmap_range_cfg *range_cfg = &config->ranges[i];
1087*4882a593Smuzhiyun struct regmap_range_node *new;
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /* Sanity check */
1090*4882a593Smuzhiyun if (range_cfg->range_max < range_cfg->range_min) {
1091*4882a593Smuzhiyun dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
1092*4882a593Smuzhiyun range_cfg->range_max, range_cfg->range_min);
1093*4882a593Smuzhiyun goto err_range;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun if (range_cfg->range_max > map->max_register) {
1097*4882a593Smuzhiyun dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
1098*4882a593Smuzhiyun range_cfg->range_max, map->max_register);
1099*4882a593Smuzhiyun goto err_range;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun if (range_cfg->selector_reg > map->max_register) {
1103*4882a593Smuzhiyun dev_err(map->dev,
1104*4882a593Smuzhiyun "Invalid range %d: selector out of map\n", i);
1105*4882a593Smuzhiyun goto err_range;
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun if (range_cfg->window_len == 0) {
1109*4882a593Smuzhiyun dev_err(map->dev, "Invalid range %d: window_len 0\n",
1110*4882a593Smuzhiyun i);
1111*4882a593Smuzhiyun goto err_range;
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun /* Make sure, that this register range has no selector
1115*4882a593Smuzhiyun or data window within its boundary */
1116*4882a593Smuzhiyun for (j = 0; j < config->num_ranges; j++) {
1117*4882a593Smuzhiyun unsigned sel_reg = config->ranges[j].selector_reg;
1118*4882a593Smuzhiyun unsigned win_min = config->ranges[j].window_start;
1119*4882a593Smuzhiyun unsigned win_max = win_min +
1120*4882a593Smuzhiyun config->ranges[j].window_len - 1;
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun /* Allow data window inside its own virtual range */
1123*4882a593Smuzhiyun if (j == i)
1124*4882a593Smuzhiyun continue;
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun if (range_cfg->range_min <= sel_reg &&
1127*4882a593Smuzhiyun sel_reg <= range_cfg->range_max) {
1128*4882a593Smuzhiyun dev_err(map->dev,
1129*4882a593Smuzhiyun "Range %d: selector for %d in window\n",
1130*4882a593Smuzhiyun i, j);
1131*4882a593Smuzhiyun goto err_range;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun if (!(win_max < range_cfg->range_min ||
1135*4882a593Smuzhiyun win_min > range_cfg->range_max)) {
1136*4882a593Smuzhiyun dev_err(map->dev,
1137*4882a593Smuzhiyun "Range %d: window for %d in window\n",
1138*4882a593Smuzhiyun i, j);
1139*4882a593Smuzhiyun goto err_range;
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun new = kzalloc(sizeof(*new), GFP_KERNEL);
1144*4882a593Smuzhiyun if (new == NULL) {
1145*4882a593Smuzhiyun ret = -ENOMEM;
1146*4882a593Smuzhiyun goto err_range;
1147*4882a593Smuzhiyun }
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun new->map = map;
1150*4882a593Smuzhiyun new->name = range_cfg->name;
1151*4882a593Smuzhiyun new->range_min = range_cfg->range_min;
1152*4882a593Smuzhiyun new->range_max = range_cfg->range_max;
1153*4882a593Smuzhiyun new->selector_reg = range_cfg->selector_reg;
1154*4882a593Smuzhiyun new->selector_mask = range_cfg->selector_mask;
1155*4882a593Smuzhiyun new->selector_shift = range_cfg->selector_shift;
1156*4882a593Smuzhiyun new->window_start = range_cfg->window_start;
1157*4882a593Smuzhiyun new->window_len = range_cfg->window_len;
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun if (!_regmap_range_add(map, new)) {
1160*4882a593Smuzhiyun dev_err(map->dev, "Failed to add range %d\n", i);
1161*4882a593Smuzhiyun kfree(new);
1162*4882a593Smuzhiyun goto err_range;
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun if (map->selector_work_buf == NULL) {
1166*4882a593Smuzhiyun map->selector_work_buf =
1167*4882a593Smuzhiyun kzalloc(map->format.buf_size, GFP_KERNEL);
1168*4882a593Smuzhiyun if (map->selector_work_buf == NULL) {
1169*4882a593Smuzhiyun ret = -ENOMEM;
1170*4882a593Smuzhiyun goto err_range;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun ret = regcache_init(map, config);
1176*4882a593Smuzhiyun if (ret != 0)
1177*4882a593Smuzhiyun goto err_range;
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun if (dev) {
1180*4882a593Smuzhiyun ret = regmap_attach_dev(dev, map, config);
1181*4882a593Smuzhiyun if (ret != 0)
1182*4882a593Smuzhiyun goto err_regcache;
1183*4882a593Smuzhiyun } else {
1184*4882a593Smuzhiyun regmap_debugfs_init(map);
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun return map;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun err_regcache:
1190*4882a593Smuzhiyun regcache_exit(map);
1191*4882a593Smuzhiyun err_range:
1192*4882a593Smuzhiyun regmap_range_exit(map);
1193*4882a593Smuzhiyun kfree(map->work_buf);
1194*4882a593Smuzhiyun err_hwlock:
1195*4882a593Smuzhiyun if (map->hwlock)
1196*4882a593Smuzhiyun hwspin_lock_free(map->hwlock);
1197*4882a593Smuzhiyun err_name:
1198*4882a593Smuzhiyun kfree_const(map->name);
1199*4882a593Smuzhiyun err_map:
1200*4882a593Smuzhiyun kfree(map);
1201*4882a593Smuzhiyun err:
1202*4882a593Smuzhiyun return ERR_PTR(ret);
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__regmap_init);
1205*4882a593Smuzhiyun
devm_regmap_release(struct device * dev,void * res)1206*4882a593Smuzhiyun static void devm_regmap_release(struct device *dev, void *res)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun regmap_exit(*(struct regmap **)res);
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun
__devm_regmap_init(struct device * dev,const struct regmap_bus * bus,void * bus_context,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)1211*4882a593Smuzhiyun struct regmap *__devm_regmap_init(struct device *dev,
1212*4882a593Smuzhiyun const struct regmap_bus *bus,
1213*4882a593Smuzhiyun void *bus_context,
1214*4882a593Smuzhiyun const struct regmap_config *config,
1215*4882a593Smuzhiyun struct lock_class_key *lock_key,
1216*4882a593Smuzhiyun const char *lock_name)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun struct regmap **ptr, *regmap;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1221*4882a593Smuzhiyun if (!ptr)
1222*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun regmap = __regmap_init(dev, bus, bus_context, config,
1225*4882a593Smuzhiyun lock_key, lock_name);
1226*4882a593Smuzhiyun if (!IS_ERR(regmap)) {
1227*4882a593Smuzhiyun *ptr = regmap;
1228*4882a593Smuzhiyun devres_add(dev, ptr);
1229*4882a593Smuzhiyun } else {
1230*4882a593Smuzhiyun devres_free(ptr);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun return regmap;
1234*4882a593Smuzhiyun }
1235*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__devm_regmap_init);
1236*4882a593Smuzhiyun
regmap_field_init(struct regmap_field * rm_field,struct regmap * regmap,struct reg_field reg_field)1237*4882a593Smuzhiyun static void regmap_field_init(struct regmap_field *rm_field,
1238*4882a593Smuzhiyun struct regmap *regmap, struct reg_field reg_field)
1239*4882a593Smuzhiyun {
1240*4882a593Smuzhiyun rm_field->regmap = regmap;
1241*4882a593Smuzhiyun rm_field->reg = reg_field.reg;
1242*4882a593Smuzhiyun rm_field->shift = reg_field.lsb;
1243*4882a593Smuzhiyun rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
1244*4882a593Smuzhiyun rm_field->id_size = reg_field.id_size;
1245*4882a593Smuzhiyun rm_field->id_offset = reg_field.id_offset;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun /**
1249*4882a593Smuzhiyun * devm_regmap_field_alloc() - Allocate and initialise a register field.
1250*4882a593Smuzhiyun *
1251*4882a593Smuzhiyun * @dev: Device that will be interacted with
1252*4882a593Smuzhiyun * @regmap: regmap bank in which this register field is located.
1253*4882a593Smuzhiyun * @reg_field: Register field with in the bank.
1254*4882a593Smuzhiyun *
1255*4882a593Smuzhiyun * The return value will be an ERR_PTR() on error or a valid pointer
1256*4882a593Smuzhiyun * to a struct regmap_field. The regmap_field will be automatically freed
1257*4882a593Smuzhiyun * by the device management code.
1258*4882a593Smuzhiyun */
devm_regmap_field_alloc(struct device * dev,struct regmap * regmap,struct reg_field reg_field)1259*4882a593Smuzhiyun struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1260*4882a593Smuzhiyun struct regmap *regmap, struct reg_field reg_field)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun struct regmap_field *rm_field = devm_kzalloc(dev,
1263*4882a593Smuzhiyun sizeof(*rm_field), GFP_KERNEL);
1264*4882a593Smuzhiyun if (!rm_field)
1265*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun regmap_field_init(rm_field, regmap, reg_field);
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun return rm_field;
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun /**
1276*4882a593Smuzhiyun * regmap_field_bulk_alloc() - Allocate and initialise a bulk register field.
1277*4882a593Smuzhiyun *
1278*4882a593Smuzhiyun * @regmap: regmap bank in which this register field is located.
1279*4882a593Smuzhiyun * @rm_field: regmap register fields within the bank.
1280*4882a593Smuzhiyun * @reg_field: Register fields within the bank.
1281*4882a593Smuzhiyun * @num_fields: Number of register fields.
1282*4882a593Smuzhiyun *
1283*4882a593Smuzhiyun * The return value will be an -ENOMEM on error or zero for success.
1284*4882a593Smuzhiyun * Newly allocated regmap_fields should be freed by calling
1285*4882a593Smuzhiyun * regmap_field_bulk_free()
1286*4882a593Smuzhiyun */
regmap_field_bulk_alloc(struct regmap * regmap,struct regmap_field ** rm_field,struct reg_field * reg_field,int num_fields)1287*4882a593Smuzhiyun int regmap_field_bulk_alloc(struct regmap *regmap,
1288*4882a593Smuzhiyun struct regmap_field **rm_field,
1289*4882a593Smuzhiyun struct reg_field *reg_field,
1290*4882a593Smuzhiyun int num_fields)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun struct regmap_field *rf;
1293*4882a593Smuzhiyun int i;
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun rf = kcalloc(num_fields, sizeof(*rf), GFP_KERNEL);
1296*4882a593Smuzhiyun if (!rf)
1297*4882a593Smuzhiyun return -ENOMEM;
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun for (i = 0; i < num_fields; i++) {
1300*4882a593Smuzhiyun regmap_field_init(&rf[i], regmap, reg_field[i]);
1301*4882a593Smuzhiyun rm_field[i] = &rf[i];
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun return 0;
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_bulk_alloc);
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun /**
1309*4882a593Smuzhiyun * devm_regmap_field_bulk_alloc() - Allocate and initialise a bulk register
1310*4882a593Smuzhiyun * fields.
1311*4882a593Smuzhiyun *
1312*4882a593Smuzhiyun * @dev: Device that will be interacted with
1313*4882a593Smuzhiyun * @regmap: regmap bank in which this register field is located.
1314*4882a593Smuzhiyun * @rm_field: regmap register fields within the bank.
1315*4882a593Smuzhiyun * @reg_field: Register fields within the bank.
1316*4882a593Smuzhiyun * @num_fields: Number of register fields.
1317*4882a593Smuzhiyun *
1318*4882a593Smuzhiyun * The return value will be an -ENOMEM on error or zero for success.
1319*4882a593Smuzhiyun * Newly allocated regmap_fields will be automatically freed by the
1320*4882a593Smuzhiyun * device management code.
1321*4882a593Smuzhiyun */
devm_regmap_field_bulk_alloc(struct device * dev,struct regmap * regmap,struct regmap_field ** rm_field,struct reg_field * reg_field,int num_fields)1322*4882a593Smuzhiyun int devm_regmap_field_bulk_alloc(struct device *dev,
1323*4882a593Smuzhiyun struct regmap *regmap,
1324*4882a593Smuzhiyun struct regmap_field **rm_field,
1325*4882a593Smuzhiyun struct reg_field *reg_field,
1326*4882a593Smuzhiyun int num_fields)
1327*4882a593Smuzhiyun {
1328*4882a593Smuzhiyun struct regmap_field *rf;
1329*4882a593Smuzhiyun int i;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun rf = devm_kcalloc(dev, num_fields, sizeof(*rf), GFP_KERNEL);
1332*4882a593Smuzhiyun if (!rf)
1333*4882a593Smuzhiyun return -ENOMEM;
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun for (i = 0; i < num_fields; i++) {
1336*4882a593Smuzhiyun regmap_field_init(&rf[i], regmap, reg_field[i]);
1337*4882a593Smuzhiyun rm_field[i] = &rf[i];
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun return 0;
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_alloc);
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun /**
1345*4882a593Smuzhiyun * regmap_field_bulk_free() - Free register field allocated using
1346*4882a593Smuzhiyun * regmap_field_bulk_alloc.
1347*4882a593Smuzhiyun *
1348*4882a593Smuzhiyun * @field: regmap fields which should be freed.
1349*4882a593Smuzhiyun */
regmap_field_bulk_free(struct regmap_field * field)1350*4882a593Smuzhiyun void regmap_field_bulk_free(struct regmap_field *field)
1351*4882a593Smuzhiyun {
1352*4882a593Smuzhiyun kfree(field);
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_bulk_free);
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun /**
1357*4882a593Smuzhiyun * devm_regmap_field_bulk_free() - Free a bulk register field allocated using
1358*4882a593Smuzhiyun * devm_regmap_field_bulk_alloc.
1359*4882a593Smuzhiyun *
1360*4882a593Smuzhiyun * @dev: Device that will be interacted with
1361*4882a593Smuzhiyun * @field: regmap field which should be freed.
1362*4882a593Smuzhiyun *
1363*4882a593Smuzhiyun * Free register field allocated using devm_regmap_field_bulk_alloc(). Usually
1364*4882a593Smuzhiyun * drivers need not call this function, as the memory allocated via devm
1365*4882a593Smuzhiyun * will be freed as per device-driver life-cycle.
1366*4882a593Smuzhiyun */
devm_regmap_field_bulk_free(struct device * dev,struct regmap_field * field)1367*4882a593Smuzhiyun void devm_regmap_field_bulk_free(struct device *dev,
1368*4882a593Smuzhiyun struct regmap_field *field)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun devm_kfree(dev, field);
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_field_bulk_free);
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun /**
1375*4882a593Smuzhiyun * devm_regmap_field_free() - Free a register field allocated using
1376*4882a593Smuzhiyun * devm_regmap_field_alloc.
1377*4882a593Smuzhiyun *
1378*4882a593Smuzhiyun * @dev: Device that will be interacted with
1379*4882a593Smuzhiyun * @field: regmap field which should be freed.
1380*4882a593Smuzhiyun *
1381*4882a593Smuzhiyun * Free register field allocated using devm_regmap_field_alloc(). Usually
1382*4882a593Smuzhiyun * drivers need not call this function, as the memory allocated via devm
1383*4882a593Smuzhiyun * will be freed as per device-driver life-cyle.
1384*4882a593Smuzhiyun */
devm_regmap_field_free(struct device * dev,struct regmap_field * field)1385*4882a593Smuzhiyun void devm_regmap_field_free(struct device *dev,
1386*4882a593Smuzhiyun struct regmap_field *field)
1387*4882a593Smuzhiyun {
1388*4882a593Smuzhiyun devm_kfree(dev, field);
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun /**
1393*4882a593Smuzhiyun * regmap_field_alloc() - Allocate and initialise a register field.
1394*4882a593Smuzhiyun *
1395*4882a593Smuzhiyun * @regmap: regmap bank in which this register field is located.
1396*4882a593Smuzhiyun * @reg_field: Register field with in the bank.
1397*4882a593Smuzhiyun *
1398*4882a593Smuzhiyun * The return value will be an ERR_PTR() on error or a valid pointer
1399*4882a593Smuzhiyun * to a struct regmap_field. The regmap_field should be freed by the
1400*4882a593Smuzhiyun * user once its finished working with it using regmap_field_free().
1401*4882a593Smuzhiyun */
regmap_field_alloc(struct regmap * regmap,struct reg_field reg_field)1402*4882a593Smuzhiyun struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1403*4882a593Smuzhiyun struct reg_field reg_field)
1404*4882a593Smuzhiyun {
1405*4882a593Smuzhiyun struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun if (!rm_field)
1408*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun regmap_field_init(rm_field, regmap, reg_field);
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun return rm_field;
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_alloc);
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun /**
1417*4882a593Smuzhiyun * regmap_field_free() - Free register field allocated using
1418*4882a593Smuzhiyun * regmap_field_alloc.
1419*4882a593Smuzhiyun *
1420*4882a593Smuzhiyun * @field: regmap field which should be freed.
1421*4882a593Smuzhiyun */
regmap_field_free(struct regmap_field * field)1422*4882a593Smuzhiyun void regmap_field_free(struct regmap_field *field)
1423*4882a593Smuzhiyun {
1424*4882a593Smuzhiyun kfree(field);
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_free);
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun /**
1429*4882a593Smuzhiyun * regmap_reinit_cache() - Reinitialise the current register cache
1430*4882a593Smuzhiyun *
1431*4882a593Smuzhiyun * @map: Register map to operate on.
1432*4882a593Smuzhiyun * @config: New configuration. Only the cache data will be used.
1433*4882a593Smuzhiyun *
1434*4882a593Smuzhiyun * Discard any existing register cache for the map and initialize a
1435*4882a593Smuzhiyun * new cache. This can be used to restore the cache to defaults or to
1436*4882a593Smuzhiyun * update the cache configuration to reflect runtime discovery of the
1437*4882a593Smuzhiyun * hardware.
1438*4882a593Smuzhiyun *
1439*4882a593Smuzhiyun * No explicit locking is done here, the user needs to ensure that
1440*4882a593Smuzhiyun * this function will not race with other calls to regmap.
1441*4882a593Smuzhiyun */
regmap_reinit_cache(struct regmap * map,const struct regmap_config * config)1442*4882a593Smuzhiyun int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun int ret;
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun regcache_exit(map);
1447*4882a593Smuzhiyun regmap_debugfs_exit(map);
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyun map->max_register = config->max_register;
1450*4882a593Smuzhiyun map->writeable_reg = config->writeable_reg;
1451*4882a593Smuzhiyun map->readable_reg = config->readable_reg;
1452*4882a593Smuzhiyun map->volatile_reg = config->volatile_reg;
1453*4882a593Smuzhiyun map->precious_reg = config->precious_reg;
1454*4882a593Smuzhiyun map->writeable_noinc_reg = config->writeable_noinc_reg;
1455*4882a593Smuzhiyun map->readable_noinc_reg = config->readable_noinc_reg;
1456*4882a593Smuzhiyun map->cache_type = config->cache_type;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun ret = regmap_set_name(map, config);
1459*4882a593Smuzhiyun if (ret)
1460*4882a593Smuzhiyun return ret;
1461*4882a593Smuzhiyun
1462*4882a593Smuzhiyun regmap_debugfs_init(map);
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun map->cache_bypass = false;
1465*4882a593Smuzhiyun map->cache_only = false;
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun return regcache_init(map, config);
1468*4882a593Smuzhiyun }
1469*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun /**
1472*4882a593Smuzhiyun * regmap_exit() - Free a previously allocated register map
1473*4882a593Smuzhiyun *
1474*4882a593Smuzhiyun * @map: Register map to operate on.
1475*4882a593Smuzhiyun */
regmap_exit(struct regmap * map)1476*4882a593Smuzhiyun void regmap_exit(struct regmap *map)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun struct regmap_async *async;
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun regcache_exit(map);
1481*4882a593Smuzhiyun regmap_debugfs_exit(map);
1482*4882a593Smuzhiyun regmap_range_exit(map);
1483*4882a593Smuzhiyun if (map->bus && map->bus->free_context)
1484*4882a593Smuzhiyun map->bus->free_context(map->bus_context);
1485*4882a593Smuzhiyun kfree(map->work_buf);
1486*4882a593Smuzhiyun while (!list_empty(&map->async_free)) {
1487*4882a593Smuzhiyun async = list_first_entry_or_null(&map->async_free,
1488*4882a593Smuzhiyun struct regmap_async,
1489*4882a593Smuzhiyun list);
1490*4882a593Smuzhiyun list_del(&async->list);
1491*4882a593Smuzhiyun kfree(async->work_buf);
1492*4882a593Smuzhiyun kfree(async);
1493*4882a593Smuzhiyun }
1494*4882a593Smuzhiyun if (map->hwlock)
1495*4882a593Smuzhiyun hwspin_lock_free(map->hwlock);
1496*4882a593Smuzhiyun if (map->lock == regmap_lock_mutex)
1497*4882a593Smuzhiyun mutex_destroy(&map->mutex);
1498*4882a593Smuzhiyun kfree_const(map->name);
1499*4882a593Smuzhiyun kfree(map->patch);
1500*4882a593Smuzhiyun kfree(map);
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_exit);
1503*4882a593Smuzhiyun
dev_get_regmap_match(struct device * dev,void * res,void * data)1504*4882a593Smuzhiyun static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1505*4882a593Smuzhiyun {
1506*4882a593Smuzhiyun struct regmap **r = res;
1507*4882a593Smuzhiyun if (!r || !*r) {
1508*4882a593Smuzhiyun WARN_ON(!r || !*r);
1509*4882a593Smuzhiyun return 0;
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun /* If the user didn't specify a name match any */
1513*4882a593Smuzhiyun if (data)
1514*4882a593Smuzhiyun return !strcmp((*r)->name, data);
1515*4882a593Smuzhiyun else
1516*4882a593Smuzhiyun return 1;
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun
1519*4882a593Smuzhiyun /**
1520*4882a593Smuzhiyun * dev_get_regmap() - Obtain the regmap (if any) for a device
1521*4882a593Smuzhiyun *
1522*4882a593Smuzhiyun * @dev: Device to retrieve the map for
1523*4882a593Smuzhiyun * @name: Optional name for the register map, usually NULL.
1524*4882a593Smuzhiyun *
1525*4882a593Smuzhiyun * Returns the regmap for the device if one is present, or NULL. If
1526*4882a593Smuzhiyun * name is specified then it must match the name specified when
1527*4882a593Smuzhiyun * registering the device, if it is NULL then the first regmap found
1528*4882a593Smuzhiyun * will be used. Devices with multiple register maps are very rare,
1529*4882a593Smuzhiyun * generic code should normally not need to specify a name.
1530*4882a593Smuzhiyun */
dev_get_regmap(struct device * dev,const char * name)1531*4882a593Smuzhiyun struct regmap *dev_get_regmap(struct device *dev, const char *name)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun struct regmap **r = devres_find(dev, dev_get_regmap_release,
1534*4882a593Smuzhiyun dev_get_regmap_match, (void *)name);
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun if (!r)
1537*4882a593Smuzhiyun return NULL;
1538*4882a593Smuzhiyun return *r;
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dev_get_regmap);
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun /**
1543*4882a593Smuzhiyun * regmap_get_device() - Obtain the device from a regmap
1544*4882a593Smuzhiyun *
1545*4882a593Smuzhiyun * @map: Register map to operate on.
1546*4882a593Smuzhiyun *
1547*4882a593Smuzhiyun * Returns the underlying device that the regmap has been created for.
1548*4882a593Smuzhiyun */
regmap_get_device(struct regmap * map)1549*4882a593Smuzhiyun struct device *regmap_get_device(struct regmap *map)
1550*4882a593Smuzhiyun {
1551*4882a593Smuzhiyun return map->dev;
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_device);
1554*4882a593Smuzhiyun
_regmap_select_page(struct regmap * map,unsigned int * reg,struct regmap_range_node * range,unsigned int val_num)1555*4882a593Smuzhiyun static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1556*4882a593Smuzhiyun struct regmap_range_node *range,
1557*4882a593Smuzhiyun unsigned int val_num)
1558*4882a593Smuzhiyun {
1559*4882a593Smuzhiyun void *orig_work_buf;
1560*4882a593Smuzhiyun unsigned int win_offset;
1561*4882a593Smuzhiyun unsigned int win_page;
1562*4882a593Smuzhiyun bool page_chg;
1563*4882a593Smuzhiyun int ret;
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun win_offset = (*reg - range->range_min) % range->window_len;
1566*4882a593Smuzhiyun win_page = (*reg - range->range_min) / range->window_len;
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun if (val_num > 1) {
1569*4882a593Smuzhiyun /* Bulk write shouldn't cross range boundary */
1570*4882a593Smuzhiyun if (*reg + val_num - 1 > range->range_max)
1571*4882a593Smuzhiyun return -EINVAL;
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun /* ... or single page boundary */
1574*4882a593Smuzhiyun if (val_num > range->window_len - win_offset)
1575*4882a593Smuzhiyun return -EINVAL;
1576*4882a593Smuzhiyun }
1577*4882a593Smuzhiyun
1578*4882a593Smuzhiyun /* It is possible to have selector register inside data window.
1579*4882a593Smuzhiyun In that case, selector register is located on every page and
1580*4882a593Smuzhiyun it needs no page switching, when accessed alone. */
1581*4882a593Smuzhiyun if (val_num > 1 ||
1582*4882a593Smuzhiyun range->window_start + win_offset != range->selector_reg) {
1583*4882a593Smuzhiyun /* Use separate work_buf during page switching */
1584*4882a593Smuzhiyun orig_work_buf = map->work_buf;
1585*4882a593Smuzhiyun map->work_buf = map->selector_work_buf;
1586*4882a593Smuzhiyun
1587*4882a593Smuzhiyun ret = _regmap_update_bits(map, range->selector_reg,
1588*4882a593Smuzhiyun range->selector_mask,
1589*4882a593Smuzhiyun win_page << range->selector_shift,
1590*4882a593Smuzhiyun &page_chg, false);
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun map->work_buf = orig_work_buf;
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun if (ret != 0)
1595*4882a593Smuzhiyun return ret;
1596*4882a593Smuzhiyun }
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun *reg = range->window_start + win_offset;
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun return 0;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
regmap_set_work_buf_flag_mask(struct regmap * map,int max_bytes,unsigned long mask)1603*4882a593Smuzhiyun static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
1604*4882a593Smuzhiyun unsigned long mask)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun u8 *buf;
1607*4882a593Smuzhiyun int i;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun if (!mask || !map->work_buf)
1610*4882a593Smuzhiyun return;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun buf = map->work_buf;
1613*4882a593Smuzhiyun
1614*4882a593Smuzhiyun for (i = 0; i < max_bytes; i++)
1615*4882a593Smuzhiyun buf[i] |= (mask >> (8 * i)) & 0xff;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
_regmap_raw_write_impl(struct regmap * map,unsigned int reg,const void * val,size_t val_len,bool noinc)1618*4882a593Smuzhiyun static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
1619*4882a593Smuzhiyun const void *val, size_t val_len, bool noinc)
1620*4882a593Smuzhiyun {
1621*4882a593Smuzhiyun struct regmap_range_node *range;
1622*4882a593Smuzhiyun unsigned long flags;
1623*4882a593Smuzhiyun void *work_val = map->work_buf + map->format.reg_bytes +
1624*4882a593Smuzhiyun map->format.pad_bytes;
1625*4882a593Smuzhiyun void *buf;
1626*4882a593Smuzhiyun int ret = -ENOTSUPP;
1627*4882a593Smuzhiyun size_t len;
1628*4882a593Smuzhiyun int i;
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun WARN_ON(!map->bus);
1631*4882a593Smuzhiyun
1632*4882a593Smuzhiyun /* Check for unwritable or noinc registers in range
1633*4882a593Smuzhiyun * before we start
1634*4882a593Smuzhiyun */
1635*4882a593Smuzhiyun if (!regmap_writeable_noinc(map, reg)) {
1636*4882a593Smuzhiyun for (i = 0; i < val_len / map->format.val_bytes; i++) {
1637*4882a593Smuzhiyun unsigned int element =
1638*4882a593Smuzhiyun reg + regmap_get_offset(map, i);
1639*4882a593Smuzhiyun if (!regmap_writeable(map, element) ||
1640*4882a593Smuzhiyun regmap_writeable_noinc(map, element))
1641*4882a593Smuzhiyun return -EINVAL;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun if (!map->cache_bypass && map->format.parse_val) {
1646*4882a593Smuzhiyun unsigned int ival;
1647*4882a593Smuzhiyun int val_bytes = map->format.val_bytes;
1648*4882a593Smuzhiyun for (i = 0; i < val_len / val_bytes; i++) {
1649*4882a593Smuzhiyun ival = map->format.parse_val(val + (i * val_bytes));
1650*4882a593Smuzhiyun ret = regcache_write(map,
1651*4882a593Smuzhiyun reg + regmap_get_offset(map, i),
1652*4882a593Smuzhiyun ival);
1653*4882a593Smuzhiyun if (ret) {
1654*4882a593Smuzhiyun dev_err(map->dev,
1655*4882a593Smuzhiyun "Error in caching of register: %x ret: %d\n",
1656*4882a593Smuzhiyun reg + regmap_get_offset(map, i), ret);
1657*4882a593Smuzhiyun return ret;
1658*4882a593Smuzhiyun }
1659*4882a593Smuzhiyun }
1660*4882a593Smuzhiyun if (map->cache_only) {
1661*4882a593Smuzhiyun map->cache_dirty = true;
1662*4882a593Smuzhiyun return 0;
1663*4882a593Smuzhiyun }
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun range = _regmap_range_lookup(map, reg);
1667*4882a593Smuzhiyun if (range) {
1668*4882a593Smuzhiyun int val_num = val_len / map->format.val_bytes;
1669*4882a593Smuzhiyun int win_offset = (reg - range->range_min) % range->window_len;
1670*4882a593Smuzhiyun int win_residue = range->window_len - win_offset;
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun /* If the write goes beyond the end of the window split it */
1673*4882a593Smuzhiyun while (val_num > win_residue) {
1674*4882a593Smuzhiyun dev_dbg(map->dev, "Writing window %d/%zu\n",
1675*4882a593Smuzhiyun win_residue, val_len / map->format.val_bytes);
1676*4882a593Smuzhiyun ret = _regmap_raw_write_impl(map, reg, val,
1677*4882a593Smuzhiyun win_residue *
1678*4882a593Smuzhiyun map->format.val_bytes, noinc);
1679*4882a593Smuzhiyun if (ret != 0)
1680*4882a593Smuzhiyun return ret;
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun reg += win_residue;
1683*4882a593Smuzhiyun val_num -= win_residue;
1684*4882a593Smuzhiyun val += win_residue * map->format.val_bytes;
1685*4882a593Smuzhiyun val_len -= win_residue * map->format.val_bytes;
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun win_offset = (reg - range->range_min) %
1688*4882a593Smuzhiyun range->window_len;
1689*4882a593Smuzhiyun win_residue = range->window_len - win_offset;
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun ret = _regmap_select_page(map, ®, range, noinc ? 1 : val_num);
1693*4882a593Smuzhiyun if (ret != 0)
1694*4882a593Smuzhiyun return ret;
1695*4882a593Smuzhiyun }
1696*4882a593Smuzhiyun
1697*4882a593Smuzhiyun map->format.format_reg(map->work_buf, reg, map->reg_shift);
1698*4882a593Smuzhiyun regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
1699*4882a593Smuzhiyun map->write_flag_mask);
1700*4882a593Smuzhiyun
1701*4882a593Smuzhiyun /*
1702*4882a593Smuzhiyun * Essentially all I/O mechanisms will be faster with a single
1703*4882a593Smuzhiyun * buffer to write. Since register syncs often generate raw
1704*4882a593Smuzhiyun * writes of single registers optimise that case.
1705*4882a593Smuzhiyun */
1706*4882a593Smuzhiyun if (val != work_val && val_len == map->format.val_bytes) {
1707*4882a593Smuzhiyun memcpy(work_val, val, map->format.val_bytes);
1708*4882a593Smuzhiyun val = work_val;
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun if (map->async && map->bus->async_write) {
1712*4882a593Smuzhiyun struct regmap_async *async;
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun trace_regmap_async_write_start(map, reg, val_len);
1715*4882a593Smuzhiyun
1716*4882a593Smuzhiyun spin_lock_irqsave(&map->async_lock, flags);
1717*4882a593Smuzhiyun async = list_first_entry_or_null(&map->async_free,
1718*4882a593Smuzhiyun struct regmap_async,
1719*4882a593Smuzhiyun list);
1720*4882a593Smuzhiyun if (async)
1721*4882a593Smuzhiyun list_del(&async->list);
1722*4882a593Smuzhiyun spin_unlock_irqrestore(&map->async_lock, flags);
1723*4882a593Smuzhiyun
1724*4882a593Smuzhiyun if (!async) {
1725*4882a593Smuzhiyun async = map->bus->async_alloc();
1726*4882a593Smuzhiyun if (!async)
1727*4882a593Smuzhiyun return -ENOMEM;
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun async->work_buf = kzalloc(map->format.buf_size,
1730*4882a593Smuzhiyun GFP_KERNEL | GFP_DMA);
1731*4882a593Smuzhiyun if (!async->work_buf) {
1732*4882a593Smuzhiyun kfree(async);
1733*4882a593Smuzhiyun return -ENOMEM;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun }
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun async->map = map;
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun /* If the caller supplied the value we can use it safely. */
1740*4882a593Smuzhiyun memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1741*4882a593Smuzhiyun map->format.reg_bytes + map->format.val_bytes);
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun spin_lock_irqsave(&map->async_lock, flags);
1744*4882a593Smuzhiyun list_add_tail(&async->list, &map->async_list);
1745*4882a593Smuzhiyun spin_unlock_irqrestore(&map->async_lock, flags);
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun if (val != work_val)
1748*4882a593Smuzhiyun ret = map->bus->async_write(map->bus_context,
1749*4882a593Smuzhiyun async->work_buf,
1750*4882a593Smuzhiyun map->format.reg_bytes +
1751*4882a593Smuzhiyun map->format.pad_bytes,
1752*4882a593Smuzhiyun val, val_len, async);
1753*4882a593Smuzhiyun else
1754*4882a593Smuzhiyun ret = map->bus->async_write(map->bus_context,
1755*4882a593Smuzhiyun async->work_buf,
1756*4882a593Smuzhiyun map->format.reg_bytes +
1757*4882a593Smuzhiyun map->format.pad_bytes +
1758*4882a593Smuzhiyun val_len, NULL, 0, async);
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun if (ret != 0) {
1761*4882a593Smuzhiyun dev_err(map->dev, "Failed to schedule write: %d\n",
1762*4882a593Smuzhiyun ret);
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun spin_lock_irqsave(&map->async_lock, flags);
1765*4882a593Smuzhiyun list_move(&async->list, &map->async_free);
1766*4882a593Smuzhiyun spin_unlock_irqrestore(&map->async_lock, flags);
1767*4882a593Smuzhiyun }
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun return ret;
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
1773*4882a593Smuzhiyun
1774*4882a593Smuzhiyun /* If we're doing a single register write we can probably just
1775*4882a593Smuzhiyun * send the work_buf directly, otherwise try to do a gather
1776*4882a593Smuzhiyun * write.
1777*4882a593Smuzhiyun */
1778*4882a593Smuzhiyun if (val == work_val)
1779*4882a593Smuzhiyun ret = map->bus->write(map->bus_context, map->work_buf,
1780*4882a593Smuzhiyun map->format.reg_bytes +
1781*4882a593Smuzhiyun map->format.pad_bytes +
1782*4882a593Smuzhiyun val_len);
1783*4882a593Smuzhiyun else if (map->bus->gather_write)
1784*4882a593Smuzhiyun ret = map->bus->gather_write(map->bus_context, map->work_buf,
1785*4882a593Smuzhiyun map->format.reg_bytes +
1786*4882a593Smuzhiyun map->format.pad_bytes,
1787*4882a593Smuzhiyun val, val_len);
1788*4882a593Smuzhiyun else
1789*4882a593Smuzhiyun ret = -ENOTSUPP;
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun /* If that didn't work fall back on linearising by hand. */
1792*4882a593Smuzhiyun if (ret == -ENOTSUPP) {
1793*4882a593Smuzhiyun len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1794*4882a593Smuzhiyun buf = kzalloc(len, GFP_KERNEL);
1795*4882a593Smuzhiyun if (!buf)
1796*4882a593Smuzhiyun return -ENOMEM;
1797*4882a593Smuzhiyun
1798*4882a593Smuzhiyun memcpy(buf, map->work_buf, map->format.reg_bytes);
1799*4882a593Smuzhiyun memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1800*4882a593Smuzhiyun val, val_len);
1801*4882a593Smuzhiyun ret = map->bus->write(map->bus_context, buf, len);
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun kfree(buf);
1804*4882a593Smuzhiyun } else if (ret != 0 && !map->cache_bypass && map->format.parse_val) {
1805*4882a593Smuzhiyun /* regcache_drop_region() takes lock that we already have,
1806*4882a593Smuzhiyun * thus call map->cache_ops->drop() directly
1807*4882a593Smuzhiyun */
1808*4882a593Smuzhiyun if (map->cache_ops && map->cache_ops->drop)
1809*4882a593Smuzhiyun map->cache_ops->drop(map, reg, reg + 1);
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun return ret;
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun /**
1818*4882a593Smuzhiyun * regmap_can_raw_write - Test if regmap_raw_write() is supported
1819*4882a593Smuzhiyun *
1820*4882a593Smuzhiyun * @map: Map to check.
1821*4882a593Smuzhiyun */
regmap_can_raw_write(struct regmap * map)1822*4882a593Smuzhiyun bool regmap_can_raw_write(struct regmap *map)
1823*4882a593Smuzhiyun {
1824*4882a593Smuzhiyun return map->bus && map->bus->write && map->format.format_val &&
1825*4882a593Smuzhiyun map->format.format_reg;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1828*4882a593Smuzhiyun
1829*4882a593Smuzhiyun /**
1830*4882a593Smuzhiyun * regmap_get_raw_read_max - Get the maximum size we can read
1831*4882a593Smuzhiyun *
1832*4882a593Smuzhiyun * @map: Map to check.
1833*4882a593Smuzhiyun */
regmap_get_raw_read_max(struct regmap * map)1834*4882a593Smuzhiyun size_t regmap_get_raw_read_max(struct regmap *map)
1835*4882a593Smuzhiyun {
1836*4882a593Smuzhiyun return map->max_raw_read;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1839*4882a593Smuzhiyun
1840*4882a593Smuzhiyun /**
1841*4882a593Smuzhiyun * regmap_get_raw_write_max - Get the maximum size we can read
1842*4882a593Smuzhiyun *
1843*4882a593Smuzhiyun * @map: Map to check.
1844*4882a593Smuzhiyun */
regmap_get_raw_write_max(struct regmap * map)1845*4882a593Smuzhiyun size_t regmap_get_raw_write_max(struct regmap *map)
1846*4882a593Smuzhiyun {
1847*4882a593Smuzhiyun return map->max_raw_write;
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1850*4882a593Smuzhiyun
_regmap_bus_formatted_write(void * context,unsigned int reg,unsigned int val)1851*4882a593Smuzhiyun static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1852*4882a593Smuzhiyun unsigned int val)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun int ret;
1855*4882a593Smuzhiyun struct regmap_range_node *range;
1856*4882a593Smuzhiyun struct regmap *map = context;
1857*4882a593Smuzhiyun
1858*4882a593Smuzhiyun WARN_ON(!map->bus || !map->format.format_write);
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun range = _regmap_range_lookup(map, reg);
1861*4882a593Smuzhiyun if (range) {
1862*4882a593Smuzhiyun ret = _regmap_select_page(map, ®, range, 1);
1863*4882a593Smuzhiyun if (ret != 0)
1864*4882a593Smuzhiyun return ret;
1865*4882a593Smuzhiyun }
1866*4882a593Smuzhiyun
1867*4882a593Smuzhiyun map->format.format_write(map, reg, val);
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun trace_regmap_hw_write_start(map, reg, 1);
1870*4882a593Smuzhiyun
1871*4882a593Smuzhiyun ret = map->bus->write(map->bus_context, map->work_buf,
1872*4882a593Smuzhiyun map->format.buf_size);
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun trace_regmap_hw_write_done(map, reg, 1);
1875*4882a593Smuzhiyun
1876*4882a593Smuzhiyun return ret;
1877*4882a593Smuzhiyun }
1878*4882a593Smuzhiyun
_regmap_bus_reg_write(void * context,unsigned int reg,unsigned int val)1879*4882a593Smuzhiyun static int _regmap_bus_reg_write(void *context, unsigned int reg,
1880*4882a593Smuzhiyun unsigned int val)
1881*4882a593Smuzhiyun {
1882*4882a593Smuzhiyun struct regmap *map = context;
1883*4882a593Smuzhiyun
1884*4882a593Smuzhiyun return map->bus->reg_write(map->bus_context, reg, val);
1885*4882a593Smuzhiyun }
1886*4882a593Smuzhiyun
_regmap_bus_raw_write(void * context,unsigned int reg,unsigned int val)1887*4882a593Smuzhiyun static int _regmap_bus_raw_write(void *context, unsigned int reg,
1888*4882a593Smuzhiyun unsigned int val)
1889*4882a593Smuzhiyun {
1890*4882a593Smuzhiyun struct regmap *map = context;
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun WARN_ON(!map->bus || !map->format.format_val);
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun map->format.format_val(map->work_buf + map->format.reg_bytes
1895*4882a593Smuzhiyun + map->format.pad_bytes, val, 0);
1896*4882a593Smuzhiyun return _regmap_raw_write_impl(map, reg,
1897*4882a593Smuzhiyun map->work_buf +
1898*4882a593Smuzhiyun map->format.reg_bytes +
1899*4882a593Smuzhiyun map->format.pad_bytes,
1900*4882a593Smuzhiyun map->format.val_bytes,
1901*4882a593Smuzhiyun false);
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun
_regmap_map_get_context(struct regmap * map)1904*4882a593Smuzhiyun static inline void *_regmap_map_get_context(struct regmap *map)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun return (map->bus) ? map : map->bus_context;
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun
_regmap_write(struct regmap * map,unsigned int reg,unsigned int val)1909*4882a593Smuzhiyun int _regmap_write(struct regmap *map, unsigned int reg,
1910*4882a593Smuzhiyun unsigned int val)
1911*4882a593Smuzhiyun {
1912*4882a593Smuzhiyun int ret;
1913*4882a593Smuzhiyun void *context = _regmap_map_get_context(map);
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun if (!regmap_writeable(map, reg))
1916*4882a593Smuzhiyun return -EIO;
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun if (!map->cache_bypass && !map->defer_caching) {
1919*4882a593Smuzhiyun ret = regcache_write(map, reg, val);
1920*4882a593Smuzhiyun if (ret != 0)
1921*4882a593Smuzhiyun return ret;
1922*4882a593Smuzhiyun if (map->cache_only) {
1923*4882a593Smuzhiyun map->cache_dirty = true;
1924*4882a593Smuzhiyun return 0;
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun if (regmap_should_log(map))
1929*4882a593Smuzhiyun dev_info(map->dev, "%x <= %x\n", reg, val);
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun trace_regmap_reg_write(map, reg, val);
1932*4882a593Smuzhiyun
1933*4882a593Smuzhiyun return map->reg_write(context, reg, val);
1934*4882a593Smuzhiyun }
1935*4882a593Smuzhiyun
1936*4882a593Smuzhiyun /**
1937*4882a593Smuzhiyun * regmap_write() - Write a value to a single register
1938*4882a593Smuzhiyun *
1939*4882a593Smuzhiyun * @map: Register map to write to
1940*4882a593Smuzhiyun * @reg: Register to write to
1941*4882a593Smuzhiyun * @val: Value to be written
1942*4882a593Smuzhiyun *
1943*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
1944*4882a593Smuzhiyun * be returned in error cases.
1945*4882a593Smuzhiyun */
regmap_write(struct regmap * map,unsigned int reg,unsigned int val)1946*4882a593Smuzhiyun int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1947*4882a593Smuzhiyun {
1948*4882a593Smuzhiyun int ret;
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
1951*4882a593Smuzhiyun return -EINVAL;
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun map->lock(map->lock_arg);
1954*4882a593Smuzhiyun
1955*4882a593Smuzhiyun ret = _regmap_write(map, reg, val);
1956*4882a593Smuzhiyun
1957*4882a593Smuzhiyun map->unlock(map->lock_arg);
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun return ret;
1960*4882a593Smuzhiyun }
1961*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_write);
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun /**
1964*4882a593Smuzhiyun * regmap_write_async() - Write a value to a single register asynchronously
1965*4882a593Smuzhiyun *
1966*4882a593Smuzhiyun * @map: Register map to write to
1967*4882a593Smuzhiyun * @reg: Register to write to
1968*4882a593Smuzhiyun * @val: Value to be written
1969*4882a593Smuzhiyun *
1970*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
1971*4882a593Smuzhiyun * be returned in error cases.
1972*4882a593Smuzhiyun */
regmap_write_async(struct regmap * map,unsigned int reg,unsigned int val)1973*4882a593Smuzhiyun int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1974*4882a593Smuzhiyun {
1975*4882a593Smuzhiyun int ret;
1976*4882a593Smuzhiyun
1977*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
1978*4882a593Smuzhiyun return -EINVAL;
1979*4882a593Smuzhiyun
1980*4882a593Smuzhiyun map->lock(map->lock_arg);
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun map->async = true;
1983*4882a593Smuzhiyun
1984*4882a593Smuzhiyun ret = _regmap_write(map, reg, val);
1985*4882a593Smuzhiyun
1986*4882a593Smuzhiyun map->async = false;
1987*4882a593Smuzhiyun
1988*4882a593Smuzhiyun map->unlock(map->lock_arg);
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun return ret;
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_write_async);
1993*4882a593Smuzhiyun
_regmap_raw_write(struct regmap * map,unsigned int reg,const void * val,size_t val_len,bool noinc)1994*4882a593Smuzhiyun int _regmap_raw_write(struct regmap *map, unsigned int reg,
1995*4882a593Smuzhiyun const void *val, size_t val_len, bool noinc)
1996*4882a593Smuzhiyun {
1997*4882a593Smuzhiyun size_t val_bytes = map->format.val_bytes;
1998*4882a593Smuzhiyun size_t val_count = val_len / val_bytes;
1999*4882a593Smuzhiyun size_t chunk_count, chunk_bytes;
2000*4882a593Smuzhiyun size_t chunk_regs = val_count;
2001*4882a593Smuzhiyun int ret, i;
2002*4882a593Smuzhiyun
2003*4882a593Smuzhiyun if (!val_count)
2004*4882a593Smuzhiyun return -EINVAL;
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun if (map->use_single_write)
2007*4882a593Smuzhiyun chunk_regs = 1;
2008*4882a593Smuzhiyun else if (map->max_raw_write && val_len > map->max_raw_write)
2009*4882a593Smuzhiyun chunk_regs = map->max_raw_write / val_bytes;
2010*4882a593Smuzhiyun
2011*4882a593Smuzhiyun chunk_count = val_count / chunk_regs;
2012*4882a593Smuzhiyun chunk_bytes = chunk_regs * val_bytes;
2013*4882a593Smuzhiyun
2014*4882a593Smuzhiyun /* Write as many bytes as possible with chunk_size */
2015*4882a593Smuzhiyun for (i = 0; i < chunk_count; i++) {
2016*4882a593Smuzhiyun ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes, noinc);
2017*4882a593Smuzhiyun if (ret)
2018*4882a593Smuzhiyun return ret;
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun reg += regmap_get_offset(map, chunk_regs);
2021*4882a593Smuzhiyun val += chunk_bytes;
2022*4882a593Smuzhiyun val_len -= chunk_bytes;
2023*4882a593Smuzhiyun }
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun /* Write remaining bytes */
2026*4882a593Smuzhiyun if (val_len)
2027*4882a593Smuzhiyun ret = _regmap_raw_write_impl(map, reg, val, val_len, noinc);
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun return ret;
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun /**
2033*4882a593Smuzhiyun * regmap_raw_write() - Write raw values to one or more registers
2034*4882a593Smuzhiyun *
2035*4882a593Smuzhiyun * @map: Register map to write to
2036*4882a593Smuzhiyun * @reg: Initial register to write to
2037*4882a593Smuzhiyun * @val: Block of data to be written, laid out for direct transmission to the
2038*4882a593Smuzhiyun * device
2039*4882a593Smuzhiyun * @val_len: Length of data pointed to by val.
2040*4882a593Smuzhiyun *
2041*4882a593Smuzhiyun * This function is intended to be used for things like firmware
2042*4882a593Smuzhiyun * download where a large block of data needs to be transferred to the
2043*4882a593Smuzhiyun * device. No formatting will be done on the data provided.
2044*4882a593Smuzhiyun *
2045*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2046*4882a593Smuzhiyun * be returned in error cases.
2047*4882a593Smuzhiyun */
regmap_raw_write(struct regmap * map,unsigned int reg,const void * val,size_t val_len)2048*4882a593Smuzhiyun int regmap_raw_write(struct regmap *map, unsigned int reg,
2049*4882a593Smuzhiyun const void *val, size_t val_len)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun int ret;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun if (!regmap_can_raw_write(map))
2054*4882a593Smuzhiyun return -EINVAL;
2055*4882a593Smuzhiyun if (val_len % map->format.val_bytes)
2056*4882a593Smuzhiyun return -EINVAL;
2057*4882a593Smuzhiyun
2058*4882a593Smuzhiyun map->lock(map->lock_arg);
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun ret = _regmap_raw_write(map, reg, val, val_len, false);
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun map->unlock(map->lock_arg);
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun return ret;
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_raw_write);
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun /**
2069*4882a593Smuzhiyun * regmap_noinc_write(): Write data from a register without incrementing the
2070*4882a593Smuzhiyun * register number
2071*4882a593Smuzhiyun *
2072*4882a593Smuzhiyun * @map: Register map to write to
2073*4882a593Smuzhiyun * @reg: Register to write to
2074*4882a593Smuzhiyun * @val: Pointer to data buffer
2075*4882a593Smuzhiyun * @val_len: Length of output buffer in bytes.
2076*4882a593Smuzhiyun *
2077*4882a593Smuzhiyun * The regmap API usually assumes that bulk bus write operations will write a
2078*4882a593Smuzhiyun * range of registers. Some devices have certain registers for which a write
2079*4882a593Smuzhiyun * operation can write to an internal FIFO.
2080*4882a593Smuzhiyun *
2081*4882a593Smuzhiyun * The target register must be volatile but registers after it can be
2082*4882a593Smuzhiyun * completely unrelated cacheable registers.
2083*4882a593Smuzhiyun *
2084*4882a593Smuzhiyun * This will attempt multiple writes as required to write val_len bytes.
2085*4882a593Smuzhiyun *
2086*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will be
2087*4882a593Smuzhiyun * returned in error cases.
2088*4882a593Smuzhiyun */
regmap_noinc_write(struct regmap * map,unsigned int reg,const void * val,size_t val_len)2089*4882a593Smuzhiyun int regmap_noinc_write(struct regmap *map, unsigned int reg,
2090*4882a593Smuzhiyun const void *val, size_t val_len)
2091*4882a593Smuzhiyun {
2092*4882a593Smuzhiyun size_t write_len;
2093*4882a593Smuzhiyun int ret;
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun if (!map->bus)
2096*4882a593Smuzhiyun return -EINVAL;
2097*4882a593Smuzhiyun if (!map->bus->write)
2098*4882a593Smuzhiyun return -ENOTSUPP;
2099*4882a593Smuzhiyun if (val_len % map->format.val_bytes)
2100*4882a593Smuzhiyun return -EINVAL;
2101*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2102*4882a593Smuzhiyun return -EINVAL;
2103*4882a593Smuzhiyun if (val_len == 0)
2104*4882a593Smuzhiyun return -EINVAL;
2105*4882a593Smuzhiyun
2106*4882a593Smuzhiyun map->lock(map->lock_arg);
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun if (!regmap_volatile(map, reg) || !regmap_writeable_noinc(map, reg)) {
2109*4882a593Smuzhiyun ret = -EINVAL;
2110*4882a593Smuzhiyun goto out_unlock;
2111*4882a593Smuzhiyun }
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun while (val_len) {
2114*4882a593Smuzhiyun if (map->max_raw_write && map->max_raw_write < val_len)
2115*4882a593Smuzhiyun write_len = map->max_raw_write;
2116*4882a593Smuzhiyun else
2117*4882a593Smuzhiyun write_len = val_len;
2118*4882a593Smuzhiyun ret = _regmap_raw_write(map, reg, val, write_len, true);
2119*4882a593Smuzhiyun if (ret)
2120*4882a593Smuzhiyun goto out_unlock;
2121*4882a593Smuzhiyun val = ((u8 *)val) + write_len;
2122*4882a593Smuzhiyun val_len -= write_len;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun out_unlock:
2126*4882a593Smuzhiyun map->unlock(map->lock_arg);
2127*4882a593Smuzhiyun return ret;
2128*4882a593Smuzhiyun }
2129*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_noinc_write);
2130*4882a593Smuzhiyun
2131*4882a593Smuzhiyun /**
2132*4882a593Smuzhiyun * regmap_field_update_bits_base() - Perform a read/modify/write cycle a
2133*4882a593Smuzhiyun * register field.
2134*4882a593Smuzhiyun *
2135*4882a593Smuzhiyun * @field: Register field to write to
2136*4882a593Smuzhiyun * @mask: Bitmask to change
2137*4882a593Smuzhiyun * @val: Value to be written
2138*4882a593Smuzhiyun * @change: Boolean indicating if a write was done
2139*4882a593Smuzhiyun * @async: Boolean indicating asynchronously
2140*4882a593Smuzhiyun * @force: Boolean indicating use force update
2141*4882a593Smuzhiyun *
2142*4882a593Smuzhiyun * Perform a read/modify/write cycle on the register field with change,
2143*4882a593Smuzhiyun * async, force option.
2144*4882a593Smuzhiyun *
2145*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2146*4882a593Smuzhiyun * be returned in error cases.
2147*4882a593Smuzhiyun */
regmap_field_update_bits_base(struct regmap_field * field,unsigned int mask,unsigned int val,bool * change,bool async,bool force)2148*4882a593Smuzhiyun int regmap_field_update_bits_base(struct regmap_field *field,
2149*4882a593Smuzhiyun unsigned int mask, unsigned int val,
2150*4882a593Smuzhiyun bool *change, bool async, bool force)
2151*4882a593Smuzhiyun {
2152*4882a593Smuzhiyun mask = (mask << field->shift) & field->mask;
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun return regmap_update_bits_base(field->regmap, field->reg,
2155*4882a593Smuzhiyun mask, val << field->shift,
2156*4882a593Smuzhiyun change, async, force);
2157*4882a593Smuzhiyun }
2158*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_update_bits_base);
2159*4882a593Smuzhiyun
2160*4882a593Smuzhiyun /**
2161*4882a593Smuzhiyun * regmap_fields_update_bits_base() - Perform a read/modify/write cycle a
2162*4882a593Smuzhiyun * register field with port ID
2163*4882a593Smuzhiyun *
2164*4882a593Smuzhiyun * @field: Register field to write to
2165*4882a593Smuzhiyun * @id: port ID
2166*4882a593Smuzhiyun * @mask: Bitmask to change
2167*4882a593Smuzhiyun * @val: Value to be written
2168*4882a593Smuzhiyun * @change: Boolean indicating if a write was done
2169*4882a593Smuzhiyun * @async: Boolean indicating asynchronously
2170*4882a593Smuzhiyun * @force: Boolean indicating use force update
2171*4882a593Smuzhiyun *
2172*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2173*4882a593Smuzhiyun * be returned in error cases.
2174*4882a593Smuzhiyun */
regmap_fields_update_bits_base(struct regmap_field * field,unsigned int id,unsigned int mask,unsigned int val,bool * change,bool async,bool force)2175*4882a593Smuzhiyun int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
2176*4882a593Smuzhiyun unsigned int mask, unsigned int val,
2177*4882a593Smuzhiyun bool *change, bool async, bool force)
2178*4882a593Smuzhiyun {
2179*4882a593Smuzhiyun if (id >= field->id_size)
2180*4882a593Smuzhiyun return -EINVAL;
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun mask = (mask << field->shift) & field->mask;
2183*4882a593Smuzhiyun
2184*4882a593Smuzhiyun return regmap_update_bits_base(field->regmap,
2185*4882a593Smuzhiyun field->reg + (field->id_offset * id),
2186*4882a593Smuzhiyun mask, val << field->shift,
2187*4882a593Smuzhiyun change, async, force);
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_fields_update_bits_base);
2190*4882a593Smuzhiyun
2191*4882a593Smuzhiyun /**
2192*4882a593Smuzhiyun * regmap_bulk_write() - Write multiple registers to the device
2193*4882a593Smuzhiyun *
2194*4882a593Smuzhiyun * @map: Register map to write to
2195*4882a593Smuzhiyun * @reg: First register to be write from
2196*4882a593Smuzhiyun * @val: Block of data to be written, in native register size for device
2197*4882a593Smuzhiyun * @val_count: Number of registers to write
2198*4882a593Smuzhiyun *
2199*4882a593Smuzhiyun * This function is intended to be used for writing a large block of
2200*4882a593Smuzhiyun * data to the device either in single transfer or multiple transfer.
2201*4882a593Smuzhiyun *
2202*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2203*4882a593Smuzhiyun * be returned in error cases.
2204*4882a593Smuzhiyun */
regmap_bulk_write(struct regmap * map,unsigned int reg,const void * val,size_t val_count)2205*4882a593Smuzhiyun int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
2206*4882a593Smuzhiyun size_t val_count)
2207*4882a593Smuzhiyun {
2208*4882a593Smuzhiyun int ret = 0, i;
2209*4882a593Smuzhiyun size_t val_bytes = map->format.val_bytes;
2210*4882a593Smuzhiyun
2211*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2212*4882a593Smuzhiyun return -EINVAL;
2213*4882a593Smuzhiyun
2214*4882a593Smuzhiyun /*
2215*4882a593Smuzhiyun * Some devices don't support bulk write, for them we have a series of
2216*4882a593Smuzhiyun * single write operations.
2217*4882a593Smuzhiyun */
2218*4882a593Smuzhiyun if (!map->bus || !map->format.parse_inplace) {
2219*4882a593Smuzhiyun map->lock(map->lock_arg);
2220*4882a593Smuzhiyun for (i = 0; i < val_count; i++) {
2221*4882a593Smuzhiyun unsigned int ival;
2222*4882a593Smuzhiyun
2223*4882a593Smuzhiyun switch (val_bytes) {
2224*4882a593Smuzhiyun case 1:
2225*4882a593Smuzhiyun ival = *(u8 *)(val + (i * val_bytes));
2226*4882a593Smuzhiyun break;
2227*4882a593Smuzhiyun case 2:
2228*4882a593Smuzhiyun ival = *(u16 *)(val + (i * val_bytes));
2229*4882a593Smuzhiyun break;
2230*4882a593Smuzhiyun case 4:
2231*4882a593Smuzhiyun ival = *(u32 *)(val + (i * val_bytes));
2232*4882a593Smuzhiyun break;
2233*4882a593Smuzhiyun #ifdef CONFIG_64BIT
2234*4882a593Smuzhiyun case 8:
2235*4882a593Smuzhiyun ival = *(u64 *)(val + (i * val_bytes));
2236*4882a593Smuzhiyun break;
2237*4882a593Smuzhiyun #endif
2238*4882a593Smuzhiyun default:
2239*4882a593Smuzhiyun ret = -EINVAL;
2240*4882a593Smuzhiyun goto out;
2241*4882a593Smuzhiyun }
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun ret = _regmap_write(map,
2244*4882a593Smuzhiyun reg + regmap_get_offset(map, i),
2245*4882a593Smuzhiyun ival);
2246*4882a593Smuzhiyun if (ret != 0)
2247*4882a593Smuzhiyun goto out;
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun out:
2250*4882a593Smuzhiyun map->unlock(map->lock_arg);
2251*4882a593Smuzhiyun } else {
2252*4882a593Smuzhiyun void *wval;
2253*4882a593Smuzhiyun
2254*4882a593Smuzhiyun wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2255*4882a593Smuzhiyun if (!wval)
2256*4882a593Smuzhiyun return -ENOMEM;
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun for (i = 0; i < val_count * val_bytes; i += val_bytes)
2259*4882a593Smuzhiyun map->format.parse_inplace(wval + i);
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun kfree(wval);
2264*4882a593Smuzhiyun }
2265*4882a593Smuzhiyun return ret;
2266*4882a593Smuzhiyun }
2267*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_bulk_write);
2268*4882a593Smuzhiyun
2269*4882a593Smuzhiyun /*
2270*4882a593Smuzhiyun * _regmap_raw_multi_reg_write()
2271*4882a593Smuzhiyun *
2272*4882a593Smuzhiyun * the (register,newvalue) pairs in regs have not been formatted, but
2273*4882a593Smuzhiyun * they are all in the same page and have been changed to being page
2274*4882a593Smuzhiyun * relative. The page register has been written if that was necessary.
2275*4882a593Smuzhiyun */
_regmap_raw_multi_reg_write(struct regmap * map,const struct reg_sequence * regs,size_t num_regs)2276*4882a593Smuzhiyun static int _regmap_raw_multi_reg_write(struct regmap *map,
2277*4882a593Smuzhiyun const struct reg_sequence *regs,
2278*4882a593Smuzhiyun size_t num_regs)
2279*4882a593Smuzhiyun {
2280*4882a593Smuzhiyun int ret;
2281*4882a593Smuzhiyun void *buf;
2282*4882a593Smuzhiyun int i;
2283*4882a593Smuzhiyun u8 *u8;
2284*4882a593Smuzhiyun size_t val_bytes = map->format.val_bytes;
2285*4882a593Smuzhiyun size_t reg_bytes = map->format.reg_bytes;
2286*4882a593Smuzhiyun size_t pad_bytes = map->format.pad_bytes;
2287*4882a593Smuzhiyun size_t pair_size = reg_bytes + pad_bytes + val_bytes;
2288*4882a593Smuzhiyun size_t len = pair_size * num_regs;
2289*4882a593Smuzhiyun
2290*4882a593Smuzhiyun if (!len)
2291*4882a593Smuzhiyun return -EINVAL;
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun buf = kzalloc(len, GFP_KERNEL);
2294*4882a593Smuzhiyun if (!buf)
2295*4882a593Smuzhiyun return -ENOMEM;
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun /* We have to linearise by hand. */
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun u8 = buf;
2300*4882a593Smuzhiyun
2301*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2302*4882a593Smuzhiyun unsigned int reg = regs[i].reg;
2303*4882a593Smuzhiyun unsigned int val = regs[i].def;
2304*4882a593Smuzhiyun trace_regmap_hw_write_start(map, reg, 1);
2305*4882a593Smuzhiyun map->format.format_reg(u8, reg, map->reg_shift);
2306*4882a593Smuzhiyun u8 += reg_bytes + pad_bytes;
2307*4882a593Smuzhiyun map->format.format_val(u8, val, 0);
2308*4882a593Smuzhiyun u8 += val_bytes;
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun u8 = buf;
2311*4882a593Smuzhiyun *u8 |= map->write_flag_mask;
2312*4882a593Smuzhiyun
2313*4882a593Smuzhiyun ret = map->bus->write(map->bus_context, buf, len);
2314*4882a593Smuzhiyun
2315*4882a593Smuzhiyun kfree(buf);
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2318*4882a593Smuzhiyun int reg = regs[i].reg;
2319*4882a593Smuzhiyun trace_regmap_hw_write_done(map, reg, 1);
2320*4882a593Smuzhiyun }
2321*4882a593Smuzhiyun return ret;
2322*4882a593Smuzhiyun }
2323*4882a593Smuzhiyun
_regmap_register_page(struct regmap * map,unsigned int reg,struct regmap_range_node * range)2324*4882a593Smuzhiyun static unsigned int _regmap_register_page(struct regmap *map,
2325*4882a593Smuzhiyun unsigned int reg,
2326*4882a593Smuzhiyun struct regmap_range_node *range)
2327*4882a593Smuzhiyun {
2328*4882a593Smuzhiyun unsigned int win_page = (reg - range->range_min) / range->window_len;
2329*4882a593Smuzhiyun
2330*4882a593Smuzhiyun return win_page;
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun
_regmap_range_multi_paged_reg_write(struct regmap * map,struct reg_sequence * regs,size_t num_regs)2333*4882a593Smuzhiyun static int _regmap_range_multi_paged_reg_write(struct regmap *map,
2334*4882a593Smuzhiyun struct reg_sequence *regs,
2335*4882a593Smuzhiyun size_t num_regs)
2336*4882a593Smuzhiyun {
2337*4882a593Smuzhiyun int ret;
2338*4882a593Smuzhiyun int i, n;
2339*4882a593Smuzhiyun struct reg_sequence *base;
2340*4882a593Smuzhiyun unsigned int this_page = 0;
2341*4882a593Smuzhiyun unsigned int page_change = 0;
2342*4882a593Smuzhiyun /*
2343*4882a593Smuzhiyun * the set of registers are not neccessarily in order, but
2344*4882a593Smuzhiyun * since the order of write must be preserved this algorithm
2345*4882a593Smuzhiyun * chops the set each time the page changes. This also applies
2346*4882a593Smuzhiyun * if there is a delay required at any point in the sequence.
2347*4882a593Smuzhiyun */
2348*4882a593Smuzhiyun base = regs;
2349*4882a593Smuzhiyun for (i = 0, n = 0; i < num_regs; i++, n++) {
2350*4882a593Smuzhiyun unsigned int reg = regs[i].reg;
2351*4882a593Smuzhiyun struct regmap_range_node *range;
2352*4882a593Smuzhiyun
2353*4882a593Smuzhiyun range = _regmap_range_lookup(map, reg);
2354*4882a593Smuzhiyun if (range) {
2355*4882a593Smuzhiyun unsigned int win_page = _regmap_register_page(map, reg,
2356*4882a593Smuzhiyun range);
2357*4882a593Smuzhiyun
2358*4882a593Smuzhiyun if (i == 0)
2359*4882a593Smuzhiyun this_page = win_page;
2360*4882a593Smuzhiyun if (win_page != this_page) {
2361*4882a593Smuzhiyun this_page = win_page;
2362*4882a593Smuzhiyun page_change = 1;
2363*4882a593Smuzhiyun }
2364*4882a593Smuzhiyun }
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun /* If we have both a page change and a delay make sure to
2367*4882a593Smuzhiyun * write the regs and apply the delay before we change the
2368*4882a593Smuzhiyun * page.
2369*4882a593Smuzhiyun */
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun if (page_change || regs[i].delay_us) {
2372*4882a593Smuzhiyun
2373*4882a593Smuzhiyun /* For situations where the first write requires
2374*4882a593Smuzhiyun * a delay we need to make sure we don't call
2375*4882a593Smuzhiyun * raw_multi_reg_write with n=0
2376*4882a593Smuzhiyun * This can't occur with page breaks as we
2377*4882a593Smuzhiyun * never write on the first iteration
2378*4882a593Smuzhiyun */
2379*4882a593Smuzhiyun if (regs[i].delay_us && i == 0)
2380*4882a593Smuzhiyun n = 1;
2381*4882a593Smuzhiyun
2382*4882a593Smuzhiyun ret = _regmap_raw_multi_reg_write(map, base, n);
2383*4882a593Smuzhiyun if (ret != 0)
2384*4882a593Smuzhiyun return ret;
2385*4882a593Smuzhiyun
2386*4882a593Smuzhiyun if (regs[i].delay_us) {
2387*4882a593Smuzhiyun if (map->can_sleep)
2388*4882a593Smuzhiyun fsleep(regs[i].delay_us);
2389*4882a593Smuzhiyun else
2390*4882a593Smuzhiyun udelay(regs[i].delay_us);
2391*4882a593Smuzhiyun }
2392*4882a593Smuzhiyun
2393*4882a593Smuzhiyun base += n;
2394*4882a593Smuzhiyun n = 0;
2395*4882a593Smuzhiyun
2396*4882a593Smuzhiyun if (page_change) {
2397*4882a593Smuzhiyun ret = _regmap_select_page(map,
2398*4882a593Smuzhiyun &base[n].reg,
2399*4882a593Smuzhiyun range, 1);
2400*4882a593Smuzhiyun if (ret != 0)
2401*4882a593Smuzhiyun return ret;
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun page_change = 0;
2404*4882a593Smuzhiyun }
2405*4882a593Smuzhiyun
2406*4882a593Smuzhiyun }
2407*4882a593Smuzhiyun
2408*4882a593Smuzhiyun }
2409*4882a593Smuzhiyun if (n > 0)
2410*4882a593Smuzhiyun return _regmap_raw_multi_reg_write(map, base, n);
2411*4882a593Smuzhiyun return 0;
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun
_regmap_multi_reg_write(struct regmap * map,const struct reg_sequence * regs,size_t num_regs)2414*4882a593Smuzhiyun static int _regmap_multi_reg_write(struct regmap *map,
2415*4882a593Smuzhiyun const struct reg_sequence *regs,
2416*4882a593Smuzhiyun size_t num_regs)
2417*4882a593Smuzhiyun {
2418*4882a593Smuzhiyun int i;
2419*4882a593Smuzhiyun int ret;
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun if (!map->can_multi_write) {
2422*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2423*4882a593Smuzhiyun ret = _regmap_write(map, regs[i].reg, regs[i].def);
2424*4882a593Smuzhiyun if (ret != 0)
2425*4882a593Smuzhiyun return ret;
2426*4882a593Smuzhiyun
2427*4882a593Smuzhiyun if (regs[i].delay_us) {
2428*4882a593Smuzhiyun if (map->can_sleep)
2429*4882a593Smuzhiyun fsleep(regs[i].delay_us);
2430*4882a593Smuzhiyun else
2431*4882a593Smuzhiyun udelay(regs[i].delay_us);
2432*4882a593Smuzhiyun }
2433*4882a593Smuzhiyun }
2434*4882a593Smuzhiyun return 0;
2435*4882a593Smuzhiyun }
2436*4882a593Smuzhiyun
2437*4882a593Smuzhiyun if (!map->format.parse_inplace)
2438*4882a593Smuzhiyun return -EINVAL;
2439*4882a593Smuzhiyun
2440*4882a593Smuzhiyun if (map->writeable_reg)
2441*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2442*4882a593Smuzhiyun int reg = regs[i].reg;
2443*4882a593Smuzhiyun if (!map->writeable_reg(map->dev, reg))
2444*4882a593Smuzhiyun return -EINVAL;
2445*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2446*4882a593Smuzhiyun return -EINVAL;
2447*4882a593Smuzhiyun }
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun if (!map->cache_bypass) {
2450*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2451*4882a593Smuzhiyun unsigned int val = regs[i].def;
2452*4882a593Smuzhiyun unsigned int reg = regs[i].reg;
2453*4882a593Smuzhiyun ret = regcache_write(map, reg, val);
2454*4882a593Smuzhiyun if (ret) {
2455*4882a593Smuzhiyun dev_err(map->dev,
2456*4882a593Smuzhiyun "Error in caching of register: %x ret: %d\n",
2457*4882a593Smuzhiyun reg, ret);
2458*4882a593Smuzhiyun return ret;
2459*4882a593Smuzhiyun }
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun if (map->cache_only) {
2462*4882a593Smuzhiyun map->cache_dirty = true;
2463*4882a593Smuzhiyun return 0;
2464*4882a593Smuzhiyun }
2465*4882a593Smuzhiyun }
2466*4882a593Smuzhiyun
2467*4882a593Smuzhiyun WARN_ON(!map->bus);
2468*4882a593Smuzhiyun
2469*4882a593Smuzhiyun for (i = 0; i < num_regs; i++) {
2470*4882a593Smuzhiyun unsigned int reg = regs[i].reg;
2471*4882a593Smuzhiyun struct regmap_range_node *range;
2472*4882a593Smuzhiyun
2473*4882a593Smuzhiyun /* Coalesce all the writes between a page break or a delay
2474*4882a593Smuzhiyun * in a sequence
2475*4882a593Smuzhiyun */
2476*4882a593Smuzhiyun range = _regmap_range_lookup(map, reg);
2477*4882a593Smuzhiyun if (range || regs[i].delay_us) {
2478*4882a593Smuzhiyun size_t len = sizeof(struct reg_sequence)*num_regs;
2479*4882a593Smuzhiyun struct reg_sequence *base = kmemdup(regs, len,
2480*4882a593Smuzhiyun GFP_KERNEL);
2481*4882a593Smuzhiyun if (!base)
2482*4882a593Smuzhiyun return -ENOMEM;
2483*4882a593Smuzhiyun ret = _regmap_range_multi_paged_reg_write(map, base,
2484*4882a593Smuzhiyun num_regs);
2485*4882a593Smuzhiyun kfree(base);
2486*4882a593Smuzhiyun
2487*4882a593Smuzhiyun return ret;
2488*4882a593Smuzhiyun }
2489*4882a593Smuzhiyun }
2490*4882a593Smuzhiyun return _regmap_raw_multi_reg_write(map, regs, num_regs);
2491*4882a593Smuzhiyun }
2492*4882a593Smuzhiyun
2493*4882a593Smuzhiyun /**
2494*4882a593Smuzhiyun * regmap_multi_reg_write() - Write multiple registers to the device
2495*4882a593Smuzhiyun *
2496*4882a593Smuzhiyun * @map: Register map to write to
2497*4882a593Smuzhiyun * @regs: Array of structures containing register,value to be written
2498*4882a593Smuzhiyun * @num_regs: Number of registers to write
2499*4882a593Smuzhiyun *
2500*4882a593Smuzhiyun * Write multiple registers to the device where the set of register, value
2501*4882a593Smuzhiyun * pairs are supplied in any order, possibly not all in a single range.
2502*4882a593Smuzhiyun *
2503*4882a593Smuzhiyun * The 'normal' block write mode will send ultimately send data on the
2504*4882a593Smuzhiyun * target bus as R,V1,V2,V3,..,Vn where successively higher registers are
2505*4882a593Smuzhiyun * addressed. However, this alternative block multi write mode will send
2506*4882a593Smuzhiyun * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2507*4882a593Smuzhiyun * must of course support the mode.
2508*4882a593Smuzhiyun *
2509*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will be
2510*4882a593Smuzhiyun * returned in error cases.
2511*4882a593Smuzhiyun */
regmap_multi_reg_write(struct regmap * map,const struct reg_sequence * regs,int num_regs)2512*4882a593Smuzhiyun int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
2513*4882a593Smuzhiyun int num_regs)
2514*4882a593Smuzhiyun {
2515*4882a593Smuzhiyun int ret;
2516*4882a593Smuzhiyun
2517*4882a593Smuzhiyun map->lock(map->lock_arg);
2518*4882a593Smuzhiyun
2519*4882a593Smuzhiyun ret = _regmap_multi_reg_write(map, regs, num_regs);
2520*4882a593Smuzhiyun
2521*4882a593Smuzhiyun map->unlock(map->lock_arg);
2522*4882a593Smuzhiyun
2523*4882a593Smuzhiyun return ret;
2524*4882a593Smuzhiyun }
2525*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2526*4882a593Smuzhiyun
2527*4882a593Smuzhiyun /**
2528*4882a593Smuzhiyun * regmap_multi_reg_write_bypassed() - Write multiple registers to the
2529*4882a593Smuzhiyun * device but not the cache
2530*4882a593Smuzhiyun *
2531*4882a593Smuzhiyun * @map: Register map to write to
2532*4882a593Smuzhiyun * @regs: Array of structures containing register,value to be written
2533*4882a593Smuzhiyun * @num_regs: Number of registers to write
2534*4882a593Smuzhiyun *
2535*4882a593Smuzhiyun * Write multiple registers to the device but not the cache where the set
2536*4882a593Smuzhiyun * of register are supplied in any order.
2537*4882a593Smuzhiyun *
2538*4882a593Smuzhiyun * This function is intended to be used for writing a large block of data
2539*4882a593Smuzhiyun * atomically to the device in single transfer for those I2C client devices
2540*4882a593Smuzhiyun * that implement this alternative block write mode.
2541*4882a593Smuzhiyun *
2542*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2543*4882a593Smuzhiyun * be returned in error cases.
2544*4882a593Smuzhiyun */
regmap_multi_reg_write_bypassed(struct regmap * map,const struct reg_sequence * regs,int num_regs)2545*4882a593Smuzhiyun int regmap_multi_reg_write_bypassed(struct regmap *map,
2546*4882a593Smuzhiyun const struct reg_sequence *regs,
2547*4882a593Smuzhiyun int num_regs)
2548*4882a593Smuzhiyun {
2549*4882a593Smuzhiyun int ret;
2550*4882a593Smuzhiyun bool bypass;
2551*4882a593Smuzhiyun
2552*4882a593Smuzhiyun map->lock(map->lock_arg);
2553*4882a593Smuzhiyun
2554*4882a593Smuzhiyun bypass = map->cache_bypass;
2555*4882a593Smuzhiyun map->cache_bypass = true;
2556*4882a593Smuzhiyun
2557*4882a593Smuzhiyun ret = _regmap_multi_reg_write(map, regs, num_regs);
2558*4882a593Smuzhiyun
2559*4882a593Smuzhiyun map->cache_bypass = bypass;
2560*4882a593Smuzhiyun
2561*4882a593Smuzhiyun map->unlock(map->lock_arg);
2562*4882a593Smuzhiyun
2563*4882a593Smuzhiyun return ret;
2564*4882a593Smuzhiyun }
2565*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
2566*4882a593Smuzhiyun
2567*4882a593Smuzhiyun /**
2568*4882a593Smuzhiyun * regmap_raw_write_async() - Write raw values to one or more registers
2569*4882a593Smuzhiyun * asynchronously
2570*4882a593Smuzhiyun *
2571*4882a593Smuzhiyun * @map: Register map to write to
2572*4882a593Smuzhiyun * @reg: Initial register to write to
2573*4882a593Smuzhiyun * @val: Block of data to be written, laid out for direct transmission to the
2574*4882a593Smuzhiyun * device. Must be valid until regmap_async_complete() is called.
2575*4882a593Smuzhiyun * @val_len: Length of data pointed to by val.
2576*4882a593Smuzhiyun *
2577*4882a593Smuzhiyun * This function is intended to be used for things like firmware
2578*4882a593Smuzhiyun * download where a large block of data needs to be transferred to the
2579*4882a593Smuzhiyun * device. No formatting will be done on the data provided.
2580*4882a593Smuzhiyun *
2581*4882a593Smuzhiyun * If supported by the underlying bus the write will be scheduled
2582*4882a593Smuzhiyun * asynchronously, helping maximise I/O speed on higher speed buses
2583*4882a593Smuzhiyun * like SPI. regmap_async_complete() can be called to ensure that all
2584*4882a593Smuzhiyun * asynchrnous writes have been completed.
2585*4882a593Smuzhiyun *
2586*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2587*4882a593Smuzhiyun * be returned in error cases.
2588*4882a593Smuzhiyun */
regmap_raw_write_async(struct regmap * map,unsigned int reg,const void * val,size_t val_len)2589*4882a593Smuzhiyun int regmap_raw_write_async(struct regmap *map, unsigned int reg,
2590*4882a593Smuzhiyun const void *val, size_t val_len)
2591*4882a593Smuzhiyun {
2592*4882a593Smuzhiyun int ret;
2593*4882a593Smuzhiyun
2594*4882a593Smuzhiyun if (val_len % map->format.val_bytes)
2595*4882a593Smuzhiyun return -EINVAL;
2596*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2597*4882a593Smuzhiyun return -EINVAL;
2598*4882a593Smuzhiyun
2599*4882a593Smuzhiyun map->lock(map->lock_arg);
2600*4882a593Smuzhiyun
2601*4882a593Smuzhiyun map->async = true;
2602*4882a593Smuzhiyun
2603*4882a593Smuzhiyun ret = _regmap_raw_write(map, reg, val, val_len, false);
2604*4882a593Smuzhiyun
2605*4882a593Smuzhiyun map->async = false;
2606*4882a593Smuzhiyun
2607*4882a593Smuzhiyun map->unlock(map->lock_arg);
2608*4882a593Smuzhiyun
2609*4882a593Smuzhiyun return ret;
2610*4882a593Smuzhiyun }
2611*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2612*4882a593Smuzhiyun
_regmap_raw_read(struct regmap * map,unsigned int reg,void * val,unsigned int val_len,bool noinc)2613*4882a593Smuzhiyun static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2614*4882a593Smuzhiyun unsigned int val_len, bool noinc)
2615*4882a593Smuzhiyun {
2616*4882a593Smuzhiyun struct regmap_range_node *range;
2617*4882a593Smuzhiyun int ret;
2618*4882a593Smuzhiyun
2619*4882a593Smuzhiyun WARN_ON(!map->bus);
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun if (!map->bus || !map->bus->read)
2622*4882a593Smuzhiyun return -EINVAL;
2623*4882a593Smuzhiyun
2624*4882a593Smuzhiyun range = _regmap_range_lookup(map, reg);
2625*4882a593Smuzhiyun if (range) {
2626*4882a593Smuzhiyun ret = _regmap_select_page(map, ®, range,
2627*4882a593Smuzhiyun noinc ? 1 : val_len / map->format.val_bytes);
2628*4882a593Smuzhiyun if (ret != 0)
2629*4882a593Smuzhiyun return ret;
2630*4882a593Smuzhiyun }
2631*4882a593Smuzhiyun
2632*4882a593Smuzhiyun map->format.format_reg(map->work_buf, reg, map->reg_shift);
2633*4882a593Smuzhiyun regmap_set_work_buf_flag_mask(map, map->format.reg_bytes,
2634*4882a593Smuzhiyun map->read_flag_mask);
2635*4882a593Smuzhiyun trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
2636*4882a593Smuzhiyun
2637*4882a593Smuzhiyun ret = map->bus->read(map->bus_context, map->work_buf,
2638*4882a593Smuzhiyun map->format.reg_bytes + map->format.pad_bytes,
2639*4882a593Smuzhiyun val, val_len);
2640*4882a593Smuzhiyun
2641*4882a593Smuzhiyun trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
2642*4882a593Smuzhiyun
2643*4882a593Smuzhiyun return ret;
2644*4882a593Smuzhiyun }
2645*4882a593Smuzhiyun
_regmap_bus_reg_read(void * context,unsigned int reg,unsigned int * val)2646*4882a593Smuzhiyun static int _regmap_bus_reg_read(void *context, unsigned int reg,
2647*4882a593Smuzhiyun unsigned int *val)
2648*4882a593Smuzhiyun {
2649*4882a593Smuzhiyun struct regmap *map = context;
2650*4882a593Smuzhiyun
2651*4882a593Smuzhiyun return map->bus->reg_read(map->bus_context, reg, val);
2652*4882a593Smuzhiyun }
2653*4882a593Smuzhiyun
_regmap_bus_read(void * context,unsigned int reg,unsigned int * val)2654*4882a593Smuzhiyun static int _regmap_bus_read(void *context, unsigned int reg,
2655*4882a593Smuzhiyun unsigned int *val)
2656*4882a593Smuzhiyun {
2657*4882a593Smuzhiyun int ret;
2658*4882a593Smuzhiyun struct regmap *map = context;
2659*4882a593Smuzhiyun void *work_val = map->work_buf + map->format.reg_bytes +
2660*4882a593Smuzhiyun map->format.pad_bytes;
2661*4882a593Smuzhiyun
2662*4882a593Smuzhiyun if (!map->format.parse_val)
2663*4882a593Smuzhiyun return -EINVAL;
2664*4882a593Smuzhiyun
2665*4882a593Smuzhiyun ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
2666*4882a593Smuzhiyun if (ret == 0)
2667*4882a593Smuzhiyun *val = map->format.parse_val(work_val);
2668*4882a593Smuzhiyun
2669*4882a593Smuzhiyun return ret;
2670*4882a593Smuzhiyun }
2671*4882a593Smuzhiyun
_regmap_read(struct regmap * map,unsigned int reg,unsigned int * val)2672*4882a593Smuzhiyun static int _regmap_read(struct regmap *map, unsigned int reg,
2673*4882a593Smuzhiyun unsigned int *val)
2674*4882a593Smuzhiyun {
2675*4882a593Smuzhiyun int ret;
2676*4882a593Smuzhiyun void *context = _regmap_map_get_context(map);
2677*4882a593Smuzhiyun
2678*4882a593Smuzhiyun if (!map->cache_bypass) {
2679*4882a593Smuzhiyun ret = regcache_read(map, reg, val);
2680*4882a593Smuzhiyun if (ret == 0)
2681*4882a593Smuzhiyun return 0;
2682*4882a593Smuzhiyun }
2683*4882a593Smuzhiyun
2684*4882a593Smuzhiyun if (map->cache_only)
2685*4882a593Smuzhiyun return -EBUSY;
2686*4882a593Smuzhiyun
2687*4882a593Smuzhiyun if (!regmap_readable(map, reg))
2688*4882a593Smuzhiyun return -EIO;
2689*4882a593Smuzhiyun
2690*4882a593Smuzhiyun ret = map->reg_read(context, reg, val);
2691*4882a593Smuzhiyun if (ret == 0) {
2692*4882a593Smuzhiyun if (regmap_should_log(map))
2693*4882a593Smuzhiyun dev_info(map->dev, "%x => %x\n", reg, *val);
2694*4882a593Smuzhiyun
2695*4882a593Smuzhiyun trace_regmap_reg_read(map, reg, *val);
2696*4882a593Smuzhiyun
2697*4882a593Smuzhiyun if (!map->cache_bypass)
2698*4882a593Smuzhiyun regcache_write(map, reg, *val);
2699*4882a593Smuzhiyun }
2700*4882a593Smuzhiyun
2701*4882a593Smuzhiyun return ret;
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun
2704*4882a593Smuzhiyun /**
2705*4882a593Smuzhiyun * regmap_read() - Read a value from a single register
2706*4882a593Smuzhiyun *
2707*4882a593Smuzhiyun * @map: Register map to read from
2708*4882a593Smuzhiyun * @reg: Register to be read from
2709*4882a593Smuzhiyun * @val: Pointer to store read value
2710*4882a593Smuzhiyun *
2711*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2712*4882a593Smuzhiyun * be returned in error cases.
2713*4882a593Smuzhiyun */
regmap_read(struct regmap * map,unsigned int reg,unsigned int * val)2714*4882a593Smuzhiyun int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2715*4882a593Smuzhiyun {
2716*4882a593Smuzhiyun int ret;
2717*4882a593Smuzhiyun
2718*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2719*4882a593Smuzhiyun return -EINVAL;
2720*4882a593Smuzhiyun
2721*4882a593Smuzhiyun map->lock(map->lock_arg);
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun ret = _regmap_read(map, reg, val);
2724*4882a593Smuzhiyun
2725*4882a593Smuzhiyun map->unlock(map->lock_arg);
2726*4882a593Smuzhiyun
2727*4882a593Smuzhiyun return ret;
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_read);
2730*4882a593Smuzhiyun
2731*4882a593Smuzhiyun /**
2732*4882a593Smuzhiyun * regmap_raw_read() - Read raw data from the device
2733*4882a593Smuzhiyun *
2734*4882a593Smuzhiyun * @map: Register map to read from
2735*4882a593Smuzhiyun * @reg: First register to be read from
2736*4882a593Smuzhiyun * @val: Pointer to store read value
2737*4882a593Smuzhiyun * @val_len: Size of data to read
2738*4882a593Smuzhiyun *
2739*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2740*4882a593Smuzhiyun * be returned in error cases.
2741*4882a593Smuzhiyun */
regmap_raw_read(struct regmap * map,unsigned int reg,void * val,size_t val_len)2742*4882a593Smuzhiyun int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2743*4882a593Smuzhiyun size_t val_len)
2744*4882a593Smuzhiyun {
2745*4882a593Smuzhiyun size_t val_bytes = map->format.val_bytes;
2746*4882a593Smuzhiyun size_t val_count = val_len / val_bytes;
2747*4882a593Smuzhiyun unsigned int v;
2748*4882a593Smuzhiyun int ret, i;
2749*4882a593Smuzhiyun
2750*4882a593Smuzhiyun if (!map->bus)
2751*4882a593Smuzhiyun return -EINVAL;
2752*4882a593Smuzhiyun if (val_len % map->format.val_bytes)
2753*4882a593Smuzhiyun return -EINVAL;
2754*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2755*4882a593Smuzhiyun return -EINVAL;
2756*4882a593Smuzhiyun if (val_count == 0)
2757*4882a593Smuzhiyun return -EINVAL;
2758*4882a593Smuzhiyun
2759*4882a593Smuzhiyun map->lock(map->lock_arg);
2760*4882a593Smuzhiyun
2761*4882a593Smuzhiyun if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2762*4882a593Smuzhiyun map->cache_type == REGCACHE_NONE) {
2763*4882a593Smuzhiyun size_t chunk_count, chunk_bytes;
2764*4882a593Smuzhiyun size_t chunk_regs = val_count;
2765*4882a593Smuzhiyun
2766*4882a593Smuzhiyun if (!map->bus->read) {
2767*4882a593Smuzhiyun ret = -ENOTSUPP;
2768*4882a593Smuzhiyun goto out;
2769*4882a593Smuzhiyun }
2770*4882a593Smuzhiyun
2771*4882a593Smuzhiyun if (map->use_single_read)
2772*4882a593Smuzhiyun chunk_regs = 1;
2773*4882a593Smuzhiyun else if (map->max_raw_read && val_len > map->max_raw_read)
2774*4882a593Smuzhiyun chunk_regs = map->max_raw_read / val_bytes;
2775*4882a593Smuzhiyun
2776*4882a593Smuzhiyun chunk_count = val_count / chunk_regs;
2777*4882a593Smuzhiyun chunk_bytes = chunk_regs * val_bytes;
2778*4882a593Smuzhiyun
2779*4882a593Smuzhiyun /* Read bytes that fit into whole chunks */
2780*4882a593Smuzhiyun for (i = 0; i < chunk_count; i++) {
2781*4882a593Smuzhiyun ret = _regmap_raw_read(map, reg, val, chunk_bytes, false);
2782*4882a593Smuzhiyun if (ret != 0)
2783*4882a593Smuzhiyun goto out;
2784*4882a593Smuzhiyun
2785*4882a593Smuzhiyun reg += regmap_get_offset(map, chunk_regs);
2786*4882a593Smuzhiyun val += chunk_bytes;
2787*4882a593Smuzhiyun val_len -= chunk_bytes;
2788*4882a593Smuzhiyun }
2789*4882a593Smuzhiyun
2790*4882a593Smuzhiyun /* Read remaining bytes */
2791*4882a593Smuzhiyun if (val_len) {
2792*4882a593Smuzhiyun ret = _regmap_raw_read(map, reg, val, val_len, false);
2793*4882a593Smuzhiyun if (ret != 0)
2794*4882a593Smuzhiyun goto out;
2795*4882a593Smuzhiyun }
2796*4882a593Smuzhiyun } else {
2797*4882a593Smuzhiyun /* Otherwise go word by word for the cache; should be low
2798*4882a593Smuzhiyun * cost as we expect to hit the cache.
2799*4882a593Smuzhiyun */
2800*4882a593Smuzhiyun for (i = 0; i < val_count; i++) {
2801*4882a593Smuzhiyun ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2802*4882a593Smuzhiyun &v);
2803*4882a593Smuzhiyun if (ret != 0)
2804*4882a593Smuzhiyun goto out;
2805*4882a593Smuzhiyun
2806*4882a593Smuzhiyun map->format.format_val(val + (i * val_bytes), v, 0);
2807*4882a593Smuzhiyun }
2808*4882a593Smuzhiyun }
2809*4882a593Smuzhiyun
2810*4882a593Smuzhiyun out:
2811*4882a593Smuzhiyun map->unlock(map->lock_arg);
2812*4882a593Smuzhiyun
2813*4882a593Smuzhiyun return ret;
2814*4882a593Smuzhiyun }
2815*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_raw_read);
2816*4882a593Smuzhiyun
2817*4882a593Smuzhiyun /**
2818*4882a593Smuzhiyun * regmap_noinc_read(): Read data from a register without incrementing the
2819*4882a593Smuzhiyun * register number
2820*4882a593Smuzhiyun *
2821*4882a593Smuzhiyun * @map: Register map to read from
2822*4882a593Smuzhiyun * @reg: Register to read from
2823*4882a593Smuzhiyun * @val: Pointer to data buffer
2824*4882a593Smuzhiyun * @val_len: Length of output buffer in bytes.
2825*4882a593Smuzhiyun *
2826*4882a593Smuzhiyun * The regmap API usually assumes that bulk bus read operations will read a
2827*4882a593Smuzhiyun * range of registers. Some devices have certain registers for which a read
2828*4882a593Smuzhiyun * operation read will read from an internal FIFO.
2829*4882a593Smuzhiyun *
2830*4882a593Smuzhiyun * The target register must be volatile but registers after it can be
2831*4882a593Smuzhiyun * completely unrelated cacheable registers.
2832*4882a593Smuzhiyun *
2833*4882a593Smuzhiyun * This will attempt multiple reads as required to read val_len bytes.
2834*4882a593Smuzhiyun *
2835*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will be
2836*4882a593Smuzhiyun * returned in error cases.
2837*4882a593Smuzhiyun */
regmap_noinc_read(struct regmap * map,unsigned int reg,void * val,size_t val_len)2838*4882a593Smuzhiyun int regmap_noinc_read(struct regmap *map, unsigned int reg,
2839*4882a593Smuzhiyun void *val, size_t val_len)
2840*4882a593Smuzhiyun {
2841*4882a593Smuzhiyun size_t read_len;
2842*4882a593Smuzhiyun int ret;
2843*4882a593Smuzhiyun
2844*4882a593Smuzhiyun if (!map->bus)
2845*4882a593Smuzhiyun return -EINVAL;
2846*4882a593Smuzhiyun if (!map->bus->read)
2847*4882a593Smuzhiyun return -ENOTSUPP;
2848*4882a593Smuzhiyun if (val_len % map->format.val_bytes)
2849*4882a593Smuzhiyun return -EINVAL;
2850*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2851*4882a593Smuzhiyun return -EINVAL;
2852*4882a593Smuzhiyun if (val_len == 0)
2853*4882a593Smuzhiyun return -EINVAL;
2854*4882a593Smuzhiyun
2855*4882a593Smuzhiyun map->lock(map->lock_arg);
2856*4882a593Smuzhiyun
2857*4882a593Smuzhiyun if (!regmap_volatile(map, reg) || !regmap_readable_noinc(map, reg)) {
2858*4882a593Smuzhiyun ret = -EINVAL;
2859*4882a593Smuzhiyun goto out_unlock;
2860*4882a593Smuzhiyun }
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun while (val_len) {
2863*4882a593Smuzhiyun if (map->max_raw_read && map->max_raw_read < val_len)
2864*4882a593Smuzhiyun read_len = map->max_raw_read;
2865*4882a593Smuzhiyun else
2866*4882a593Smuzhiyun read_len = val_len;
2867*4882a593Smuzhiyun ret = _regmap_raw_read(map, reg, val, read_len, true);
2868*4882a593Smuzhiyun if (ret)
2869*4882a593Smuzhiyun goto out_unlock;
2870*4882a593Smuzhiyun val = ((u8 *)val) + read_len;
2871*4882a593Smuzhiyun val_len -= read_len;
2872*4882a593Smuzhiyun }
2873*4882a593Smuzhiyun
2874*4882a593Smuzhiyun out_unlock:
2875*4882a593Smuzhiyun map->unlock(map->lock_arg);
2876*4882a593Smuzhiyun return ret;
2877*4882a593Smuzhiyun }
2878*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_noinc_read);
2879*4882a593Smuzhiyun
2880*4882a593Smuzhiyun /**
2881*4882a593Smuzhiyun * regmap_field_read(): Read a value to a single register field
2882*4882a593Smuzhiyun *
2883*4882a593Smuzhiyun * @field: Register field to read from
2884*4882a593Smuzhiyun * @val: Pointer to store read value
2885*4882a593Smuzhiyun *
2886*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2887*4882a593Smuzhiyun * be returned in error cases.
2888*4882a593Smuzhiyun */
regmap_field_read(struct regmap_field * field,unsigned int * val)2889*4882a593Smuzhiyun int regmap_field_read(struct regmap_field *field, unsigned int *val)
2890*4882a593Smuzhiyun {
2891*4882a593Smuzhiyun int ret;
2892*4882a593Smuzhiyun unsigned int reg_val;
2893*4882a593Smuzhiyun ret = regmap_read(field->regmap, field->reg, ®_val);
2894*4882a593Smuzhiyun if (ret != 0)
2895*4882a593Smuzhiyun return ret;
2896*4882a593Smuzhiyun
2897*4882a593Smuzhiyun reg_val &= field->mask;
2898*4882a593Smuzhiyun reg_val >>= field->shift;
2899*4882a593Smuzhiyun *val = reg_val;
2900*4882a593Smuzhiyun
2901*4882a593Smuzhiyun return ret;
2902*4882a593Smuzhiyun }
2903*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_field_read);
2904*4882a593Smuzhiyun
2905*4882a593Smuzhiyun /**
2906*4882a593Smuzhiyun * regmap_fields_read() - Read a value to a single register field with port ID
2907*4882a593Smuzhiyun *
2908*4882a593Smuzhiyun * @field: Register field to read from
2909*4882a593Smuzhiyun * @id: port ID
2910*4882a593Smuzhiyun * @val: Pointer to store read value
2911*4882a593Smuzhiyun *
2912*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2913*4882a593Smuzhiyun * be returned in error cases.
2914*4882a593Smuzhiyun */
regmap_fields_read(struct regmap_field * field,unsigned int id,unsigned int * val)2915*4882a593Smuzhiyun int regmap_fields_read(struct regmap_field *field, unsigned int id,
2916*4882a593Smuzhiyun unsigned int *val)
2917*4882a593Smuzhiyun {
2918*4882a593Smuzhiyun int ret;
2919*4882a593Smuzhiyun unsigned int reg_val;
2920*4882a593Smuzhiyun
2921*4882a593Smuzhiyun if (id >= field->id_size)
2922*4882a593Smuzhiyun return -EINVAL;
2923*4882a593Smuzhiyun
2924*4882a593Smuzhiyun ret = regmap_read(field->regmap,
2925*4882a593Smuzhiyun field->reg + (field->id_offset * id),
2926*4882a593Smuzhiyun ®_val);
2927*4882a593Smuzhiyun if (ret != 0)
2928*4882a593Smuzhiyun return ret;
2929*4882a593Smuzhiyun
2930*4882a593Smuzhiyun reg_val &= field->mask;
2931*4882a593Smuzhiyun reg_val >>= field->shift;
2932*4882a593Smuzhiyun *val = reg_val;
2933*4882a593Smuzhiyun
2934*4882a593Smuzhiyun return ret;
2935*4882a593Smuzhiyun }
2936*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_fields_read);
2937*4882a593Smuzhiyun
2938*4882a593Smuzhiyun /**
2939*4882a593Smuzhiyun * regmap_bulk_read() - Read multiple registers from the device
2940*4882a593Smuzhiyun *
2941*4882a593Smuzhiyun * @map: Register map to read from
2942*4882a593Smuzhiyun * @reg: First register to be read from
2943*4882a593Smuzhiyun * @val: Pointer to store read value, in native register size for device
2944*4882a593Smuzhiyun * @val_count: Number of registers to read
2945*4882a593Smuzhiyun *
2946*4882a593Smuzhiyun * A value of zero will be returned on success, a negative errno will
2947*4882a593Smuzhiyun * be returned in error cases.
2948*4882a593Smuzhiyun */
regmap_bulk_read(struct regmap * map,unsigned int reg,void * val,size_t val_count)2949*4882a593Smuzhiyun int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2950*4882a593Smuzhiyun size_t val_count)
2951*4882a593Smuzhiyun {
2952*4882a593Smuzhiyun int ret, i;
2953*4882a593Smuzhiyun size_t val_bytes = map->format.val_bytes;
2954*4882a593Smuzhiyun bool vol = regmap_volatile_range(map, reg, val_count);
2955*4882a593Smuzhiyun
2956*4882a593Smuzhiyun if (!IS_ALIGNED(reg, map->reg_stride))
2957*4882a593Smuzhiyun return -EINVAL;
2958*4882a593Smuzhiyun if (val_count == 0)
2959*4882a593Smuzhiyun return -EINVAL;
2960*4882a593Smuzhiyun
2961*4882a593Smuzhiyun if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2962*4882a593Smuzhiyun ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
2963*4882a593Smuzhiyun if (ret != 0)
2964*4882a593Smuzhiyun return ret;
2965*4882a593Smuzhiyun
2966*4882a593Smuzhiyun for (i = 0; i < val_count * val_bytes; i += val_bytes)
2967*4882a593Smuzhiyun map->format.parse_inplace(val + i);
2968*4882a593Smuzhiyun } else {
2969*4882a593Smuzhiyun #ifdef CONFIG_64BIT
2970*4882a593Smuzhiyun u64 *u64 = val;
2971*4882a593Smuzhiyun #endif
2972*4882a593Smuzhiyun u32 *u32 = val;
2973*4882a593Smuzhiyun u16 *u16 = val;
2974*4882a593Smuzhiyun u8 *u8 = val;
2975*4882a593Smuzhiyun
2976*4882a593Smuzhiyun map->lock(map->lock_arg);
2977*4882a593Smuzhiyun
2978*4882a593Smuzhiyun for (i = 0; i < val_count; i++) {
2979*4882a593Smuzhiyun unsigned int ival;
2980*4882a593Smuzhiyun
2981*4882a593Smuzhiyun ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2982*4882a593Smuzhiyun &ival);
2983*4882a593Smuzhiyun if (ret != 0)
2984*4882a593Smuzhiyun goto out;
2985*4882a593Smuzhiyun
2986*4882a593Smuzhiyun switch (map->format.val_bytes) {
2987*4882a593Smuzhiyun #ifdef CONFIG_64BIT
2988*4882a593Smuzhiyun case 8:
2989*4882a593Smuzhiyun u64[i] = ival;
2990*4882a593Smuzhiyun break;
2991*4882a593Smuzhiyun #endif
2992*4882a593Smuzhiyun case 4:
2993*4882a593Smuzhiyun u32[i] = ival;
2994*4882a593Smuzhiyun break;
2995*4882a593Smuzhiyun case 2:
2996*4882a593Smuzhiyun u16[i] = ival;
2997*4882a593Smuzhiyun break;
2998*4882a593Smuzhiyun case 1:
2999*4882a593Smuzhiyun u8[i] = ival;
3000*4882a593Smuzhiyun break;
3001*4882a593Smuzhiyun default:
3002*4882a593Smuzhiyun ret = -EINVAL;
3003*4882a593Smuzhiyun goto out;
3004*4882a593Smuzhiyun }
3005*4882a593Smuzhiyun }
3006*4882a593Smuzhiyun
3007*4882a593Smuzhiyun out:
3008*4882a593Smuzhiyun map->unlock(map->lock_arg);
3009*4882a593Smuzhiyun }
3010*4882a593Smuzhiyun
3011*4882a593Smuzhiyun return ret;
3012*4882a593Smuzhiyun }
3013*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_bulk_read);
3014*4882a593Smuzhiyun
_regmap_update_bits(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change,bool force_write)3015*4882a593Smuzhiyun static int _regmap_update_bits(struct regmap *map, unsigned int reg,
3016*4882a593Smuzhiyun unsigned int mask, unsigned int val,
3017*4882a593Smuzhiyun bool *change, bool force_write)
3018*4882a593Smuzhiyun {
3019*4882a593Smuzhiyun int ret;
3020*4882a593Smuzhiyun unsigned int tmp, orig;
3021*4882a593Smuzhiyun
3022*4882a593Smuzhiyun if (change)
3023*4882a593Smuzhiyun *change = false;
3024*4882a593Smuzhiyun
3025*4882a593Smuzhiyun if (regmap_volatile(map, reg) && map->reg_update_bits) {
3026*4882a593Smuzhiyun ret = map->reg_update_bits(map->bus_context, reg, mask, val);
3027*4882a593Smuzhiyun if (ret == 0 && change)
3028*4882a593Smuzhiyun *change = true;
3029*4882a593Smuzhiyun } else {
3030*4882a593Smuzhiyun ret = _regmap_read(map, reg, &orig);
3031*4882a593Smuzhiyun if (ret != 0)
3032*4882a593Smuzhiyun return ret;
3033*4882a593Smuzhiyun
3034*4882a593Smuzhiyun tmp = orig & ~mask;
3035*4882a593Smuzhiyun tmp |= val & mask;
3036*4882a593Smuzhiyun
3037*4882a593Smuzhiyun if (force_write || (tmp != orig)) {
3038*4882a593Smuzhiyun ret = _regmap_write(map, reg, tmp);
3039*4882a593Smuzhiyun if (ret == 0 && change)
3040*4882a593Smuzhiyun *change = true;
3041*4882a593Smuzhiyun }
3042*4882a593Smuzhiyun }
3043*4882a593Smuzhiyun
3044*4882a593Smuzhiyun return ret;
3045*4882a593Smuzhiyun }
3046*4882a593Smuzhiyun
3047*4882a593Smuzhiyun /**
3048*4882a593Smuzhiyun * regmap_update_bits_base() - Perform a read/modify/write cycle on a register
3049*4882a593Smuzhiyun *
3050*4882a593Smuzhiyun * @map: Register map to update
3051*4882a593Smuzhiyun * @reg: Register to update
3052*4882a593Smuzhiyun * @mask: Bitmask to change
3053*4882a593Smuzhiyun * @val: New value for bitmask
3054*4882a593Smuzhiyun * @change: Boolean indicating if a write was done
3055*4882a593Smuzhiyun * @async: Boolean indicating asynchronously
3056*4882a593Smuzhiyun * @force: Boolean indicating use force update
3057*4882a593Smuzhiyun *
3058*4882a593Smuzhiyun * Perform a read/modify/write cycle on a register map with change, async, force
3059*4882a593Smuzhiyun * options.
3060*4882a593Smuzhiyun *
3061*4882a593Smuzhiyun * If async is true:
3062*4882a593Smuzhiyun *
3063*4882a593Smuzhiyun * With most buses the read must be done synchronously so this is most useful
3064*4882a593Smuzhiyun * for devices with a cache which do not need to interact with the hardware to
3065*4882a593Smuzhiyun * determine the current register value.
3066*4882a593Smuzhiyun *
3067*4882a593Smuzhiyun * Returns zero for success, a negative number on error.
3068*4882a593Smuzhiyun */
regmap_update_bits_base(struct regmap * map,unsigned int reg,unsigned int mask,unsigned int val,bool * change,bool async,bool force)3069*4882a593Smuzhiyun int regmap_update_bits_base(struct regmap *map, unsigned int reg,
3070*4882a593Smuzhiyun unsigned int mask, unsigned int val,
3071*4882a593Smuzhiyun bool *change, bool async, bool force)
3072*4882a593Smuzhiyun {
3073*4882a593Smuzhiyun int ret;
3074*4882a593Smuzhiyun
3075*4882a593Smuzhiyun map->lock(map->lock_arg);
3076*4882a593Smuzhiyun
3077*4882a593Smuzhiyun map->async = async;
3078*4882a593Smuzhiyun
3079*4882a593Smuzhiyun ret = _regmap_update_bits(map, reg, mask, val, change, force);
3080*4882a593Smuzhiyun
3081*4882a593Smuzhiyun map->async = false;
3082*4882a593Smuzhiyun
3083*4882a593Smuzhiyun map->unlock(map->lock_arg);
3084*4882a593Smuzhiyun
3085*4882a593Smuzhiyun return ret;
3086*4882a593Smuzhiyun }
3087*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_update_bits_base);
3088*4882a593Smuzhiyun
3089*4882a593Smuzhiyun /**
3090*4882a593Smuzhiyun * regmap_test_bits() - Check if all specified bits are set in a register.
3091*4882a593Smuzhiyun *
3092*4882a593Smuzhiyun * @map: Register map to operate on
3093*4882a593Smuzhiyun * @reg: Register to read from
3094*4882a593Smuzhiyun * @bits: Bits to test
3095*4882a593Smuzhiyun *
3096*4882a593Smuzhiyun * Returns 0 if at least one of the tested bits is not set, 1 if all tested
3097*4882a593Smuzhiyun * bits are set and a negative error number if the underlying regmap_read()
3098*4882a593Smuzhiyun * fails.
3099*4882a593Smuzhiyun */
regmap_test_bits(struct regmap * map,unsigned int reg,unsigned int bits)3100*4882a593Smuzhiyun int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits)
3101*4882a593Smuzhiyun {
3102*4882a593Smuzhiyun unsigned int val, ret;
3103*4882a593Smuzhiyun
3104*4882a593Smuzhiyun ret = regmap_read(map, reg, &val);
3105*4882a593Smuzhiyun if (ret)
3106*4882a593Smuzhiyun return ret;
3107*4882a593Smuzhiyun
3108*4882a593Smuzhiyun return (val & bits) == bits;
3109*4882a593Smuzhiyun }
3110*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_test_bits);
3111*4882a593Smuzhiyun
regmap_async_complete_cb(struct regmap_async * async,int ret)3112*4882a593Smuzhiyun void regmap_async_complete_cb(struct regmap_async *async, int ret)
3113*4882a593Smuzhiyun {
3114*4882a593Smuzhiyun struct regmap *map = async->map;
3115*4882a593Smuzhiyun bool wake;
3116*4882a593Smuzhiyun
3117*4882a593Smuzhiyun trace_regmap_async_io_complete(map);
3118*4882a593Smuzhiyun
3119*4882a593Smuzhiyun spin_lock(&map->async_lock);
3120*4882a593Smuzhiyun list_move(&async->list, &map->async_free);
3121*4882a593Smuzhiyun wake = list_empty(&map->async_list);
3122*4882a593Smuzhiyun
3123*4882a593Smuzhiyun if (ret != 0)
3124*4882a593Smuzhiyun map->async_ret = ret;
3125*4882a593Smuzhiyun
3126*4882a593Smuzhiyun spin_unlock(&map->async_lock);
3127*4882a593Smuzhiyun
3128*4882a593Smuzhiyun if (wake)
3129*4882a593Smuzhiyun wake_up(&map->async_waitq);
3130*4882a593Smuzhiyun }
3131*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
3132*4882a593Smuzhiyun
regmap_async_is_done(struct regmap * map)3133*4882a593Smuzhiyun static int regmap_async_is_done(struct regmap *map)
3134*4882a593Smuzhiyun {
3135*4882a593Smuzhiyun unsigned long flags;
3136*4882a593Smuzhiyun int ret;
3137*4882a593Smuzhiyun
3138*4882a593Smuzhiyun spin_lock_irqsave(&map->async_lock, flags);
3139*4882a593Smuzhiyun ret = list_empty(&map->async_list);
3140*4882a593Smuzhiyun spin_unlock_irqrestore(&map->async_lock, flags);
3141*4882a593Smuzhiyun
3142*4882a593Smuzhiyun return ret;
3143*4882a593Smuzhiyun }
3144*4882a593Smuzhiyun
3145*4882a593Smuzhiyun /**
3146*4882a593Smuzhiyun * regmap_async_complete - Ensure all asynchronous I/O has completed.
3147*4882a593Smuzhiyun *
3148*4882a593Smuzhiyun * @map: Map to operate on.
3149*4882a593Smuzhiyun *
3150*4882a593Smuzhiyun * Blocks until any pending asynchronous I/O has completed. Returns
3151*4882a593Smuzhiyun * an error code for any failed I/O operations.
3152*4882a593Smuzhiyun */
regmap_async_complete(struct regmap * map)3153*4882a593Smuzhiyun int regmap_async_complete(struct regmap *map)
3154*4882a593Smuzhiyun {
3155*4882a593Smuzhiyun unsigned long flags;
3156*4882a593Smuzhiyun int ret;
3157*4882a593Smuzhiyun
3158*4882a593Smuzhiyun /* Nothing to do with no async support */
3159*4882a593Smuzhiyun if (!map->bus || !map->bus->async_write)
3160*4882a593Smuzhiyun return 0;
3161*4882a593Smuzhiyun
3162*4882a593Smuzhiyun trace_regmap_async_complete_start(map);
3163*4882a593Smuzhiyun
3164*4882a593Smuzhiyun wait_event(map->async_waitq, regmap_async_is_done(map));
3165*4882a593Smuzhiyun
3166*4882a593Smuzhiyun spin_lock_irqsave(&map->async_lock, flags);
3167*4882a593Smuzhiyun ret = map->async_ret;
3168*4882a593Smuzhiyun map->async_ret = 0;
3169*4882a593Smuzhiyun spin_unlock_irqrestore(&map->async_lock, flags);
3170*4882a593Smuzhiyun
3171*4882a593Smuzhiyun trace_regmap_async_complete_done(map);
3172*4882a593Smuzhiyun
3173*4882a593Smuzhiyun return ret;
3174*4882a593Smuzhiyun }
3175*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_async_complete);
3176*4882a593Smuzhiyun
3177*4882a593Smuzhiyun /**
3178*4882a593Smuzhiyun * regmap_register_patch - Register and apply register updates to be applied
3179*4882a593Smuzhiyun * on device initialistion
3180*4882a593Smuzhiyun *
3181*4882a593Smuzhiyun * @map: Register map to apply updates to.
3182*4882a593Smuzhiyun * @regs: Values to update.
3183*4882a593Smuzhiyun * @num_regs: Number of entries in regs.
3184*4882a593Smuzhiyun *
3185*4882a593Smuzhiyun * Register a set of register updates to be applied to the device
3186*4882a593Smuzhiyun * whenever the device registers are synchronised with the cache and
3187*4882a593Smuzhiyun * apply them immediately. Typically this is used to apply
3188*4882a593Smuzhiyun * corrections to be applied to the device defaults on startup, such
3189*4882a593Smuzhiyun * as the updates some vendors provide to undocumented registers.
3190*4882a593Smuzhiyun *
3191*4882a593Smuzhiyun * The caller must ensure that this function cannot be called
3192*4882a593Smuzhiyun * concurrently with either itself or regcache_sync().
3193*4882a593Smuzhiyun */
regmap_register_patch(struct regmap * map,const struct reg_sequence * regs,int num_regs)3194*4882a593Smuzhiyun int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
3195*4882a593Smuzhiyun int num_regs)
3196*4882a593Smuzhiyun {
3197*4882a593Smuzhiyun struct reg_sequence *p;
3198*4882a593Smuzhiyun int ret;
3199*4882a593Smuzhiyun bool bypass;
3200*4882a593Smuzhiyun
3201*4882a593Smuzhiyun if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
3202*4882a593Smuzhiyun num_regs))
3203*4882a593Smuzhiyun return 0;
3204*4882a593Smuzhiyun
3205*4882a593Smuzhiyun p = krealloc(map->patch,
3206*4882a593Smuzhiyun sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
3207*4882a593Smuzhiyun GFP_KERNEL);
3208*4882a593Smuzhiyun if (p) {
3209*4882a593Smuzhiyun memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
3210*4882a593Smuzhiyun map->patch = p;
3211*4882a593Smuzhiyun map->patch_regs += num_regs;
3212*4882a593Smuzhiyun } else {
3213*4882a593Smuzhiyun return -ENOMEM;
3214*4882a593Smuzhiyun }
3215*4882a593Smuzhiyun
3216*4882a593Smuzhiyun map->lock(map->lock_arg);
3217*4882a593Smuzhiyun
3218*4882a593Smuzhiyun bypass = map->cache_bypass;
3219*4882a593Smuzhiyun
3220*4882a593Smuzhiyun map->cache_bypass = true;
3221*4882a593Smuzhiyun map->async = true;
3222*4882a593Smuzhiyun
3223*4882a593Smuzhiyun ret = _regmap_multi_reg_write(map, regs, num_regs);
3224*4882a593Smuzhiyun
3225*4882a593Smuzhiyun map->async = false;
3226*4882a593Smuzhiyun map->cache_bypass = bypass;
3227*4882a593Smuzhiyun
3228*4882a593Smuzhiyun map->unlock(map->lock_arg);
3229*4882a593Smuzhiyun
3230*4882a593Smuzhiyun regmap_async_complete(map);
3231*4882a593Smuzhiyun
3232*4882a593Smuzhiyun return ret;
3233*4882a593Smuzhiyun }
3234*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_register_patch);
3235*4882a593Smuzhiyun
3236*4882a593Smuzhiyun /**
3237*4882a593Smuzhiyun * regmap_get_val_bytes() - Report the size of a register value
3238*4882a593Smuzhiyun *
3239*4882a593Smuzhiyun * @map: Register map to operate on.
3240*4882a593Smuzhiyun *
3241*4882a593Smuzhiyun * Report the size of a register value, mainly intended to for use by
3242*4882a593Smuzhiyun * generic infrastructure built on top of regmap.
3243*4882a593Smuzhiyun */
regmap_get_val_bytes(struct regmap * map)3244*4882a593Smuzhiyun int regmap_get_val_bytes(struct regmap *map)
3245*4882a593Smuzhiyun {
3246*4882a593Smuzhiyun if (map->format.format_write)
3247*4882a593Smuzhiyun return -EINVAL;
3248*4882a593Smuzhiyun
3249*4882a593Smuzhiyun return map->format.val_bytes;
3250*4882a593Smuzhiyun }
3251*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
3252*4882a593Smuzhiyun
3253*4882a593Smuzhiyun /**
3254*4882a593Smuzhiyun * regmap_get_max_register() - Report the max register value
3255*4882a593Smuzhiyun *
3256*4882a593Smuzhiyun * @map: Register map to operate on.
3257*4882a593Smuzhiyun *
3258*4882a593Smuzhiyun * Report the max register value, mainly intended to for use by
3259*4882a593Smuzhiyun * generic infrastructure built on top of regmap.
3260*4882a593Smuzhiyun */
regmap_get_max_register(struct regmap * map)3261*4882a593Smuzhiyun int regmap_get_max_register(struct regmap *map)
3262*4882a593Smuzhiyun {
3263*4882a593Smuzhiyun return map->max_register ? map->max_register : -EINVAL;
3264*4882a593Smuzhiyun }
3265*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_max_register);
3266*4882a593Smuzhiyun
3267*4882a593Smuzhiyun /**
3268*4882a593Smuzhiyun * regmap_get_reg_stride() - Report the register address stride
3269*4882a593Smuzhiyun *
3270*4882a593Smuzhiyun * @map: Register map to operate on.
3271*4882a593Smuzhiyun *
3272*4882a593Smuzhiyun * Report the register address stride, mainly intended to for use by
3273*4882a593Smuzhiyun * generic infrastructure built on top of regmap.
3274*4882a593Smuzhiyun */
regmap_get_reg_stride(struct regmap * map)3275*4882a593Smuzhiyun int regmap_get_reg_stride(struct regmap *map)
3276*4882a593Smuzhiyun {
3277*4882a593Smuzhiyun return map->reg_stride;
3278*4882a593Smuzhiyun }
3279*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
3280*4882a593Smuzhiyun
regmap_parse_val(struct regmap * map,const void * buf,unsigned int * val)3281*4882a593Smuzhiyun int regmap_parse_val(struct regmap *map, const void *buf,
3282*4882a593Smuzhiyun unsigned int *val)
3283*4882a593Smuzhiyun {
3284*4882a593Smuzhiyun if (!map->format.parse_val)
3285*4882a593Smuzhiyun return -EINVAL;
3286*4882a593Smuzhiyun
3287*4882a593Smuzhiyun *val = map->format.parse_val(buf);
3288*4882a593Smuzhiyun
3289*4882a593Smuzhiyun return 0;
3290*4882a593Smuzhiyun }
3291*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_parse_val);
3292*4882a593Smuzhiyun
regmap_initcall(void)3293*4882a593Smuzhiyun static int __init regmap_initcall(void)
3294*4882a593Smuzhiyun {
3295*4882a593Smuzhiyun regmap_debugfs_initcall();
3296*4882a593Smuzhiyun
3297*4882a593Smuzhiyun return 0;
3298*4882a593Smuzhiyun }
3299*4882a593Smuzhiyun postcore_initcall(regmap_initcall);
3300