blob: f676bf641694702ef0af739b0e688d964f4f1934 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Keerthyf0df1b12016-09-30 09:20:43 +05302/*
3 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
4 * Keerthy <j-keerthy@ti.com>
Keerthyf0df1b12016-09-30 09:20:43 +05305 */
6
Keerthyf0df1b12016-09-30 09:20:43 +05307#include <fdtdec.h>
8#include <errno.h>
9#include <dm.h>
Svyatoslav Ryheleacea672023-10-24 10:49:08 +030010#include <dm/lists.h>
Keerthyf0df1b12016-09-30 09:20:43 +053011#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060013#include <linux/printk.h>
Keerthyf0df1b12016-09-30 09:20:43 +053014#include <power/pmic.h>
15#include <power/regulator.h>
16#include <power/palmas.h>
Keerthyf0df1b12016-09-30 09:20:43 +053017
Keerthyf0df1b12016-09-30 09:20:43 +053018static const struct pmic_child_info pmic_children_info[] = {
19 { .prefix = "ldo", .driver = PALMAS_LDO_DRIVER },
20 { .prefix = "smps", .driver = PALMAS_SMPS_DRIVER },
21 { },
22};
23
24static int palmas_write(struct udevice *dev, uint reg, const uint8_t *buff,
25 int len)
26{
27 if (dm_i2c_write(dev, reg, buff, len)) {
Simon Glass73126ac2018-11-18 08:14:28 -070028 pr_err("write error to device: %p register: %#x!\n", dev, reg);
Keerthyf0df1b12016-09-30 09:20:43 +053029 return -EIO;
30 }
31
32 return 0;
33}
34
35static int palmas_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
36{
37 if (dm_i2c_read(dev, reg, buff, len)) {
Simon Glass73126ac2018-11-18 08:14:28 -070038 pr_err("read error from device: %p register: %#x!\n", dev, reg);
Keerthyf0df1b12016-09-30 09:20:43 +053039 return -EIO;
40 }
41
42 return 0;
43}
44
45static int palmas_bind(struct udevice *dev)
46{
Simon Glass2c2d2c22017-05-18 20:09:32 -060047 ofnode pmic_node = ofnode_null(), regulators_node;
Svyatoslav Ryhel32648d32023-07-21 10:50:15 +030048 ofnode subnode, gpio_node;
Svyatoslav Ryheleacea672023-10-24 10:49:08 +030049 int children, ret;
50
51 if (IS_ENABLED(CONFIG_SYSRESET_PALMAS)) {
52 ret = device_bind_driver(dev, PALMAS_RST_DRIVER,
53 "sysreset", NULL);
54 if (ret) {
55 log_err("cannot bind SYSRESET (ret = %d)\n", ret);
56 return ret;
57 }
58 }
Keerthyf0df1b12016-09-30 09:20:43 +053059
Svyatoslav Ryhel32648d32023-07-21 10:50:15 +030060 gpio_node = ofnode_find_subnode(dev_ofnode(dev), "gpio");
61 if (ofnode_valid(gpio_node)) {
62 ret = device_bind_driver_to_node(dev, PALMAS_GPIO_DRIVER,
63 "gpio", gpio_node, NULL);
64 if (ret)
65 log_err("cannot bind GPIOs (ret = %d)\n", ret);
66 }
67
Simon Glass2c2d2c22017-05-18 20:09:32 -060068 dev_for_each_subnode(subnode, dev) {
Keerthyf0df1b12016-09-30 09:20:43 +053069 const char *name;
70 char *temp;
71
Simon Glass2c2d2c22017-05-18 20:09:32 -060072 name = ofnode_get_name(subnode);
Keerthyf0df1b12016-09-30 09:20:43 +053073 temp = strstr(name, "pmic");
74 if (temp) {
75 pmic_node = subnode;
76 break;
77 }
78 }
79
Simon Glass2c2d2c22017-05-18 20:09:32 -060080 if (!ofnode_valid(pmic_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -070081 debug("%s: %s pmic subnode not found!\n", __func__, dev->name);
Keerthyf0df1b12016-09-30 09:20:43 +053082 return -ENXIO;
83 }
84
Simon Glass2c2d2c22017-05-18 20:09:32 -060085 regulators_node = ofnode_find_subnode(pmic_node, "regulators");
Keerthyf0df1b12016-09-30 09:20:43 +053086
Simon Glass2c2d2c22017-05-18 20:09:32 -060087 if (!ofnode_valid(regulators_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -070088 debug("%s: %s reg subnode not found!\n", __func__, dev->name);
Keerthyf0df1b12016-09-30 09:20:43 +053089 return -ENXIO;
90 }
91
92 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
93 if (!children)
94 debug("%s: %s - no child found\n", __func__, dev->name);
95
96 /* Always return success for this device */
97 return 0;
98}
99
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300100static int palmas_probe(struct udevice *dev)
101{
102 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
103 struct palmas_priv *priv = dev_get_priv(dev);
104 struct udevice *bus = dev_get_parent(dev);
105 u32 chip2_addr = chip->chip_addr + 1;
106 int ret;
107
108 /* Palmas PMIC is multi chip and chips are located in a row */
109 ret = i2c_get_chip(bus, chip2_addr, 1, &priv->chip2);
110 if (ret) {
111 log_err("cannot get second PMIC I2C chip (err %d)\n", ret);
112 return ret;
113 }
114
115 return 0;
116}
117
Keerthyf0df1b12016-09-30 09:20:43 +0530118static struct dm_pmic_ops palmas_ops = {
119 .read = palmas_read,
120 .write = palmas_write,
121};
122
123static const struct udevice_id palmas_ids[] = {
124 { .compatible = "ti,tps659038", .data = TPS659038 },
Svyatoslav Ryhelc6951c32023-10-27 11:26:08 +0300125 { .compatible = "ti,tps65913" , .data = TPS659038 },
Keerthyf0df1b12016-09-30 09:20:43 +0530126 { .compatible = "ti,tps65917" , .data = TPS65917 },
127 { }
128};
129
130U_BOOT_DRIVER(pmic_palmas) = {
131 .name = "palmas_pmic",
132 .id = UCLASS_PMIC,
133 .of_match = palmas_ids,
134 .bind = palmas_bind,
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300135 .probe = palmas_probe,
Keerthyf0df1b12016-09-30 09:20:43 +0530136 .ops = &palmas_ops,
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300137 .priv_auto = sizeof(struct palmas_priv),
Keerthyf0df1b12016-09-30 09:20:43 +0530138};