Axel Lin | fd2f02f | 2019-05-03 13:36:37 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // da9210-regulator.c - Regulator device driver for DA9210 |
| 4 | // Copyright (C) 2013 Dialog Semiconductor Ltd. |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 5 | |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/i2c.h> |
| 8 | #include <linux/module.h> |
Geert Uytterhoeven | 4245746 | 2015-06-24 14:14:21 +0200 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/irq.h> |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 11 | #include <linux/regulator/driver.h> |
| 12 | #include <linux/regulator/machine.h> |
Steve Twiss | 2d0cf04 | 2016-07-15 10:29:41 +0100 | [diff] [blame] | 13 | #include <linux/of_device.h> |
Guennadi Liakhovetski | 937433c | 2013-09-12 14:28:35 +0200 | [diff] [blame] | 14 | #include <linux/regulator/of_regulator.h> |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 15 | #include <linux/regmap.h> |
| 16 | |
| 17 | #include "da9210-regulator.h" |
| 18 | |
| 19 | struct da9210 { |
| 20 | struct regulator_dev *rdev; |
| 21 | struct regmap *regmap; |
| 22 | }; |
| 23 | |
| 24 | static const struct regmap_config da9210_regmap_config = { |
| 25 | .reg_bits = 8, |
| 26 | .val_bits = 8, |
| 27 | }; |
| 28 | |
Julia Lawall | 71242b4 | 2015-12-19 16:07:09 +0100 | [diff] [blame] | 29 | static const struct regulator_ops da9210_buck_ops = { |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 30 | .enable = regulator_enable_regmap, |
| 31 | .disable = regulator_disable_regmap, |
| 32 | .is_enabled = regulator_is_enabled_regmap, |
| 33 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 34 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 35 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 8b3216c4 | 2019-02-28 21:40:15 +0800 | [diff] [blame] | 36 | .set_current_limit = regulator_set_current_limit_regmap, |
| 37 | .get_current_limit = regulator_get_current_limit_regmap, |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /* Default limits measured in millivolts and milliamps */ |
| 41 | #define DA9210_MIN_MV 300 |
| 42 | #define DA9210_MAX_MV 1570 |
| 43 | #define DA9210_STEP_MV 10 |
| 44 | |
| 45 | /* Current limits for buck (uA) indices corresponds with register values */ |
Axel Lin | 8b3216c4 | 2019-02-28 21:40:15 +0800 | [diff] [blame] | 46 | static const unsigned int da9210_buck_limits[] = { |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 47 | 1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, |
| 48 | 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000 |
| 49 | }; |
| 50 | |
| 51 | static const struct regulator_desc da9210_reg = { |
| 52 | .name = "DA9210", |
| 53 | .id = 0, |
| 54 | .ops = &da9210_buck_ops, |
| 55 | .type = REGULATOR_VOLTAGE, |
| 56 | .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1, |
| 57 | .min_uV = (DA9210_MIN_MV * 1000), |
| 58 | .uV_step = (DA9210_STEP_MV * 1000), |
| 59 | .vsel_reg = DA9210_REG_VBUCK_A, |
| 60 | .vsel_mask = DA9210_VBUCK_MASK, |
| 61 | .enable_reg = DA9210_REG_BUCK_CONT, |
| 62 | .enable_mask = DA9210_BUCK_EN, |
| 63 | .owner = THIS_MODULE, |
Axel Lin | 8b3216c4 | 2019-02-28 21:40:15 +0800 | [diff] [blame] | 64 | .curr_table = da9210_buck_limits, |
| 65 | .n_current_limits = ARRAY_SIZE(da9210_buck_limits), |
| 66 | .csel_reg = DA9210_REG_BUCK_ILIM, |
| 67 | .csel_mask = DA9210_BUCK_ILIM_MASK, |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Geert Uytterhoeven | 4245746 | 2015-06-24 14:14:21 +0200 | [diff] [blame] | 70 | static irqreturn_t da9210_irq_handler(int irq, void *data) |
| 71 | { |
| 72 | struct da9210 *chip = data; |
| 73 | unsigned int val, handled = 0; |
| 74 | int error, ret = IRQ_NONE; |
| 75 | |
| 76 | error = regmap_read(chip->regmap, DA9210_REG_EVENT_B, &val); |
| 77 | if (error < 0) |
| 78 | goto error_i2c; |
| 79 | |
| 80 | if (val & DA9210_E_OVCURR) { |
| 81 | regulator_notifier_call_chain(chip->rdev, |
| 82 | REGULATOR_EVENT_OVER_CURRENT, |
| 83 | NULL); |
| 84 | handled |= DA9210_E_OVCURR; |
| 85 | } |
| 86 | if (val & DA9210_E_NPWRGOOD) { |
| 87 | regulator_notifier_call_chain(chip->rdev, |
| 88 | REGULATOR_EVENT_UNDER_VOLTAGE, |
| 89 | NULL); |
| 90 | handled |= DA9210_E_NPWRGOOD; |
| 91 | } |
| 92 | if (val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT)) { |
| 93 | regulator_notifier_call_chain(chip->rdev, |
| 94 | REGULATOR_EVENT_OVER_TEMP, NULL); |
| 95 | handled |= val & (DA9210_E_TEMP_WARN | DA9210_E_TEMP_CRIT); |
| 96 | } |
| 97 | if (val & DA9210_E_VMAX) { |
| 98 | regulator_notifier_call_chain(chip->rdev, |
| 99 | REGULATOR_EVENT_REGULATION_OUT, |
| 100 | NULL); |
| 101 | handled |= DA9210_E_VMAX; |
| 102 | } |
Wolfram Sang | 0f48eed | 2016-01-12 18:28:51 +0100 | [diff] [blame] | 103 | |
Geert Uytterhoeven | 4245746 | 2015-06-24 14:14:21 +0200 | [diff] [blame] | 104 | if (handled) { |
| 105 | /* Clear handled events */ |
| 106 | error = regmap_write(chip->regmap, DA9210_REG_EVENT_B, handled); |
| 107 | if (error < 0) |
| 108 | goto error_i2c; |
| 109 | |
| 110 | ret = IRQ_HANDLED; |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | |
| 115 | error_i2c: |
| 116 | dev_err(regmap_get_device(chip->regmap), "I2C error : %d\n", error); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 120 | /* |
| 121 | * I2C driver interface functions |
| 122 | */ |
Steve Twiss | 2d0cf04 | 2016-07-15 10:29:41 +0100 | [diff] [blame] | 123 | |
Jisheng Zhang | 2d2a84a | 2020-08-21 11:12:35 +0800 | [diff] [blame] | 124 | static const struct of_device_id __maybe_unused da9210_dt_ids[] = { |
Steve Twiss | 2d0cf04 | 2016-07-15 10:29:41 +0100 | [diff] [blame] | 125 | { .compatible = "dlg,da9210", }, |
| 126 | { } |
| 127 | }; |
| 128 | MODULE_DEVICE_TABLE(of, da9210_dt_ids); |
| 129 | |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 130 | static int da9210_i2c_probe(struct i2c_client *i2c) |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 131 | { |
| 132 | struct da9210 *chip; |
Guennadi Liakhovetski | 937433c | 2013-09-12 14:28:35 +0200 | [diff] [blame] | 133 | struct device *dev = &i2c->dev; |
| 134 | struct da9210_pdata *pdata = dev_get_platdata(dev); |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 135 | struct regulator_dev *rdev = NULL; |
| 136 | struct regulator_config config = { }; |
| 137 | int error; |
Steve Twiss | 2d0cf04 | 2016-07-15 10:29:41 +0100 | [diff] [blame] | 138 | const struct of_device_id *match; |
| 139 | |
| 140 | if (i2c->dev.of_node && !pdata) { |
| 141 | match = of_match_device(of_match_ptr(da9210_dt_ids), |
| 142 | &i2c->dev); |
| 143 | if (!match) { |
| 144 | dev_err(&i2c->dev, "Error: No device match found\n"); |
| 145 | return -ENODEV; |
| 146 | } |
| 147 | } |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 148 | |
| 149 | chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL); |
Sachin Kamat | 6c48acf | 2014-02-20 14:23:02 +0530 | [diff] [blame] | 150 | if (!chip) |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 151 | return -ENOMEM; |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 152 | |
| 153 | chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config); |
| 154 | if (IS_ERR(chip->regmap)) { |
| 155 | error = PTR_ERR(chip->regmap); |
| 156 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 157 | error); |
| 158 | return error; |
| 159 | } |
| 160 | |
| 161 | config.dev = &i2c->dev; |
Guennadi Liakhovetski | 937433c | 2013-09-12 14:28:35 +0200 | [diff] [blame] | 162 | config.init_data = pdata ? &pdata->da9210_constraints : |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 163 | of_get_regulator_init_data(dev, dev->of_node, &da9210_reg); |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 164 | config.driver_data = chip; |
| 165 | config.regmap = chip->regmap; |
Guennadi Liakhovetski | 937433c | 2013-09-12 14:28:35 +0200 | [diff] [blame] | 166 | config.of_node = dev->of_node; |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 167 | |
Geert Uytterhoeven | 4d9cbff5 | 2015-02-17 13:11:11 +0100 | [diff] [blame] | 168 | /* Mask all interrupt sources to deassert interrupt line */ |
| 169 | error = regmap_write(chip->regmap, DA9210_REG_MASK_A, ~0); |
| 170 | if (!error) |
| 171 | error = regmap_write(chip->regmap, DA9210_REG_MASK_B, ~0); |
| 172 | if (error) { |
| 173 | dev_err(&i2c->dev, "Failed to write to mask reg: %d\n", error); |
| 174 | return error; |
| 175 | } |
| 176 | |
Jingoo Han | 11c0da7 | 2013-09-30 09:53:57 +0900 | [diff] [blame] | 177 | rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config); |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 178 | if (IS_ERR(rdev)) { |
| 179 | dev_err(&i2c->dev, "Failed to register DA9210 regulator\n"); |
| 180 | return PTR_ERR(rdev); |
| 181 | } |
| 182 | |
| 183 | chip->rdev = rdev; |
Geert Uytterhoeven | 4245746 | 2015-06-24 14:14:21 +0200 | [diff] [blame] | 184 | if (i2c->irq) { |
| 185 | error = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, |
| 186 | da9210_irq_handler, |
| 187 | IRQF_TRIGGER_LOW | |
| 188 | IRQF_ONESHOT | IRQF_SHARED, |
| 189 | "da9210", chip); |
| 190 | if (error) { |
| 191 | dev_err(&i2c->dev, "Failed to request IRQ%u: %d\n", |
| 192 | i2c->irq, error); |
| 193 | return error; |
| 194 | } |
| 195 | |
| 196 | error = regmap_update_bits(chip->regmap, DA9210_REG_MASK_B, |
| 197 | DA9210_M_OVCURR | DA9210_M_NPWRGOOD | |
| 198 | DA9210_M_TEMP_WARN | |
| 199 | DA9210_M_TEMP_CRIT | DA9210_M_VMAX, 0); |
| 200 | if (error < 0) { |
| 201 | dev_err(&i2c->dev, "Failed to update mask reg: %d\n", |
| 202 | error); |
| 203 | return error; |
| 204 | } |
| 205 | } else { |
| 206 | dev_warn(&i2c->dev, "No IRQ configured\n"); |
| 207 | } |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 208 | |
| 209 | i2c_set_clientdata(i2c, chip); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 214 | static const struct i2c_device_id da9210_i2c_id[] = { |
| 215 | {"da9210", 0}, |
| 216 | {}, |
| 217 | }; |
| 218 | |
| 219 | MODULE_DEVICE_TABLE(i2c, da9210_i2c_id); |
| 220 | |
| 221 | static struct i2c_driver da9210_regulator_driver = { |
| 222 | .driver = { |
| 223 | .name = "da9210", |
Steve Twiss | 2d0cf04 | 2016-07-15 10:29:41 +0100 | [diff] [blame] | 224 | .of_match_table = of_match_ptr(da9210_dt_ids), |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 225 | }, |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 226 | .probe_new = da9210_i2c_probe, |
Steve Twiss | 16f1091 | 2013-08-06 15:30:48 +0100 | [diff] [blame] | 227 | .id_table = da9210_i2c_id, |
| 228 | }; |
| 229 | |
| 230 | module_i2c_driver(da9210_regulator_driver); |
| 231 | |
| 232 | MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>"); |
| 233 | MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210"); |
| 234 | MODULE_LICENSE("GPL v2"); |