1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas RAA215300 PMIC driver
5 // Copyright (C) 2023 Renesas Electronics Corporation
9 #include <linux/clkdev.h>
10 #include <linux/clk-provider.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
15 #include <linux/regmap.h>
17 #define RAA215300_FAULT_LATCHED_STATUS_1 0x59
18 #define RAA215300_FAULT_LATCHED_STATUS_2 0x5a
19 #define RAA215300_FAULT_LATCHED_STATUS_3 0x5b
20 #define RAA215300_FAULT_LATCHED_STATUS_4 0x5c
21 #define RAA215300_FAULT_LATCHED_STATUS_6 0x5e
23 #define RAA215300_INT_MASK_1 0x64
24 #define RAA215300_INT_MASK_2 0x65
25 #define RAA215300_INT_MASK_3 0x66
26 #define RAA215300_INT_MASK_4 0x67
27 #define RAA215300_INT_MASK_6 0x68
29 #define RAA215300_REG_BLOCK_EN 0x6c
30 #define RAA215300_HW_REV 0xf8
32 #define RAA215300_INT_MASK_1_ALL GENMASK(5, 0)
33 #define RAA215300_INT_MASK_2_ALL GENMASK(3, 0)
34 #define RAA215300_INT_MASK_3_ALL GENMASK(5, 0)
35 #define RAA215300_INT_MASK_4_ALL BIT(0)
36 #define RAA215300_INT_MASK_6_ALL GENMASK(7, 0)
38 #define RAA215300_REG_BLOCK_EN_RTC_EN BIT(6)
39 #define RAA215300_RTC_DEFAULT_ADDR 0x6f
41 static const struct regmap_config raa215300_regmap_config
= {
47 static void raa215300_rtc_unregister_device(void *data
)
49 i2c_unregister_device(data
);
52 static int raa215300_clk_present(struct i2c_client
*client
, const char *name
)
56 clk
= devm_clk_get_optional(&client
->dev
, name
);
63 static int raa215300_i2c_probe(struct i2c_client
*client
)
65 struct device
*dev
= &client
->dev
;
66 const char *clkin_name
= "clkin";
67 unsigned int pmic_version
, val
;
68 const char *xin_name
= "xin";
69 const char *clk_name
= NULL
;
70 struct regmap
*regmap
;
73 regmap
= devm_regmap_init_i2c(client
, &raa215300_regmap_config
);
75 return dev_err_probe(dev
, PTR_ERR(regmap
),
76 "regmap i2c init failed\n");
78 ret
= regmap_read(regmap
, RAA215300_HW_REV
, &pmic_version
);
80 return dev_err_probe(dev
, ret
, "HW rev read failed\n");
82 dev_dbg(dev
, "RAA215300 PMIC version 0x%04x\n", pmic_version
);
84 /* Clear all blocks except RTC, if enabled */
85 regmap_read(regmap
, RAA215300_REG_BLOCK_EN
, &val
);
86 val
&= RAA215300_REG_BLOCK_EN_RTC_EN
;
87 regmap_write(regmap
, RAA215300_REG_BLOCK_EN
, val
);
89 /* Clear the latched registers */
90 regmap_read(regmap
, RAA215300_FAULT_LATCHED_STATUS_1
, &val
);
91 regmap_write(regmap
, RAA215300_FAULT_LATCHED_STATUS_1
, val
);
92 regmap_read(regmap
, RAA215300_FAULT_LATCHED_STATUS_2
, &val
);
93 regmap_write(regmap
, RAA215300_FAULT_LATCHED_STATUS_2
, val
);
94 regmap_read(regmap
, RAA215300_FAULT_LATCHED_STATUS_3
, &val
);
95 regmap_write(regmap
, RAA215300_FAULT_LATCHED_STATUS_3
, val
);
96 regmap_read(regmap
, RAA215300_FAULT_LATCHED_STATUS_4
, &val
);
97 regmap_write(regmap
, RAA215300_FAULT_LATCHED_STATUS_4
, val
);
98 regmap_read(regmap
, RAA215300_FAULT_LATCHED_STATUS_6
, &val
);
99 regmap_write(regmap
, RAA215300_FAULT_LATCHED_STATUS_6
, val
);
101 /* Mask all the PMIC interrupts */
102 regmap_write(regmap
, RAA215300_INT_MASK_1
, RAA215300_INT_MASK_1_ALL
);
103 regmap_write(regmap
, RAA215300_INT_MASK_2
, RAA215300_INT_MASK_2_ALL
);
104 regmap_write(regmap
, RAA215300_INT_MASK_3
, RAA215300_INT_MASK_3_ALL
);
105 regmap_write(regmap
, RAA215300_INT_MASK_4
, RAA215300_INT_MASK_4_ALL
);
106 regmap_write(regmap
, RAA215300_INT_MASK_6
, RAA215300_INT_MASK_6_ALL
);
108 ret
= raa215300_clk_present(client
, xin_name
);
114 ret
= raa215300_clk_present(client
, clkin_name
);
118 clk_name
= clkin_name
;
122 const char *name
= pmic_version
>= 0x12 ? "isl1208" : "raa215300_a0";
123 struct device_node
*np
= client
->dev
.of_node
;
124 u32 addr
= RAA215300_RTC_DEFAULT_ADDR
;
125 struct i2c_board_info info
= {};
126 struct i2c_client
*rtc_client
;
130 hw
= devm_clk_hw_register_fixed_rate(dev
, clk_name
, NULL
, 0, 32768);
134 ret
= devm_clk_hw_register_clkdev(dev
, hw
, clk_name
, NULL
);
136 return dev_err_probe(dev
, ret
, "Failed to initialize clkdev\n");
141 i
= of_property_match_string(np
, "reg-names", "rtc");
143 of_property_read_u32_index(np
, "reg", i
, &addr
);
148 info
.irq
= client
->irq
;
150 size
= strscpy(info
.type
, name
, sizeof(info
.type
));
152 return dev_err_probe(dev
, size
,
153 "Invalid device name: %s\n", name
);
155 /* Enable RTC block */
156 regmap_update_bits(regmap
, RAA215300_REG_BLOCK_EN
,
157 RAA215300_REG_BLOCK_EN_RTC_EN
,
158 RAA215300_REG_BLOCK_EN_RTC_EN
);
160 rtc_client
= i2c_new_client_device(client
->adapter
, &info
);
161 if (IS_ERR(rtc_client
))
162 return PTR_ERR(rtc_client
);
164 ret
= devm_add_action_or_reset(dev
,
165 raa215300_rtc_unregister_device
,
174 static const struct of_device_id raa215300_dt_match
[] = {
175 { .compatible
= "renesas,raa215300" },
178 MODULE_DEVICE_TABLE(of
, raa215300_dt_match
);
180 static struct i2c_driver raa215300_i2c_driver
= {
183 .of_match_table
= raa215300_dt_match
,
185 .probe
= raa215300_i2c_probe
,
187 module_i2c_driver(raa215300_i2c_driver
);
189 MODULE_DESCRIPTION("Renesas RAA215300 PMIC driver");
190 MODULE_AUTHOR("Fabrizio Castro <fabrizio.castro.jz@renesas.com>");
191 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
192 MODULE_LICENSE("GPL");