blob: e340a32279fbf35445fd60ee38b78836307d434f [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
Tom Riniabb9a042024-05-18 20:20:43 -06007#include <common.h>
Keerthyf0df1b12016-09-30 09:20:43 +05308#include <fdtdec.h>
9#include <errno.h>
10#include <dm.h>
Svyatoslav Ryheleacea672023-10-24 10:49:08 +030011#include <dm/lists.h>
Keerthyf0df1b12016-09-30 09:20:43 +053012#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060014#include <linux/printk.h>
Keerthyf0df1b12016-09-30 09:20:43 +053015#include <power/pmic.h>
16#include <power/regulator.h>
17#include <power/palmas.h>
Keerthyf0df1b12016-09-30 09:20:43 +053018
Keerthyf0df1b12016-09-30 09:20:43 +053019static const struct pmic_child_info pmic_children_info[] = {
20 { .prefix = "ldo", .driver = PALMAS_LDO_DRIVER },
21 { .prefix = "smps", .driver = PALMAS_SMPS_DRIVER },
22 { },
23};
24
25static int palmas_write(struct udevice *dev, uint reg, const uint8_t *buff,
26 int len)
27{
28 if (dm_i2c_write(dev, reg, buff, len)) {
Simon Glass73126ac2018-11-18 08:14:28 -070029 pr_err("write error to device: %p register: %#x!\n", dev, reg);
Keerthyf0df1b12016-09-30 09:20:43 +053030 return -EIO;
31 }
32
33 return 0;
34}
35
36static int palmas_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
37{
38 if (dm_i2c_read(dev, reg, buff, len)) {
Simon Glass73126ac2018-11-18 08:14:28 -070039 pr_err("read error from device: %p register: %#x!\n", dev, reg);
Keerthyf0df1b12016-09-30 09:20:43 +053040 return -EIO;
41 }
42
43 return 0;
44}
45
46static int palmas_bind(struct udevice *dev)
47{
Simon Glass2c2d2c22017-05-18 20:09:32 -060048 ofnode pmic_node = ofnode_null(), regulators_node;
Svyatoslav Ryhel32648d32023-07-21 10:50:15 +030049 ofnode subnode, gpio_node;
Svyatoslav Ryheleacea672023-10-24 10:49:08 +030050 int children, ret;
51
52 if (IS_ENABLED(CONFIG_SYSRESET_PALMAS)) {
53 ret = device_bind_driver(dev, PALMAS_RST_DRIVER,
54 "sysreset", NULL);
55 if (ret) {
56 log_err("cannot bind SYSRESET (ret = %d)\n", ret);
57 return ret;
58 }
59 }
Keerthyf0df1b12016-09-30 09:20:43 +053060
Svyatoslav Ryhel32648d32023-07-21 10:50:15 +030061 gpio_node = ofnode_find_subnode(dev_ofnode(dev), "gpio");
62 if (ofnode_valid(gpio_node)) {
63 ret = device_bind_driver_to_node(dev, PALMAS_GPIO_DRIVER,
64 "gpio", gpio_node, NULL);
65 if (ret)
66 log_err("cannot bind GPIOs (ret = %d)\n", ret);
67 }
68
Simon Glass2c2d2c22017-05-18 20:09:32 -060069 dev_for_each_subnode(subnode, dev) {
Keerthyf0df1b12016-09-30 09:20:43 +053070 const char *name;
71 char *temp;
72
Simon Glass2c2d2c22017-05-18 20:09:32 -060073 name = ofnode_get_name(subnode);
Keerthyf0df1b12016-09-30 09:20:43 +053074 temp = strstr(name, "pmic");
75 if (temp) {
76 pmic_node = subnode;
77 break;
78 }
79 }
80
Simon Glass2c2d2c22017-05-18 20:09:32 -060081 if (!ofnode_valid(pmic_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -070082 debug("%s: %s pmic subnode not found!\n", __func__, dev->name);
Keerthyf0df1b12016-09-30 09:20:43 +053083 return -ENXIO;
84 }
85
Simon Glass2c2d2c22017-05-18 20:09:32 -060086 regulators_node = ofnode_find_subnode(pmic_node, "regulators");
Keerthyf0df1b12016-09-30 09:20:43 +053087
Simon Glass2c2d2c22017-05-18 20:09:32 -060088 if (!ofnode_valid(regulators_node)) {
Simon Glass73126ac2018-11-18 08:14:28 -070089 debug("%s: %s reg subnode not found!\n", __func__, dev->name);
Keerthyf0df1b12016-09-30 09:20:43 +053090 return -ENXIO;
91 }
92
93 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
94 if (!children)
95 debug("%s: %s - no child found\n", __func__, dev->name);
96
97 /* Always return success for this device */
98 return 0;
99}
100
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300101static int palmas_probe(struct udevice *dev)
102{
103 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
104 struct palmas_priv *priv = dev_get_priv(dev);
105 struct udevice *bus = dev_get_parent(dev);
106 u32 chip2_addr = chip->chip_addr + 1;
107 int ret;
108
109 /* Palmas PMIC is multi chip and chips are located in a row */
110 ret = i2c_get_chip(bus, chip2_addr, 1, &priv->chip2);
111 if (ret) {
112 log_err("cannot get second PMIC I2C chip (err %d)\n", ret);
113 return ret;
114 }
115
116 return 0;
117}
118
Keerthyf0df1b12016-09-30 09:20:43 +0530119static struct dm_pmic_ops palmas_ops = {
120 .read = palmas_read,
121 .write = palmas_write,
122};
123
124static const struct udevice_id palmas_ids[] = {
125 { .compatible = "ti,tps659038", .data = TPS659038 },
Svyatoslav Ryhelc6951c32023-10-27 11:26:08 +0300126 { .compatible = "ti,tps65913" , .data = TPS659038 },
Keerthyf0df1b12016-09-30 09:20:43 +0530127 { .compatible = "ti,tps65917" , .data = TPS65917 },
128 { }
129};
130
131U_BOOT_DRIVER(pmic_palmas) = {
132 .name = "palmas_pmic",
133 .id = UCLASS_PMIC,
134 .of_match = palmas_ids,
135 .bind = palmas_bind,
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300136 .probe = palmas_probe,
Keerthyf0df1b12016-09-30 09:20:43 +0530137 .ops = &palmas_ops,
Svyatoslav Ryheleacea672023-10-24 10:49:08 +0300138 .priv_auto = sizeof(struct palmas_priv),
Keerthyf0df1b12016-09-30 09:20:43 +0530139};