1 /* 2 * TI LMU (Lighting Management Unit) Core Driver 3 * 4 * Copyright 2017 Texas Instruments 5 * 6 * Author: Milo Kim <milo.kim@ti.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/delay.h> 14 #include <linux/err.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/i2c.h> 17 #include <linux/kernel.h> 18 #include <linux/mfd/core.h> 19 #include <linux/mfd/ti-lmu.h> 20 #include <linux/mfd/ti-lmu-register.h> 21 #include <linux/module.h> 22 #include <linux/of.h> 23 #include <linux/of_device.h> 24 #include <linux/slab.h> 25 26 struct ti_lmu_data { 27 const struct mfd_cell *cells; 28 int num_cells; 29 unsigned int max_register; 30 }; 31 32 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id) 33 { 34 if (lmu->en_gpio) 35 gpiod_set_value(lmu->en_gpio, 1); 36 37 /* Delay about 1ms after HW enable pin control */ 38 usleep_range(1000, 1500); 39 40 /* LM3631 has additional power up sequence - enable LCD_EN bit. */ 41 if (id == LM3631) { 42 return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL, 43 LM3631_LCD_EN_MASK, 44 LM3631_LCD_EN_MASK); 45 } 46 47 return 0; 48 } 49 50 static void ti_lmu_disable_hw(void *data) 51 { 52 struct ti_lmu *lmu = data; 53 if (lmu->en_gpio) 54 gpiod_set_value(lmu->en_gpio, 0); 55 } 56 57 #define LM363X_REGULATOR(_id) \ 58 { \ 59 .name = "lm363x-regulator", \ 60 .id = _id, \ 61 .of_compatible = "ti,lm363x-regulator", \ 62 } \ 63 64 static const struct mfd_cell lm3631_devices[] = { 65 LM363X_REGULATOR(LM3631_BOOST), 66 LM363X_REGULATOR(LM3631_LDO_CONT), 67 LM363X_REGULATOR(LM3631_LDO_OREF), 68 LM363X_REGULATOR(LM3631_LDO_POS), 69 LM363X_REGULATOR(LM3631_LDO_NEG), 70 { 71 .name = "ti-lmu-backlight", 72 .id = LM3631, 73 .of_compatible = "ti,lm3631-backlight", 74 }, 75 }; 76 77 static const struct mfd_cell lm3632_devices[] = { 78 LM363X_REGULATOR(LM3632_BOOST), 79 LM363X_REGULATOR(LM3632_LDO_POS), 80 LM363X_REGULATOR(LM3632_LDO_NEG), 81 { 82 .name = "ti-lmu-backlight", 83 .id = LM3632, 84 .of_compatible = "ti,lm3632-backlight", 85 }, 86 }; 87 88 static const struct mfd_cell lm3633_devices[] = { 89 { 90 .name = "ti-lmu-backlight", 91 .id = LM3633, 92 .of_compatible = "ti,lm3633-backlight", 93 }, 94 { 95 .name = "lm3633-leds", 96 .of_compatible = "ti,lm3633-leds", 97 }, 98 /* Monitoring driver for open/short circuit detection */ 99 { 100 .name = "ti-lmu-fault-monitor", 101 .id = LM3633, 102 .of_compatible = "ti,lm3633-fault-monitor", 103 }, 104 }; 105 106 static const struct mfd_cell lm3695_devices[] = { 107 { 108 .name = "ti-lmu-backlight", 109 .id = LM3695, 110 .of_compatible = "ti,lm3695-backlight", 111 }, 112 }; 113 114 static const struct mfd_cell lm3697_devices[] = { 115 { 116 .name = "ti-lmu-backlight", 117 .id = LM3697, 118 .of_compatible = "ti,lm3697-backlight", 119 }, 120 /* Monitoring driver for open/short circuit detection */ 121 { 122 .name = "ti-lmu-fault-monitor", 123 .id = LM3697, 124 .of_compatible = "ti,lm3697-fault-monitor", 125 }, 126 }; 127 128 #define TI_LMU_DATA(chip, max_reg) \ 129 static const struct ti_lmu_data chip##_data = \ 130 { \ 131 .cells = chip##_devices, \ 132 .num_cells = ARRAY_SIZE(chip##_devices),\ 133 .max_register = max_reg, \ 134 } \ 135 136 TI_LMU_DATA(lm3631, LM3631_MAX_REG); 137 TI_LMU_DATA(lm3632, LM3632_MAX_REG); 138 TI_LMU_DATA(lm3633, LM3633_MAX_REG); 139 TI_LMU_DATA(lm3695, LM3695_MAX_REG); 140 TI_LMU_DATA(lm3697, LM3697_MAX_REG); 141 142 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id) 143 { 144 struct device *dev = &cl->dev; 145 const struct ti_lmu_data *data; 146 struct regmap_config regmap_cfg; 147 struct ti_lmu *lmu; 148 int ret; 149 150 /* 151 * Get device specific data from of_match table. 152 * This data is defined by using TI_LMU_DATA() macro. 153 */ 154 data = of_device_get_match_data(dev); 155 if (!data) 156 return -ENODEV; 157 158 lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL); 159 if (!lmu) 160 return -ENOMEM; 161 162 lmu->dev = &cl->dev; 163 164 /* Setup regmap */ 165 memset(®map_cfg, 0, sizeof(struct regmap_config)); 166 regmap_cfg.reg_bits = 8; 167 regmap_cfg.val_bits = 8; 168 regmap_cfg.name = id->name; 169 regmap_cfg.max_register = data->max_register; 170 171 lmu->regmap = devm_regmap_init_i2c(cl, ®map_cfg); 172 if (IS_ERR(lmu->regmap)) 173 return PTR_ERR(lmu->regmap); 174 175 /* HW enable pin control and additional power up sequence if required */ 176 lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); 177 if (IS_ERR(lmu->en_gpio)) { 178 ret = PTR_ERR(lmu->en_gpio); 179 dev_err(dev, "Can not request enable GPIO: %d\n", ret); 180 return ret; 181 } 182 183 ret = ti_lmu_enable_hw(lmu, id->driver_data); 184 if (ret) 185 return ret; 186 187 ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu); 188 if (ret) 189 return ret; 190 191 /* 192 * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor. 193 * After fault detection is done, some devices should re-initialize 194 * configuration. The notifier enables such kind of handling. 195 */ 196 BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier); 197 198 i2c_set_clientdata(cl, lmu); 199 200 return devm_mfd_add_devices(lmu->dev, 0, data->cells, 201 data->num_cells, NULL, 0, NULL); 202 } 203 204 static const struct of_device_id ti_lmu_of_match[] = { 205 { .compatible = "ti,lm3631", .data = &lm3631_data }, 206 { .compatible = "ti,lm3632", .data = &lm3632_data }, 207 { .compatible = "ti,lm3633", .data = &lm3633_data }, 208 { .compatible = "ti,lm3695", .data = &lm3695_data }, 209 { .compatible = "ti,lm3697", .data = &lm3697_data }, 210 { } 211 }; 212 MODULE_DEVICE_TABLE(of, ti_lmu_of_match); 213 214 static const struct i2c_device_id ti_lmu_ids[] = { 215 { "lm3631", LM3631 }, 216 { "lm3632", LM3632 }, 217 { "lm3633", LM3633 }, 218 { "lm3695", LM3695 }, 219 { "lm3697", LM3697 }, 220 { } 221 }; 222 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids); 223 224 static struct i2c_driver ti_lmu_driver = { 225 .probe = ti_lmu_probe, 226 .driver = { 227 .name = "ti-lmu", 228 .of_match_table = ti_lmu_of_match, 229 }, 230 .id_table = ti_lmu_ids, 231 }; 232 233 module_i2c_driver(ti_lmu_driver); 234 235 MODULE_DESCRIPTION("TI LMU MFD Core Driver"); 236 MODULE_AUTHOR("Milo Kim"); 237 MODULE_LICENSE("GPL v2"); 238