1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Renesas RAA215300 PMIC driver 4 // 5 // Copyright (C) 2023 Renesas Electronics Corporation 6 // 7 8 #include <linux/clk.h> 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> 14 #include <linux/of.h> 15 #include <linux/regmap.h> 16 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 22 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 28 29 #define RAA215300_REG_BLOCK_EN 0x6c 30 #define RAA215300_HW_REV 0xf8 31 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) 37 38 #define RAA215300_REG_BLOCK_EN_RTC_EN BIT(6) 39 #define RAA215300_RTC_DEFAULT_ADDR 0x6f 40 41 static struct clk *clk; 42 43 static const struct regmap_config raa215300_regmap_config = { 44 .reg_bits = 8, 45 .val_bits = 8, 46 .max_register = 0xff, 47 }; 48 49 static void raa215300_rtc_unregister_device(void *data) 50 { 51 i2c_unregister_device(data); 52 if (!clk) { 53 clk_unregister_fixed_rate(clk); 54 clk = NULL; 55 } 56 } 57 58 static int raa215300_clk_present(struct i2c_client *client, const char *name) 59 { 60 struct clk *clk; 61 62 clk = devm_clk_get_optional(&client->dev, name); 63 if (IS_ERR(clk)) 64 return PTR_ERR(clk); 65 66 return !!clk; 67 } 68 69 static int raa215300_i2c_probe(struct i2c_client *client) 70 { 71 struct device *dev = &client->dev; 72 const char *clkin_name = "clkin"; 73 unsigned int pmic_version, val; 74 const char *xin_name = "xin"; 75 const char *clk_name = NULL; 76 struct regmap *regmap; 77 int ret; 78 79 regmap = devm_regmap_init_i2c(client, &raa215300_regmap_config); 80 if (IS_ERR(regmap)) 81 return dev_err_probe(dev, PTR_ERR(regmap), 82 "regmap i2c init failed\n"); 83 84 ret = regmap_read(regmap, RAA215300_HW_REV, &pmic_version); 85 if (ret < 0) 86 return dev_err_probe(dev, ret, "HW rev read failed\n"); 87 88 dev_dbg(dev, "RAA215300 PMIC version 0x%04x\n", pmic_version); 89 90 /* Clear all blocks except RTC, if enabled */ 91 regmap_read(regmap, RAA215300_REG_BLOCK_EN, &val); 92 val &= RAA215300_REG_BLOCK_EN_RTC_EN; 93 regmap_write(regmap, RAA215300_REG_BLOCK_EN, val); 94 95 /*Clear the latched registers */ 96 regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_1, &val); 97 regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_1, val); 98 regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_2, &val); 99 regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_2, val); 100 regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_3, &val); 101 regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_3, val); 102 regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_4, &val); 103 regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_4, val); 104 regmap_read(regmap, RAA215300_FAULT_LATCHED_STATUS_6, &val); 105 regmap_write(regmap, RAA215300_FAULT_LATCHED_STATUS_6, val); 106 107 /* Mask all the PMIC interrupts */ 108 regmap_write(regmap, RAA215300_INT_MASK_1, RAA215300_INT_MASK_1_ALL); 109 regmap_write(regmap, RAA215300_INT_MASK_2, RAA215300_INT_MASK_2_ALL); 110 regmap_write(regmap, RAA215300_INT_MASK_3, RAA215300_INT_MASK_3_ALL); 111 regmap_write(regmap, RAA215300_INT_MASK_4, RAA215300_INT_MASK_4_ALL); 112 regmap_write(regmap, RAA215300_INT_MASK_6, RAA215300_INT_MASK_6_ALL); 113 114 ret = raa215300_clk_present(client, xin_name); 115 if (ret < 0) { 116 return ret; 117 } else if (ret) { 118 clk_name = xin_name; 119 } else { 120 ret = raa215300_clk_present(client, clkin_name); 121 if (ret < 0) 122 return ret; 123 if (ret) 124 clk_name = clkin_name; 125 } 126 127 if (clk_name) { 128 char *name = pmic_version >= 0x12 ? "isl1208" : "raa215300_a0"; 129 struct device_node *np = client->dev.of_node; 130 u32 addr = RAA215300_RTC_DEFAULT_ADDR; 131 struct i2c_board_info info = {}; 132 struct i2c_client *rtc_client; 133 ssize_t size; 134 135 clk = clk_register_fixed_rate(NULL, clk_name, NULL, 0, 32000); 136 clk_register_clkdev(clk, clk_name, NULL); 137 138 if (np) { 139 int i; 140 141 i = of_property_match_string(np, "reg-names", "rtc"); 142 if (i >= 0) 143 of_property_read_u32_index(np, "reg", i, &addr); 144 } 145 146 info.addr = addr; 147 if (client->irq > 0) 148 info.irq = client->irq; 149 150 size = strscpy(info.type, name, sizeof(info.type)); 151 if (size < 0) 152 return dev_err_probe(dev, size, 153 "Invalid device name: %s\n", name); 154 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); 159 160 rtc_client = i2c_new_client_device(client->adapter, &info); 161 if (IS_ERR(rtc_client)) 162 return PTR_ERR(rtc_client); 163 164 ret = devm_add_action_or_reset(dev, 165 raa215300_rtc_unregister_device, 166 rtc_client); 167 if (ret < 0) 168 return ret; 169 } 170 171 return 0; 172 } 173 174 static const struct of_device_id raa215300_dt_match[] = { 175 { .compatible = "renesas,raa215300" }, 176 { /* sentinel */ } 177 }; 178 MODULE_DEVICE_TABLE(of, raa215300_dt_match); 179 180 static struct i2c_driver raa215300_i2c_driver = { 181 .driver = { 182 .name = "raa215300", 183 .of_match_table = raa215300_dt_match, 184 }, 185 .probe_new = raa215300_i2c_probe, 186 }; 187 module_i2c_driver(raa215300_i2c_driver); 188 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"); 193