1/*
2 * Register map access API - SPMI support
3 *
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 *
6 * Based on regmap-i2c.c:
7 * Copyright 2011 Wolfson Microelectronics plc
8 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 and
12 * only version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 */
20#include <linux/regmap.h>
21#include <linux/spmi.h>
22#include <linux/module.h>
23#include <linux/init.h>
24
25static int regmap_spmi_base_read(void *context,
26				 const void *reg, size_t reg_size,
27				 void *val, size_t val_size)
28{
29	u8 addr = *(u8 *)reg;
30	int err = 0;
31
32	BUG_ON(reg_size != 1);
33
34	while (val_size-- && !err)
35		err = spmi_register_read(context, addr++, val++);
36
37	return err;
38}
39
40static int regmap_spmi_base_gather_write(void *context,
41					 const void *reg, size_t reg_size,
42					 const void *val, size_t val_size)
43{
44	const u8 *data = val;
45	u8 addr = *(u8 *)reg;
46	int err = 0;
47
48	BUG_ON(reg_size != 1);
49
50	/*
51	 * SPMI defines a more bandwidth-efficient 'Register 0 Write' sequence,
52	 * use it when possible.
53	 */
54	if (addr == 0 && val_size) {
55		err = spmi_register_zero_write(context, *data);
56		if (err)
57			goto err_out;
58
59		data++;
60		addr++;
61		val_size--;
62	}
63
64	while (val_size) {
65		err = spmi_register_write(context, addr, *data);
66		if (err)
67			goto err_out;
68
69		data++;
70		addr++;
71		val_size--;
72	}
73
74err_out:
75	return err;
76}
77
78static int regmap_spmi_base_write(void *context, const void *data,
79				  size_t count)
80{
81	BUG_ON(count < 1);
82	return regmap_spmi_base_gather_write(context, data, 1, data + 1,
83					     count - 1);
84}
85
86static struct regmap_bus regmap_spmi_base = {
87	.read				= regmap_spmi_base_read,
88	.write				= regmap_spmi_base_write,
89	.gather_write			= regmap_spmi_base_gather_write,
90	.reg_format_endian_default	= REGMAP_ENDIAN_NATIVE,
91	.val_format_endian_default	= REGMAP_ENDIAN_NATIVE,
92};
93
94/**
95 * regmap_init_spmi_base(): Create regmap for the Base register space
96 * @sdev:	SPMI device that will be interacted with
97 * @config:	Configuration for register map
98 *
99 * The return value will be an ERR_PTR() on error or a valid pointer to
100 * a struct regmap.
101 */
102struct regmap *regmap_init_spmi_base(struct spmi_device *sdev,
103				     const struct regmap_config *config)
104{
105	return regmap_init(&sdev->dev, &regmap_spmi_base, sdev, config);
106}
107EXPORT_SYMBOL_GPL(regmap_init_spmi_base);
108
109/**
110 * devm_regmap_init_spmi_base(): Create managed regmap for Base register space
111 * @sdev:	SPMI device that will be interacted with
112 * @config:	Configuration for register map
113 *
114 * The return value will be an ERR_PTR() on error or a valid pointer
115 * to a struct regmap.  The regmap will be automatically freed by the
116 * device management code.
117 */
118struct regmap *devm_regmap_init_spmi_base(struct spmi_device *sdev,
119					  const struct regmap_config *config)
120{
121	return devm_regmap_init(&sdev->dev, &regmap_spmi_base, sdev, config);
122}
123EXPORT_SYMBOL_GPL(devm_regmap_init_spmi_base);
124
125static int regmap_spmi_ext_read(void *context,
126				const void *reg, size_t reg_size,
127				void *val, size_t val_size)
128{
129	int err = 0;
130	size_t len;
131	u16 addr;
132
133	BUG_ON(reg_size != 2);
134
135	addr = *(u16 *)reg;
136
137	/*
138	 * Split accesses into two to take advantage of the more
139	 * bandwidth-efficient 'Extended Register Read' command when possible
140	 */
141	while (addr <= 0xFF && val_size) {
142		len = min_t(size_t, val_size, 16);
143
144		err = spmi_ext_register_read(context, addr, val, len);
145		if (err)
146			goto err_out;
147
148		addr += len;
149		val += len;
150		val_size -= len;
151	}
152
153	while (val_size) {
154		len = min_t(size_t, val_size, 8);
155
156		err = spmi_ext_register_readl(context, addr, val, len);
157		if (err)
158			goto err_out;
159
160		addr += len;
161		val += len;
162		val_size -= len;
163	}
164
165err_out:
166	return err;
167}
168
169static int regmap_spmi_ext_gather_write(void *context,
170					const void *reg, size_t reg_size,
171					const void *val, size_t val_size)
172{
173	int err = 0;
174	size_t len;
175	u16 addr;
176
177	BUG_ON(reg_size != 2);
178
179	addr = *(u16 *)reg;
180
181	while (addr <= 0xFF && val_size) {
182		len = min_t(size_t, val_size, 16);
183
184		err = spmi_ext_register_write(context, addr, val, len);
185		if (err)
186			goto err_out;
187
188		addr += len;
189		val += len;
190		val_size -= len;
191	}
192
193	while (val_size) {
194		len = min_t(size_t, val_size, 8);
195
196		err = spmi_ext_register_writel(context, addr, val, len);
197		if (err)
198			goto err_out;
199
200		addr += len;
201		val += len;
202		val_size -= len;
203	}
204
205err_out:
206	return err;
207}
208
209static int regmap_spmi_ext_write(void *context, const void *data,
210				 size_t count)
211{
212	BUG_ON(count < 2);
213	return regmap_spmi_ext_gather_write(context, data, 2, data + 2,
214					    count - 2);
215}
216
217static struct regmap_bus regmap_spmi_ext = {
218	.read				= regmap_spmi_ext_read,
219	.write				= regmap_spmi_ext_write,
220	.gather_write			= regmap_spmi_ext_gather_write,
221	.reg_format_endian_default	= REGMAP_ENDIAN_NATIVE,
222	.val_format_endian_default	= REGMAP_ENDIAN_NATIVE,
223};
224
225/**
226 * regmap_init_spmi_ext(): Create regmap for Ext register space
227 * @sdev:	Device that will be interacted with
228 * @config:	Configuration for register map
229 *
230 * The return value will be an ERR_PTR() on error or a valid pointer to
231 * a struct regmap.
232 */
233struct regmap *regmap_init_spmi_ext(struct spmi_device *sdev,
234				    const struct regmap_config *config)
235{
236	return regmap_init(&sdev->dev, &regmap_spmi_ext, sdev, config);
237}
238EXPORT_SYMBOL_GPL(regmap_init_spmi_ext);
239
240/**
241 * devm_regmap_init_spmi_ext(): Create managed regmap for Ext register space
242 * @sdev:	SPMI device that will be interacted with
243 * @config:	Configuration for register map
244 *
245 * The return value will be an ERR_PTR() on error or a valid pointer
246 * to a struct regmap.  The regmap will be automatically freed by the
247 * device management code.
248 */
249struct regmap *devm_regmap_init_spmi_ext(struct spmi_device *sdev,
250				     const struct regmap_config *config)
251{
252	return devm_regmap_init(&sdev->dev, &regmap_spmi_ext, sdev, config);
253}
254EXPORT_SYMBOL_GPL(devm_regmap_init_spmi_ext);
255
256MODULE_LICENSE("GPL");
257