1 /* 2 * ROHM BD9571MWV-M MFD driver 3 * 4 * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 11 * kind, whether expressed or implied; without even the implied warranty 12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License version 2 for more details. 14 * 15 * Based on the TPS65086 driver 16 */ 17 18 #include <linux/i2c.h> 19 #include <linux/interrupt.h> 20 #include <linux/mfd/core.h> 21 #include <linux/module.h> 22 23 #include <linux/mfd/bd9571mwv.h> 24 25 static const struct mfd_cell bd9571mwv_cells[] = { 26 { .name = "bd9571mwv-regulator", }, 27 { .name = "bd9571mwv-gpio", }, 28 }; 29 30 static const struct regmap_range bd9571mwv_readable_yes_ranges[] = { 31 regmap_reg_range(BD9571MWV_VENDOR_CODE, BD9571MWV_PRODUCT_REVISION), 32 regmap_reg_range(BD9571MWV_BKUP_MODE_CNT, BD9571MWV_BKUP_MODE_CNT), 33 regmap_reg_range(BD9571MWV_AVS_SET_MONI, BD9571MWV_AVS_DVFS_VID(3)), 34 regmap_reg_range(BD9571MWV_VD18_VID, BD9571MWV_VD33_VID), 35 regmap_reg_range(BD9571MWV_DVFS_VINIT, BD9571MWV_DVFS_VINIT), 36 regmap_reg_range(BD9571MWV_DVFS_SETVMAX, BD9571MWV_DVFS_MONIVDAC), 37 regmap_reg_range(BD9571MWV_GPIO_IN, BD9571MWV_GPIO_IN), 38 regmap_reg_range(BD9571MWV_GPIO_INT, BD9571MWV_GPIO_INTMASK), 39 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTMASK), 40 }; 41 42 static const struct regmap_access_table bd9571mwv_readable_table = { 43 .yes_ranges = bd9571mwv_readable_yes_ranges, 44 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_readable_yes_ranges), 45 }; 46 47 static const struct regmap_range bd9571mwv_writable_yes_ranges[] = { 48 regmap_reg_range(BD9571MWV_BKUP_MODE_CNT, BD9571MWV_BKUP_MODE_CNT), 49 regmap_reg_range(BD9571MWV_AVS_VD09_VID(0), BD9571MWV_AVS_VD09_VID(3)), 50 regmap_reg_range(BD9571MWV_DVFS_SETVID, BD9571MWV_DVFS_SETVID), 51 regmap_reg_range(BD9571MWV_GPIO_DIR, BD9571MWV_GPIO_OUT), 52 regmap_reg_range(BD9571MWV_GPIO_INT_SET, BD9571MWV_GPIO_INTMASK), 53 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTMASK), 54 }; 55 56 static const struct regmap_access_table bd9571mwv_writable_table = { 57 .yes_ranges = bd9571mwv_writable_yes_ranges, 58 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_writable_yes_ranges), 59 }; 60 61 static const struct regmap_range bd9571mwv_volatile_yes_ranges[] = { 62 regmap_reg_range(BD9571MWV_DVFS_MONIVDAC, BD9571MWV_DVFS_MONIVDAC), 63 regmap_reg_range(BD9571MWV_GPIO_IN, BD9571MWV_GPIO_IN), 64 regmap_reg_range(BD9571MWV_GPIO_INT, BD9571MWV_GPIO_INT), 65 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTREQ), 66 }; 67 68 static const struct regmap_access_table bd9571mwv_volatile_table = { 69 .yes_ranges = bd9571mwv_volatile_yes_ranges, 70 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_volatile_yes_ranges), 71 }; 72 73 static const struct regmap_config bd9571mwv_regmap_config = { 74 .reg_bits = 8, 75 .val_bits = 8, 76 .cache_type = REGCACHE_RBTREE, 77 .rd_table = &bd9571mwv_readable_table, 78 .wr_table = &bd9571mwv_writable_table, 79 .volatile_table = &bd9571mwv_volatile_table, 80 .max_register = 0xff, 81 }; 82 83 static const struct regmap_irq bd9571mwv_irqs[] = { 84 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD1, 0, 85 BD9571MWV_INT_INTREQ_MD1_INT), 86 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD2_E1, 0, 87 BD9571MWV_INT_INTREQ_MD2_E1_INT), 88 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD2_E2, 0, 89 BD9571MWV_INT_INTREQ_MD2_E2_INT), 90 REGMAP_IRQ_REG(BD9571MWV_IRQ_PROT_ERR, 0, 91 BD9571MWV_INT_INTREQ_PROT_ERR_INT), 92 REGMAP_IRQ_REG(BD9571MWV_IRQ_GP, 0, 93 BD9571MWV_INT_INTREQ_GP_INT), 94 REGMAP_IRQ_REG(BD9571MWV_IRQ_128H_OF, 0, 95 BD9571MWV_INT_INTREQ_128H_OF_INT), 96 REGMAP_IRQ_REG(BD9571MWV_IRQ_WDT_OF, 0, 97 BD9571MWV_INT_INTREQ_WDT_OF_INT), 98 REGMAP_IRQ_REG(BD9571MWV_IRQ_BKUP_TRG, 0, 99 BD9571MWV_INT_INTREQ_BKUP_TRG_INT), 100 }; 101 102 static struct regmap_irq_chip bd9571mwv_irq_chip = { 103 .name = "bd9571mwv", 104 .status_base = BD9571MWV_INT_INTREQ, 105 .mask_base = BD9571MWV_INT_INTMASK, 106 .ack_base = BD9571MWV_INT_INTREQ, 107 .init_ack_masked = true, 108 .num_regs = 1, 109 .irqs = bd9571mwv_irqs, 110 .num_irqs = ARRAY_SIZE(bd9571mwv_irqs), 111 }; 112 113 static int bd9571mwv_identify(struct bd9571mwv *bd) 114 { 115 struct device *dev = bd->dev; 116 unsigned int value; 117 int ret; 118 119 ret = regmap_read(bd->regmap, BD9571MWV_VENDOR_CODE, &value); 120 if (ret) { 121 dev_err(dev, "Failed to read vendor code register (ret=%i)\n", 122 ret); 123 return ret; 124 } 125 126 if (value != BD9571MWV_VENDOR_CODE_VAL) { 127 dev_err(dev, "Invalid vendor code ID %02x (expected %02x)\n", 128 value, BD9571MWV_VENDOR_CODE_VAL); 129 return -EINVAL; 130 } 131 132 ret = regmap_read(bd->regmap, BD9571MWV_PRODUCT_CODE, &value); 133 if (ret) { 134 dev_err(dev, "Failed to read product code register (ret=%i)\n", 135 ret); 136 return ret; 137 } 138 139 if (value != BD9571MWV_PRODUCT_CODE_VAL) { 140 dev_err(dev, "Invalid product code ID %02x (expected %02x)\n", 141 value, BD9571MWV_PRODUCT_CODE_VAL); 142 return -EINVAL; 143 } 144 145 ret = regmap_read(bd->regmap, BD9571MWV_PRODUCT_REVISION, &value); 146 if (ret) { 147 dev_err(dev, "Failed to read revision register (ret=%i)\n", 148 ret); 149 return ret; 150 } 151 152 dev_info(dev, "Device: BD9571MWV rev. %d\n", value & 0xff); 153 154 return 0; 155 } 156 157 static int bd9571mwv_probe(struct i2c_client *client, 158 const struct i2c_device_id *ids) 159 { 160 struct bd9571mwv *bd; 161 int ret; 162 163 bd = devm_kzalloc(&client->dev, sizeof(*bd), GFP_KERNEL); 164 if (!bd) 165 return -ENOMEM; 166 167 i2c_set_clientdata(client, bd); 168 bd->dev = &client->dev; 169 bd->irq = client->irq; 170 171 bd->regmap = devm_regmap_init_i2c(client, &bd9571mwv_regmap_config); 172 if (IS_ERR(bd->regmap)) { 173 dev_err(bd->dev, "Failed to initialize register map\n"); 174 return PTR_ERR(bd->regmap); 175 } 176 177 ret = bd9571mwv_identify(bd); 178 if (ret) 179 return ret; 180 181 ret = regmap_add_irq_chip(bd->regmap, bd->irq, IRQF_ONESHOT, 0, 182 &bd9571mwv_irq_chip, &bd->irq_data); 183 if (ret) { 184 dev_err(bd->dev, "Failed to register IRQ chip\n"); 185 return ret; 186 } 187 188 ret = mfd_add_devices(bd->dev, PLATFORM_DEVID_AUTO, bd9571mwv_cells, 189 ARRAY_SIZE(bd9571mwv_cells), NULL, 0, 190 regmap_irq_get_domain(bd->irq_data)); 191 if (ret) { 192 regmap_del_irq_chip(bd->irq, bd->irq_data); 193 return ret; 194 } 195 196 return 0; 197 } 198 199 static int bd9571mwv_remove(struct i2c_client *client) 200 { 201 struct bd9571mwv *bd = i2c_get_clientdata(client); 202 203 regmap_del_irq_chip(bd->irq, bd->irq_data); 204 205 return 0; 206 } 207 208 static const struct of_device_id bd9571mwv_of_match_table[] = { 209 { .compatible = "rohm,bd9571mwv", }, 210 { /* sentinel */ } 211 }; 212 MODULE_DEVICE_TABLE(of, bd9571mwv_of_match_table); 213 214 static const struct i2c_device_id bd9571mwv_id_table[] = { 215 { "bd9571mwv", 0 }, 216 { /* sentinel */ } 217 }; 218 MODULE_DEVICE_TABLE(i2c, bd9571mwv_id_table); 219 220 static struct i2c_driver bd9571mwv_driver = { 221 .driver = { 222 .name = "bd9571mwv", 223 .of_match_table = bd9571mwv_of_match_table, 224 }, 225 .probe = bd9571mwv_probe, 226 .remove = bd9571mwv_remove, 227 .id_table = bd9571mwv_id_table, 228 }; 229 module_i2c_driver(bd9571mwv_driver); 230 231 MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>"); 232 MODULE_DESCRIPTION("BD9571MWV PMIC Driver"); 233 MODULE_LICENSE("GPL v2"); 234