blob: 6ba4b6ecd6073f943b866b69ce7f16bcab562ae1 [file] [log] [blame]
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00001/*
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +02002 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
4 *
Łukasz Majewskic7336812012-11-13 03:21:55 +00005 * Copyright (C) 2011-2012 Samsung Electronics
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00006 * Lukasz Majewski <l.majewski@samsung.com>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie542b7f2011-10-06 02:37:34 +00009 */
10
11#ifndef __CORE_PMIC_H_
12#define __CORE_PMIC_H_
13
Łukasz Majewskic7336812012-11-13 03:21:55 +000014#include <i2c.h>
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020015#include <spi.h>
16#include <linux/list.h>
Łukasz Majewskibd8479e2012-11-13 03:22:00 +000017#include <power/power_chrg.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000018
Łukasz Majewskibd8479e2012-11-13 03:22:00 +000019enum { PMIC_I2C, PMIC_SPI, PMIC_NONE};
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020020
21#ifdef CONFIG_POWER
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000022enum { I2C_PMIC, I2C_NUM, };
23enum { PMIC_READ, PMIC_WRITE, };
Łukasz Majewski86879d72012-11-13 03:21:53 +000024enum { PMIC_SENSOR_BYTE_ORDER_LITTLE, PMIC_SENSOR_BYTE_ORDER_BIG, };
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000025
Simon Glass78a36c32014-05-20 06:01:35 -060026enum {
27 PMIC_CHARGER_DISABLE,
28 PMIC_CHARGER_ENABLE,
29};
30
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000031struct p_i2c {
32 unsigned char addr;
33 unsigned char *buf;
34 unsigned char tx_num;
35};
36
37struct p_spi {
38 unsigned int cs;
39 unsigned int mode;
40 unsigned int bitlen;
41 unsigned int clk;
42 unsigned int flags;
43 u32 (*prepare_tx)(u32 reg, u32 *val, u32 write);
44};
45
Łukasz Majewskibd8479e2012-11-13 03:22:00 +000046struct pmic;
47struct power_fg {
48 int (*fg_battery_check) (struct pmic *p, struct pmic *bat);
49 int (*fg_battery_update) (struct pmic *p, struct pmic *bat);
50};
51
52struct power_chrg {
53 int (*chrg_type) (struct pmic *p);
54 int (*chrg_bat_present) (struct pmic *p);
55 int (*chrg_state) (struct pmic *p, int state, int current);
56};
57
58struct power_battery {
59 struct battery *bat;
60 int (*battery_init) (struct pmic *bat, struct pmic *p1,
61 struct pmic *p2, struct pmic *p3);
62 int (*battery_charge) (struct pmic *bat);
63 /* Keep info about power devices involved with battery operation */
64 struct pmic *chrg, *fg, *muic;
65};
66
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000067struct pmic {
68 const char *name;
69 unsigned char bus;
70 unsigned char interface;
Łukasz Majewski86879d72012-11-13 03:21:53 +000071 unsigned char sensor_byte_order;
Łukasz Majewskic7336812012-11-13 03:21:55 +000072 unsigned int number_of_regs;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000073 union hw {
74 struct p_i2c i2c;
75 struct p_spi spi;
76 } hw;
Łukasz Majewskic7336812012-11-13 03:21:55 +000077
Łukasz Majewskibd8479e2012-11-13 03:22:00 +000078 void (*low_power_mode) (void);
79 struct power_battery *pbat;
80 struct power_chrg *chrg;
81 struct power_fg *fg;
82
83 struct pmic *parent;
Łukasz Majewskic7336812012-11-13 03:21:55 +000084 struct list_head list;
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000085};
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020086#endif /* CONFIG_POWER */
Łukasz Majewskie542b7f2011-10-06 02:37:34 +000087
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020088#ifdef CONFIG_DM_PMIC
89/**
90 * U-Boot PMIC Framework
91 * =====================
92 *
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020093 * UCLASS_PMIC - This is designed to provide an I/O interface for PMIC devices.
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020094 *
95 * For the multi-function PMIC devices, this can be used as parent I/O device
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020096 * for each IC's interface. Then, each child uses its parent for read/write.
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020097 *
98 * The driver model tree could look like this:
99 *
100 *_ root device
101 * |_ BUS 0 device (e.g. I2C0) - UCLASS_I2C/SPI/...
102 * | |_ PMIC device (READ/WRITE ops) - UCLASS_PMIC
103 * | |_ REGULATOR device (ldo/buck/... ops) - UCLASS_REGULATOR
104 * | |_ CHARGER device (charger ops) - UCLASS_CHARGER (in the future)
105 * | |_ MUIC device (microUSB connector ops) - UCLASS_MUIC (in the future)
106 * | |_ ...
107 * |
108 * |_ BUS 1 device (e.g. I2C1) - UCLASS_I2C/SPI/...
109 * |_ PMIC device (READ/WRITE ops) - UCLASS_PMIC
110 * |_ RTC device (rtc ops) - UCLASS_RTC (in the future)
111 *
112 * We can find two PMIC cases in boards design:
113 * - single I/O interface
114 * - multiple I/O interfaces
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200115 * We bind a single PMIC device for each interface, to provide an I/O for
116 * its child devices. And each child usually implements a different function,
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200117 * controlled by the same interface.
118 *
119 * The binding should be done automatically. If device tree nodes/subnodes are
120 * proper defined, then:
121 *
122 * |_ the ROOT driver will bind the device for I2C/SPI node:
123 * |_ the I2C/SPI driver should bind a device for pmic node:
124 * |_ the PMIC driver should bind devices for its childs:
125 * |_ regulator (child)
126 * |_ charger (child)
127 * |_ other (child)
128 *
129 * The same for other device nodes, for multi-interface PMIC.
130 *
131 * Note:
132 * Each PMIC interface driver should use a different compatible string.
133 *
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200134 * If a PMIC child device driver needs access the PMIC-specific registers,
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200135 * it need know only the register address and the access can be done through
136 * the parent pmic driver. Like in the example:
137 *
138 *_ root driver
139 * |_ dev: bus I2C0 - UCLASS_I2C
140 * | |_ dev: my_pmic (read/write) (is parent) - UCLASS_PMIC
141 * | |_ dev: my_regulator (set value/etc..) (is child) - UCLASS_REGULATOR
142 *
143 * To ensure such device relationship, the pmic device driver should also bind
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200144 * all its child devices, like in the example below. It can be done by calling
145 * the 'pmic_bind_children()' - please refer to the function description, which
146 * can be found in this header file. This function, should be called inside the
147 * driver's bind() method.
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200148 *
149 * For the example driver, please refer the MAX77686 driver:
150 * - 'drivers/power/pmic/max77686.c'
151 */
152
153/**
154 * struct dm_pmic_ops - PMIC device I/O interface
155 *
156 * Should be implemented by UCLASS_PMIC device drivers. The standard
157 * device operations provides the I/O interface for it's childs.
158 *
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200159 * @reg_count: device's register count
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200160 * @read: read 'len' bytes at "reg" and store it into the 'buffer'
161 * @write: write 'len' bytes from the 'buffer' to the register at 'reg' address
162 */
163struct dm_pmic_ops {
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200164 int (*reg_count)(struct udevice *dev);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200165 int (*read)(struct udevice *dev, uint reg, uint8_t *buffer, int len);
166 int (*write)(struct udevice *dev, uint reg, const uint8_t *buffer,
167 int len);
168};
169
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200170/**
171 * enum pmic_op_type - used for various pmic devices operation calls,
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200172 * for reduce a number of lines with the same code for read/write or get/set.
173 *
174 * @PMIC_OP_GET - get operation
175 * @PMIC_OP_SET - set operation
176*/
177enum pmic_op_type {
178 PMIC_OP_GET,
179 PMIC_OP_SET,
180};
181
182/**
183 * struct pmic_child_info - basic device's child info for bind child nodes with
184 * the driver by the node name prefix and driver name. This is a helper struct
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200185 * for function: pmic_bind_children().
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200186 *
187 * @prefix - child node name prefix (or its name if is unique or single)
188 * @driver - driver name for the sub-node with prefix
189 */
190struct pmic_child_info {
191 const char *prefix;
192 const char *driver;
193};
194
195/* drivers/power/pmic-uclass.c */
196
197/**
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200198 * pmic_bind_children() - bind drivers for given parent pmic, using child info
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200199 * found in 'child_info' array.
200 *
201 * @pmic - pmic device - the parent of found child's
202 * @child_info - N-childs info array
203 * @return a positive number of childs, or 0 if no child found (error)
204 *
205 * Note: For N-childs the child_info array should have N+1 entries and the last
206 * entry prefix should be NULL - the same as for drivers compatible.
207 *
208 * For example, a single prefix info (N=1):
209 * static const struct pmic_child_info bind_info[] = {
210 * { .prefix = "ldo", .driver = "ldo_driver" },
211 * { },
212 * };
213 *
214 * This function is useful for regulator sub-nodes:
215 * my_regulator@0xa {
216 * reg = <0xa>;
217 * (pmic - bind automatically by compatible)
218 * compatible = "my_pmic";
219 * ...
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200220 * (pmic's childs - bind by pmic_bind_children())
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200221 * (nodes prefix: "ldo", driver: "my_regulator_ldo")
222 * ldo1 { ... };
223 * ldo2 { ... };
224 *
225 * (nodes prefix: "buck", driver: "my_regulator_buck")
226 * buck1 { ... };
227 * buck2 { ... };
228 * };
229 */
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200230int pmic_bind_children(struct udevice *pmic, int offset,
231 const struct pmic_child_info *child_info);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200232
233/**
234 * pmic_get: get the pmic device using its name
235 *
236 * @name - device name
237 * @devp - returned pointer to the pmic device
238 * @return 0 on success or negative value of errno.
239 *
240 * The returned devp device can be used with pmic_read/write calls
241 */
242int pmic_get(const char *name, struct udevice **devp);
243
244/**
245 * pmic_reg_count: get the pmic register count
246 *
247 * The required pmic device can be obtained by 'pmic_get()'
248 *
249 * @dev - pointer to the UCLASS_PMIC device
250 * @return register count value on success or negative value of errno.
251 */
252int pmic_reg_count(struct udevice *dev);
253
254/**
255 * pmic_read/write: read/write to the UCLASS_PMIC device
256 *
257 * The required pmic device can be obtained by 'pmic_get()'
258 *
259 * @pmic - pointer to the UCLASS_PMIC device
260 * @reg - device register offset
261 * @buffer - pointer to read/write buffer
262 * @len - byte count for read/write
263 * @return 0 on success or negative value of errno.
264 */
265int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len);
266int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len);
Simon Glass6c69c7f2015-06-23 15:39:01 -0600267
268/**
269 * pmic_reg_read() - read a PMIC register value
270 *
271 * @dev: PMIC device to read
272 * @reg: Register to read
273 * @return value read on success or negative value of errno.
274 */
275int pmic_reg_read(struct udevice *dev, uint reg);
276
277/**
278 * pmic_reg_write() - write a PMIC register value
279 *
280 * @dev: PMIC device to write
281 * @reg: Register to write
282 * @value: Value to write
283 * @return 0 on success or negative value of errno.
284 */
285int pmic_reg_write(struct udevice *dev, uint reg, uint value);
286
287/**
288 * pmic_clrsetbits() - clear and set bits in a PMIC register
289 *
290 * This reads a register, optionally clears some bits, optionally sets some
291 * bits, then writes the register.
292 *
293 * @dev: PMIC device to update
294 * @reg: Register to update
295 * @clr: Bit mask to clear (set those bits that you want cleared)
296 * @set: Bit mask to set (set those bits that you want set)
297 * @return 0 on success or negative value of errno.
298 */
299int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set);
300
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200301#endif /* CONFIG_DM_PMIC */
302
303#ifdef CONFIG_POWER
Łukasz Majewskic7336812012-11-13 03:21:55 +0000304int pmic_init(unsigned char bus);
Tom Rini5c44dd62014-06-23 16:06:28 -0400305int power_init_board(void);
Łukasz Majewskic7336812012-11-13 03:21:55 +0000306int pmic_dialog_init(unsigned char bus);
307int check_reg(struct pmic *p, u32 reg);
308struct pmic *pmic_alloc(void);
309struct pmic *pmic_get(const char *s);
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000310int pmic_probe(struct pmic *p);
311int pmic_reg_read(struct pmic *p, u32 reg, u32 *val);
312int pmic_reg_write(struct pmic *p, u32 reg, u32 val);
313int pmic_set_output(struct pmic *p, u32 reg, int ldo, int on);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200314#endif
Łukasz Majewskie542b7f2011-10-06 02:37:34 +0000315
316#define pmic_i2c_addr (p->hw.i2c.addr)
317#define pmic_i2c_tx_num (p->hw.i2c.tx_num)
318
319#define pmic_spi_bitlen (p->hw.spi.bitlen)
320#define pmic_spi_flags (p->hw.spi.flags)
321
322#endif /* __CORE_PMIC_H_ */