18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * TI LMU (Lighting Management Unit) Core Driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2017 Texas Instruments 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Milo Kim <milo.kim@ti.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/delay.h> 118c2ecf20Sopenharmony_ci#include <linux/err.h> 128c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/mfd/core.h> 168c2ecf20Sopenharmony_ci#include <linux/mfd/ti-lmu.h> 178c2ecf20Sopenharmony_ci#include <linux/mfd/ti-lmu-register.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/of.h> 208c2ecf20Sopenharmony_ci#include <linux/of_device.h> 218c2ecf20Sopenharmony_ci#include <linux/slab.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct ti_lmu_data { 248c2ecf20Sopenharmony_ci const struct mfd_cell *cells; 258c2ecf20Sopenharmony_ci int num_cells; 268c2ecf20Sopenharmony_ci unsigned int max_register; 278c2ecf20Sopenharmony_ci}; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci if (lmu->en_gpio) 328c2ecf20Sopenharmony_ci gpiod_set_value(lmu->en_gpio, 1); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci /* Delay about 1ms after HW enable pin control */ 358c2ecf20Sopenharmony_ci usleep_range(1000, 1500); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* LM3631 has additional power up sequence - enable LCD_EN bit. */ 388c2ecf20Sopenharmony_ci if (id == LM3631) { 398c2ecf20Sopenharmony_ci return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL, 408c2ecf20Sopenharmony_ci LM3631_LCD_EN_MASK, 418c2ecf20Sopenharmony_ci LM3631_LCD_EN_MASK); 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci return 0; 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic void ti_lmu_disable_hw(void *data) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct ti_lmu *lmu = data; 508c2ecf20Sopenharmony_ci if (lmu->en_gpio) 518c2ecf20Sopenharmony_ci gpiod_set_value(lmu->en_gpio, 0); 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define LM363X_REGULATOR(_id) \ 558c2ecf20Sopenharmony_ci{ \ 568c2ecf20Sopenharmony_ci .name = "lm363x-regulator", \ 578c2ecf20Sopenharmony_ci .id = _id, \ 588c2ecf20Sopenharmony_ci .of_compatible = "ti,lm363x-regulator", \ 598c2ecf20Sopenharmony_ci} \ 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic const struct mfd_cell lm3631_devices[] = { 628c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3631_BOOST), 638c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3631_LDO_CONT), 648c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3631_LDO_OREF), 658c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3631_LDO_POS), 668c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3631_LDO_NEG), 678c2ecf20Sopenharmony_ci { 688c2ecf20Sopenharmony_ci .name = "ti-lmu-backlight", 698c2ecf20Sopenharmony_ci .id = LM3631, 708c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3631-backlight", 718c2ecf20Sopenharmony_ci }, 728c2ecf20Sopenharmony_ci}; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic const struct mfd_cell lm3632_devices[] = { 758c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3632_BOOST), 768c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3632_LDO_POS), 778c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM3632_LDO_NEG), 788c2ecf20Sopenharmony_ci { 798c2ecf20Sopenharmony_ci .name = "ti-lmu-backlight", 808c2ecf20Sopenharmony_ci .id = LM3632, 818c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3632-backlight", 828c2ecf20Sopenharmony_ci }, 838c2ecf20Sopenharmony_ci}; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic const struct mfd_cell lm3633_devices[] = { 868c2ecf20Sopenharmony_ci { 878c2ecf20Sopenharmony_ci .name = "ti-lmu-backlight", 888c2ecf20Sopenharmony_ci .id = LM3633, 898c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3633-backlight", 908c2ecf20Sopenharmony_ci }, 918c2ecf20Sopenharmony_ci { 928c2ecf20Sopenharmony_ci .name = "lm3633-leds", 938c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3633-leds", 948c2ecf20Sopenharmony_ci }, 958c2ecf20Sopenharmony_ci /* Monitoring driver for open/short circuit detection */ 968c2ecf20Sopenharmony_ci { 978c2ecf20Sopenharmony_ci .name = "ti-lmu-fault-monitor", 988c2ecf20Sopenharmony_ci .id = LM3633, 998c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3633-fault-monitor", 1008c2ecf20Sopenharmony_ci }, 1018c2ecf20Sopenharmony_ci}; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic const struct mfd_cell lm3695_devices[] = { 1048c2ecf20Sopenharmony_ci { 1058c2ecf20Sopenharmony_ci .name = "ti-lmu-backlight", 1068c2ecf20Sopenharmony_ci .id = LM3695, 1078c2ecf20Sopenharmony_ci .of_compatible = "ti,lm3695-backlight", 1088c2ecf20Sopenharmony_ci }, 1098c2ecf20Sopenharmony_ci}; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic const struct mfd_cell lm36274_devices[] = { 1128c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM36274_BOOST), 1138c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM36274_LDO_POS), 1148c2ecf20Sopenharmony_ci LM363X_REGULATOR(LM36274_LDO_NEG), 1158c2ecf20Sopenharmony_ci { 1168c2ecf20Sopenharmony_ci .name = "lm36274-leds", 1178c2ecf20Sopenharmony_ci .id = LM36274, 1188c2ecf20Sopenharmony_ci .of_compatible = "ti,lm36274-backlight", 1198c2ecf20Sopenharmony_ci }, 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci#define TI_LMU_DATA(chip, max_reg) \ 1238c2ecf20Sopenharmony_cistatic const struct ti_lmu_data chip##_data = \ 1248c2ecf20Sopenharmony_ci{ \ 1258c2ecf20Sopenharmony_ci .cells = chip##_devices, \ 1268c2ecf20Sopenharmony_ci .num_cells = ARRAY_SIZE(chip##_devices),\ 1278c2ecf20Sopenharmony_ci .max_register = max_reg, \ 1288c2ecf20Sopenharmony_ci} \ 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciTI_LMU_DATA(lm3631, LM3631_MAX_REG); 1318c2ecf20Sopenharmony_ciTI_LMU_DATA(lm3632, LM3632_MAX_REG); 1328c2ecf20Sopenharmony_ciTI_LMU_DATA(lm3633, LM3633_MAX_REG); 1338c2ecf20Sopenharmony_ciTI_LMU_DATA(lm3695, LM3695_MAX_REG); 1348c2ecf20Sopenharmony_ciTI_LMU_DATA(lm36274, LM36274_MAX_REG); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci struct device *dev = &cl->dev; 1398c2ecf20Sopenharmony_ci const struct ti_lmu_data *data; 1408c2ecf20Sopenharmony_ci struct regmap_config regmap_cfg; 1418c2ecf20Sopenharmony_ci struct ti_lmu *lmu; 1428c2ecf20Sopenharmony_ci int ret; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci * Get device specific data from of_match table. 1468c2ecf20Sopenharmony_ci * This data is defined by using TI_LMU_DATA() macro. 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci data = of_device_get_match_data(dev); 1498c2ecf20Sopenharmony_ci if (!data) 1508c2ecf20Sopenharmony_ci return -ENODEV; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL); 1538c2ecf20Sopenharmony_ci if (!lmu) 1548c2ecf20Sopenharmony_ci return -ENOMEM; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci lmu->dev = &cl->dev; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* Setup regmap */ 1598c2ecf20Sopenharmony_ci memset(®map_cfg, 0, sizeof(struct regmap_config)); 1608c2ecf20Sopenharmony_ci regmap_cfg.reg_bits = 8; 1618c2ecf20Sopenharmony_ci regmap_cfg.val_bits = 8; 1628c2ecf20Sopenharmony_ci regmap_cfg.name = id->name; 1638c2ecf20Sopenharmony_ci regmap_cfg.max_register = data->max_register; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci lmu->regmap = devm_regmap_init_i2c(cl, ®map_cfg); 1668c2ecf20Sopenharmony_ci if (IS_ERR(lmu->regmap)) 1678c2ecf20Sopenharmony_ci return PTR_ERR(lmu->regmap); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* HW enable pin control and additional power up sequence if required */ 1708c2ecf20Sopenharmony_ci lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); 1718c2ecf20Sopenharmony_ci if (IS_ERR(lmu->en_gpio)) { 1728c2ecf20Sopenharmony_ci ret = PTR_ERR(lmu->en_gpio); 1738c2ecf20Sopenharmony_ci dev_err(dev, "Can not request enable GPIO: %d\n", ret); 1748c2ecf20Sopenharmony_ci return ret; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci ret = ti_lmu_enable_hw(lmu, id->driver_data); 1788c2ecf20Sopenharmony_ci if (ret) 1798c2ecf20Sopenharmony_ci return ret; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu); 1828c2ecf20Sopenharmony_ci if (ret) 1838c2ecf20Sopenharmony_ci return ret; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* 1868c2ecf20Sopenharmony_ci * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor. 1878c2ecf20Sopenharmony_ci * After fault detection is done, some devices should re-initialize 1888c2ecf20Sopenharmony_ci * configuration. The notifier enables such kind of handling. 1898c2ecf20Sopenharmony_ci */ 1908c2ecf20Sopenharmony_ci BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci i2c_set_clientdata(cl, lmu); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci return devm_mfd_add_devices(lmu->dev, 0, data->cells, 1958c2ecf20Sopenharmony_ci data->num_cells, NULL, 0, NULL); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic const struct of_device_id ti_lmu_of_match[] = { 1998c2ecf20Sopenharmony_ci { .compatible = "ti,lm3631", .data = &lm3631_data }, 2008c2ecf20Sopenharmony_ci { .compatible = "ti,lm3632", .data = &lm3632_data }, 2018c2ecf20Sopenharmony_ci { .compatible = "ti,lm3633", .data = &lm3633_data }, 2028c2ecf20Sopenharmony_ci { .compatible = "ti,lm3695", .data = &lm3695_data }, 2038c2ecf20Sopenharmony_ci { .compatible = "ti,lm36274", .data = &lm36274_data }, 2048c2ecf20Sopenharmony_ci { } 2058c2ecf20Sopenharmony_ci}; 2068c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ti_lmu_of_match); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic const struct i2c_device_id ti_lmu_ids[] = { 2098c2ecf20Sopenharmony_ci { "lm3631", LM3631 }, 2108c2ecf20Sopenharmony_ci { "lm3632", LM3632 }, 2118c2ecf20Sopenharmony_ci { "lm3633", LM3633 }, 2128c2ecf20Sopenharmony_ci { "lm3695", LM3695 }, 2138c2ecf20Sopenharmony_ci { "lm36274", LM36274 }, 2148c2ecf20Sopenharmony_ci { } 2158c2ecf20Sopenharmony_ci}; 2168c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ti_lmu_ids); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic struct i2c_driver ti_lmu_driver = { 2198c2ecf20Sopenharmony_ci .probe = ti_lmu_probe, 2208c2ecf20Sopenharmony_ci .driver = { 2218c2ecf20Sopenharmony_ci .name = "ti-lmu", 2228c2ecf20Sopenharmony_ci .of_match_table = ti_lmu_of_match, 2238c2ecf20Sopenharmony_ci }, 2248c2ecf20Sopenharmony_ci .id_table = ti_lmu_ids, 2258c2ecf20Sopenharmony_ci}; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cimodule_i2c_driver(ti_lmu_driver); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI LMU MFD Core Driver"); 2308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Milo Kim"); 2318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 232