1 /* 2 * Copyright (C) 2014-2015 Samsung Electronics 3 * Przemyslaw Marczak <p.marczak@samsung.com> 4 * 5 * Copyright (C) 2011-2012 Samsung Electronics 6 * Lukasz Majewski <l.majewski@samsung.com> 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #ifndef __CORE_PMIC_H_ 12 #define __CORE_PMIC_H_ 13 14 #include <dm/ofnode.h> 15 #include <i2c.h> 16 #include <linux/list.h> 17 #include <power/power_chrg.h> 18 19 enum { PMIC_I2C, PMIC_SPI, PMIC_NONE}; 20 21 #ifdef CONFIG_POWER 22 enum { I2C_PMIC, I2C_NUM, }; 23 enum { PMIC_READ, PMIC_WRITE, }; 24 enum { PMIC_SENSOR_BYTE_ORDER_LITTLE, PMIC_SENSOR_BYTE_ORDER_BIG, }; 25 26 enum { 27 PMIC_CHARGER_DISABLE, 28 PMIC_CHARGER_ENABLE, 29 }; 30 31 struct p_i2c { 32 unsigned char addr; 33 unsigned char *buf; 34 unsigned char tx_num; 35 }; 36 37 struct 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 46 struct pmic; 47 struct 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 52 struct 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 58 struct 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 67 struct pmic { 68 const char *name; 69 unsigned char bus; 70 unsigned char interface; 71 unsigned char sensor_byte_order; 72 unsigned int number_of_regs; 73 union hw { 74 struct p_i2c i2c; 75 struct p_spi spi; 76 } hw; 77 78 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; 84 struct list_head list; 85 }; 86 #endif /* CONFIG_POWER */ 87 88 #ifdef CONFIG_DM_PMIC 89 /** 90 * U-Boot PMIC Framework 91 * ===================== 92 * 93 * UCLASS_PMIC - This is designed to provide an I/O interface for PMIC devices. 94 * 95 * For the multi-function PMIC devices, this can be used as parent I/O device 96 * for each IC's interface. Then, each child uses its parent for read/write. 97 * 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 115 * 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, 117 * 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 * 134 * If a PMIC child device driver needs access the PMIC-specific registers, 135 * 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 144 * 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. 148 * 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 * 159 * @reg_count: device's register count 160 * @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 */ 163 struct dm_pmic_ops { 164 int (*reg_count)(struct udevice *dev); 165 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 int (*shutdown)(struct udevice *dev); 169 }; 170 171 /** 172 * enum pmic_op_type - used for various pmic devices operation calls, 173 * for reduce a number of lines with the same code for read/write or get/set. 174 * 175 * @PMIC_OP_GET - get operation 176 * @PMIC_OP_SET - set operation 177 */ 178 enum pmic_op_type { 179 PMIC_OP_GET, 180 PMIC_OP_SET, 181 }; 182 183 /** 184 * struct pmic_child_info - basic device's child info for bind child nodes with 185 * the driver by the node name prefix and driver name. This is a helper struct 186 * for function: pmic_bind_children(). 187 * 188 * @prefix - child node name prefix (or its name if is unique or single) 189 * @driver - driver name for the sub-node with prefix 190 */ 191 struct pmic_child_info { 192 const char *addr; 193 const char *prefix; 194 const char *driver; 195 }; 196 197 /* drivers/power/pmic-uclass.c */ 198 199 /** 200 * pmic_bind_children() - bind drivers for given parent pmic, using child info 201 * found in 'child_info' array. 202 * 203 * @pmic - pmic device - the parent of found child's 204 * @child_info - N-childs info array 205 * @return a positive number of childs, or 0 if no child found (error) 206 * 207 * Note: For N-childs the child_info array should have N+1 entries and the last 208 * entry prefix should be NULL - the same as for drivers compatible. 209 * 210 * For example, a single prefix info (N=1): 211 * static const struct pmic_child_info bind_info[] = { 212 * { .prefix = "ldo", .driver = "ldo_driver" }, 213 * { }, 214 * }; 215 * 216 * This function is useful for regulator sub-nodes: 217 * my_regulator@0xa { 218 * reg = <0xa>; 219 * (pmic - bind automatically by compatible) 220 * compatible = "my_pmic"; 221 * ... 222 * (pmic's childs - bind by pmic_bind_children()) 223 * (nodes prefix: "ldo", driver: "my_regulator_ldo") 224 * ldo1 { ... }; 225 * ldo2 { ... }; 226 * 227 * (nodes prefix: "buck", driver: "my_regulator_buck") 228 * buck1 { ... }; 229 * buck2 { ... }; 230 * }; 231 */ 232 int pmic_bind_children(struct udevice *pmic, ofnode parent, 233 const struct pmic_child_info *child_info); 234 235 /** 236 * pmic_get: get the pmic device using its name 237 * 238 * @name - device name 239 * @devp - returned pointer to the pmic device 240 * @return 0 on success or negative value of errno. 241 * 242 * The returned devp device can be used with pmic_read/write calls 243 */ 244 int pmic_get(const char *name, struct udevice **devp); 245 246 /** 247 * pmic_reg_count: get the pmic register count 248 * 249 * The required pmic device can be obtained by 'pmic_get()' 250 * 251 * @dev - pointer to the UCLASS_PMIC device 252 * @return register count value on success or negative value of errno. 253 */ 254 int pmic_reg_count(struct udevice *dev); 255 256 /** 257 * pmic_read/write: read/write to the UCLASS_PMIC device 258 * 259 * The required pmic device can be obtained by 'pmic_get()' 260 * 261 * @pmic - pointer to the UCLASS_PMIC device 262 * @reg - device register offset 263 * @buffer - pointer to read/write buffer 264 * @len - byte count for read/write 265 * @return 0 on success or negative value of errno. 266 */ 267 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len); 268 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len); 269 270 /** 271 * pmic_reg_read() - read a PMIC register value 272 * 273 * @dev: PMIC device to read 274 * @reg: Register to read 275 * @return value read on success or negative value of errno. 276 */ 277 int pmic_reg_read(struct udevice *dev, uint reg); 278 279 /** 280 * pmic_reg_write() - write a PMIC register value 281 * 282 * @dev: PMIC device to write 283 * @reg: Register to write 284 * @value: Value to write 285 * @return 0 on success or negative value of errno. 286 */ 287 int pmic_reg_write(struct udevice *dev, uint reg, uint value); 288 289 /** 290 * pmic_clrsetbits() - clear and set bits in a PMIC register 291 * 292 * This reads a register, optionally clears some bits, optionally sets some 293 * bits, then writes the register. 294 * 295 * @dev: PMIC device to update 296 * @reg: Register to update 297 * @clr: Bit mask to clear (set those bits that you want cleared) 298 * @set: Bit mask to set (set those bits that you want set) 299 * @return 0 on success or negative value of errno. 300 */ 301 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set); 302 303 /** 304 * pmic_shutdown() - power off supplies of PMIC 305 * 306 * @dev: PMIC device to update 307 * @return 0 on success or negative value of errno. 308 */ 309 int pmic_shutdown(struct udevice *dev); 310 311 #endif /* CONFIG_DM_PMIC */ 312 313 #ifdef CONFIG_POWER 314 int pmic_init(unsigned char bus); 315 int power_init_board(void); 316 int pmic_dialog_init(unsigned char bus); 317 int check_reg(struct pmic *p, u32 reg); 318 struct pmic *pmic_alloc(void); 319 struct pmic *pmic_get(const char *s); 320 int pmic_probe(struct pmic *p); 321 int pmic_reg_read(struct pmic *p, u32 reg, u32 *val); 322 int pmic_reg_write(struct pmic *p, u32 reg, u32 val); 323 int pmic_set_output(struct pmic *p, u32 reg, int ldo, int on); 324 #endif 325 326 #define pmic_i2c_addr (p->hw.i2c.addr) 327 #define pmic_i2c_tx_num (p->hw.i2c.tx_num) 328 329 #define pmic_spi_bitlen (p->hw.spi.bitlen) 330 #define pmic_spi_flags (p->hw.spi.flags) 331 332 #endif /* __CORE_PMIC_H_ */ 333