12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
227c6750eSGraeme Gregory /*
3a8799defSPaul Gortmaker * tps65910.c -- TI TPS6591x chip family multi-function driver
427c6750eSGraeme Gregory *
527c6750eSGraeme Gregory * Copyright 2010 Texas Instruments Inc.
627c6750eSGraeme Gregory *
727c6750eSGraeme Gregory * Author: Graeme Gregory <gg@slimlogic.co.uk>
827c6750eSGraeme Gregory * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
927c6750eSGraeme Gregory */
1027c6750eSGraeme Gregory
1127c6750eSGraeme Gregory #include <linux/init.h>
12dc9913a0SLaxman Dewangan #include <linux/err.h>
1327c6750eSGraeme Gregory #include <linux/slab.h>
1427c6750eSGraeme Gregory #include <linux/i2c.h>
154aab3fadSLaxman Dewangan #include <linux/interrupt.h>
164aab3fadSLaxman Dewangan #include <linux/irq.h>
174aab3fadSLaxman Dewangan #include <linux/irqdomain.h>
1827c6750eSGraeme Gregory #include <linux/mfd/core.h>
19dc9913a0SLaxman Dewangan #include <linux/regmap.h>
2027c6750eSGraeme Gregory #include <linux/mfd/tps65910.h>
211fead3f3SSachin Kamat #include <linux/of.h>
22830fafceSRob Herring #include <linux/property.h>
2327c6750eSGraeme Gregory
240aefed0eSRikard Falkeborn static const struct resource rtc_resources[] = {
255863eabbSVenu Byravarasu {
265863eabbSVenu Byravarasu .start = TPS65910_IRQ_RTC_ALARM,
275863eabbSVenu Byravarasu .end = TPS65910_IRQ_RTC_ALARM,
285863eabbSVenu Byravarasu .flags = IORESOURCE_IRQ,
295863eabbSVenu Byravarasu }
305863eabbSVenu Byravarasu };
315863eabbSVenu Byravarasu
3230fe2b5bSGeert Uytterhoeven static const struct mfd_cell tps65910s[] = {
3327c6750eSGraeme Gregory {
3432df986eSLaxman Dewangan .name = "tps65910-gpio",
3532df986eSLaxman Dewangan },
3632df986eSLaxman Dewangan {
3727c6750eSGraeme Gregory .name = "tps65910-pmic",
3827c6750eSGraeme Gregory },
3927c6750eSGraeme Gregory {
4027c6750eSGraeme Gregory .name = "tps65910-rtc",
415863eabbSVenu Byravarasu .num_resources = ARRAY_SIZE(rtc_resources),
425863eabbSVenu Byravarasu .resources = &rtc_resources[0],
4327c6750eSGraeme Gregory },
4427c6750eSGraeme Gregory {
4527c6750eSGraeme Gregory .name = "tps65910-power",
4627c6750eSGraeme Gregory },
4727c6750eSGraeme Gregory };
4827c6750eSGraeme Gregory
4927c6750eSGraeme Gregory
504aab3fadSLaxman Dewangan static const struct regmap_irq tps65911_irqs[] = {
514aab3fadSLaxman Dewangan /* INT_STS */
524aab3fadSLaxman Dewangan [TPS65911_IRQ_PWRHOLD_F] = {
534aab3fadSLaxman Dewangan .mask = INT_MSK_PWRHOLD_F_IT_MSK_MASK,
544aab3fadSLaxman Dewangan .reg_offset = 0,
554aab3fadSLaxman Dewangan },
564aab3fadSLaxman Dewangan [TPS65911_IRQ_VBAT_VMHI] = {
574aab3fadSLaxman Dewangan .mask = INT_MSK_VMBHI_IT_MSK_MASK,
584aab3fadSLaxman Dewangan .reg_offset = 0,
594aab3fadSLaxman Dewangan },
604aab3fadSLaxman Dewangan [TPS65911_IRQ_PWRON] = {
614aab3fadSLaxman Dewangan .mask = INT_MSK_PWRON_IT_MSK_MASK,
624aab3fadSLaxman Dewangan .reg_offset = 0,
634aab3fadSLaxman Dewangan },
644aab3fadSLaxman Dewangan [TPS65911_IRQ_PWRON_LP] = {
654aab3fadSLaxman Dewangan .mask = INT_MSK_PWRON_LP_IT_MSK_MASK,
664aab3fadSLaxman Dewangan .reg_offset = 0,
674aab3fadSLaxman Dewangan },
684aab3fadSLaxman Dewangan [TPS65911_IRQ_PWRHOLD_R] = {
694aab3fadSLaxman Dewangan .mask = INT_MSK_PWRHOLD_R_IT_MSK_MASK,
704aab3fadSLaxman Dewangan .reg_offset = 0,
714aab3fadSLaxman Dewangan },
724aab3fadSLaxman Dewangan [TPS65911_IRQ_HOTDIE] = {
734aab3fadSLaxman Dewangan .mask = INT_MSK_HOTDIE_IT_MSK_MASK,
744aab3fadSLaxman Dewangan .reg_offset = 0,
754aab3fadSLaxman Dewangan },
764aab3fadSLaxman Dewangan [TPS65911_IRQ_RTC_ALARM] = {
774aab3fadSLaxman Dewangan .mask = INT_MSK_RTC_ALARM_IT_MSK_MASK,
784aab3fadSLaxman Dewangan .reg_offset = 0,
794aab3fadSLaxman Dewangan },
804aab3fadSLaxman Dewangan [TPS65911_IRQ_RTC_PERIOD] = {
814aab3fadSLaxman Dewangan .mask = INT_MSK_RTC_PERIOD_IT_MSK_MASK,
824aab3fadSLaxman Dewangan .reg_offset = 0,
834aab3fadSLaxman Dewangan },
844aab3fadSLaxman Dewangan
854aab3fadSLaxman Dewangan /* INT_STS2 */
864aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO0_R] = {
874aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO0_R_IT_MSK_MASK,
884aab3fadSLaxman Dewangan .reg_offset = 1,
894aab3fadSLaxman Dewangan },
904aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO0_F] = {
914aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO0_F_IT_MSK_MASK,
924aab3fadSLaxman Dewangan .reg_offset = 1,
934aab3fadSLaxman Dewangan },
944aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO1_R] = {
954aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO1_R_IT_MSK_MASK,
964aab3fadSLaxman Dewangan .reg_offset = 1,
974aab3fadSLaxman Dewangan },
984aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO1_F] = {
994aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO1_F_IT_MSK_MASK,
1004aab3fadSLaxman Dewangan .reg_offset = 1,
1014aab3fadSLaxman Dewangan },
1024aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO2_R] = {
1034aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO2_R_IT_MSK_MASK,
1044aab3fadSLaxman Dewangan .reg_offset = 1,
1054aab3fadSLaxman Dewangan },
1064aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO2_F] = {
1074aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO2_F_IT_MSK_MASK,
1084aab3fadSLaxman Dewangan .reg_offset = 1,
1094aab3fadSLaxman Dewangan },
1104aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO3_R] = {
1114aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO3_R_IT_MSK_MASK,
1124aab3fadSLaxman Dewangan .reg_offset = 1,
1134aab3fadSLaxman Dewangan },
1144aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO3_F] = {
1154aab3fadSLaxman Dewangan .mask = INT_MSK2_GPIO3_F_IT_MSK_MASK,
1164aab3fadSLaxman Dewangan .reg_offset = 1,
1174aab3fadSLaxman Dewangan },
1184aab3fadSLaxman Dewangan
1194aab3fadSLaxman Dewangan /* INT_STS2 */
1204aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO4_R] = {
1214aab3fadSLaxman Dewangan .mask = INT_MSK3_GPIO4_R_IT_MSK_MASK,
1224aab3fadSLaxman Dewangan .reg_offset = 2,
1234aab3fadSLaxman Dewangan },
1244aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO4_F] = {
1254aab3fadSLaxman Dewangan .mask = INT_MSK3_GPIO4_F_IT_MSK_MASK,
1264aab3fadSLaxman Dewangan .reg_offset = 2,
1274aab3fadSLaxman Dewangan },
1284aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO5_R] = {
1294aab3fadSLaxman Dewangan .mask = INT_MSK3_GPIO5_R_IT_MSK_MASK,
1304aab3fadSLaxman Dewangan .reg_offset = 2,
1314aab3fadSLaxman Dewangan },
1324aab3fadSLaxman Dewangan [TPS65911_IRQ_GPIO5_F] = {
1334aab3fadSLaxman Dewangan .mask = INT_MSK3_GPIO5_F_IT_MSK_MASK,
1344aab3fadSLaxman Dewangan .reg_offset = 2,
1354aab3fadSLaxman Dewangan },
1364aab3fadSLaxman Dewangan [TPS65911_IRQ_WTCHDG] = {
1374aab3fadSLaxman Dewangan .mask = INT_MSK3_WTCHDG_IT_MSK_MASK,
1384aab3fadSLaxman Dewangan .reg_offset = 2,
1394aab3fadSLaxman Dewangan },
1404aab3fadSLaxman Dewangan [TPS65911_IRQ_VMBCH2_H] = {
1414aab3fadSLaxman Dewangan .mask = INT_MSK3_VMBCH2_H_IT_MSK_MASK,
1424aab3fadSLaxman Dewangan .reg_offset = 2,
1434aab3fadSLaxman Dewangan },
1444aab3fadSLaxman Dewangan [TPS65911_IRQ_VMBCH2_L] = {
1454aab3fadSLaxman Dewangan .mask = INT_MSK3_VMBCH2_L_IT_MSK_MASK,
1464aab3fadSLaxman Dewangan .reg_offset = 2,
1474aab3fadSLaxman Dewangan },
1484aab3fadSLaxman Dewangan [TPS65911_IRQ_PWRDN] = {
1494aab3fadSLaxman Dewangan .mask = INT_MSK3_PWRDN_IT_MSK_MASK,
1504aab3fadSLaxman Dewangan .reg_offset = 2,
1514aab3fadSLaxman Dewangan },
1524aab3fadSLaxman Dewangan };
1534aab3fadSLaxman Dewangan
1544aab3fadSLaxman Dewangan static const struct regmap_irq tps65910_irqs[] = {
1554aab3fadSLaxman Dewangan /* INT_STS */
1564aab3fadSLaxman Dewangan [TPS65910_IRQ_VBAT_VMBDCH] = {
1574aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK,
1584aab3fadSLaxman Dewangan .reg_offset = 0,
1594aab3fadSLaxman Dewangan },
1604aab3fadSLaxman Dewangan [TPS65910_IRQ_VBAT_VMHI] = {
1614aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_VMBHI_IT_MSK_MASK,
1624aab3fadSLaxman Dewangan .reg_offset = 0,
1634aab3fadSLaxman Dewangan },
1644aab3fadSLaxman Dewangan [TPS65910_IRQ_PWRON] = {
1654aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_PWRON_IT_MSK_MASK,
1664aab3fadSLaxman Dewangan .reg_offset = 0,
1674aab3fadSLaxman Dewangan },
1684aab3fadSLaxman Dewangan [TPS65910_IRQ_PWRON_LP] = {
1694aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK,
1704aab3fadSLaxman Dewangan .reg_offset = 0,
1714aab3fadSLaxman Dewangan },
1724aab3fadSLaxman Dewangan [TPS65910_IRQ_PWRHOLD] = {
1734aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK,
1744aab3fadSLaxman Dewangan .reg_offset = 0,
1754aab3fadSLaxman Dewangan },
1764aab3fadSLaxman Dewangan [TPS65910_IRQ_HOTDIE] = {
1774aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK,
1784aab3fadSLaxman Dewangan .reg_offset = 0,
1794aab3fadSLaxman Dewangan },
1804aab3fadSLaxman Dewangan [TPS65910_IRQ_RTC_ALARM] = {
1814aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK,
1824aab3fadSLaxman Dewangan .reg_offset = 0,
1834aab3fadSLaxman Dewangan },
1844aab3fadSLaxman Dewangan [TPS65910_IRQ_RTC_PERIOD] = {
1854aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK,
1864aab3fadSLaxman Dewangan .reg_offset = 0,
1874aab3fadSLaxman Dewangan },
1884aab3fadSLaxman Dewangan
1894aab3fadSLaxman Dewangan /* INT_STS2 */
1904aab3fadSLaxman Dewangan [TPS65910_IRQ_GPIO_R] = {
1914aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK,
1924aab3fadSLaxman Dewangan .reg_offset = 1,
1934aab3fadSLaxman Dewangan },
1944aab3fadSLaxman Dewangan [TPS65910_IRQ_GPIO_F] = {
1954aab3fadSLaxman Dewangan .mask = TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK,
1964aab3fadSLaxman Dewangan .reg_offset = 1,
1974aab3fadSLaxman Dewangan },
1984aab3fadSLaxman Dewangan };
1994aab3fadSLaxman Dewangan
200*9544cc65SJavier Carrasco static const struct regmap_irq_chip tps65911_irq_chip = {
2014aab3fadSLaxman Dewangan .name = "tps65910",
2024aab3fadSLaxman Dewangan .irqs = tps65911_irqs,
2034aab3fadSLaxman Dewangan .num_irqs = ARRAY_SIZE(tps65911_irqs),
2044aab3fadSLaxman Dewangan .num_regs = 3,
2054aab3fadSLaxman Dewangan .irq_reg_stride = 2,
2064aab3fadSLaxman Dewangan .status_base = TPS65910_INT_STS,
2074aab3fadSLaxman Dewangan .mask_base = TPS65910_INT_MSK,
2080582c0faSKim, Milo .ack_base = TPS65910_INT_STS,
2094aab3fadSLaxman Dewangan };
2104aab3fadSLaxman Dewangan
211*9544cc65SJavier Carrasco static const struct regmap_irq_chip tps65910_irq_chip = {
2124aab3fadSLaxman Dewangan .name = "tps65910",
2134aab3fadSLaxman Dewangan .irqs = tps65910_irqs,
2144aab3fadSLaxman Dewangan .num_irqs = ARRAY_SIZE(tps65910_irqs),
2154aab3fadSLaxman Dewangan .num_regs = 2,
2164aab3fadSLaxman Dewangan .irq_reg_stride = 2,
2174aab3fadSLaxman Dewangan .status_base = TPS65910_INT_STS,
2184aab3fadSLaxman Dewangan .mask_base = TPS65910_INT_MSK,
2190582c0faSKim, Milo .ack_base = TPS65910_INT_STS,
2204aab3fadSLaxman Dewangan };
2214aab3fadSLaxman Dewangan
tps65910_irq_init(struct tps65910 * tps65910,int irq,struct tps65910_platform_data * pdata)2224aab3fadSLaxman Dewangan static int tps65910_irq_init(struct tps65910 *tps65910, int irq,
2234aab3fadSLaxman Dewangan struct tps65910_platform_data *pdata)
2244aab3fadSLaxman Dewangan {
225dae3be36SMarkus Elfring int ret;
226*9544cc65SJavier Carrasco static const struct regmap_irq_chip *tps6591x_irqs_chip;
2274aab3fadSLaxman Dewangan
2284aab3fadSLaxman Dewangan if (!irq) {
2294aab3fadSLaxman Dewangan dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
2304aab3fadSLaxman Dewangan return -EINVAL;
2314aab3fadSLaxman Dewangan }
2324aab3fadSLaxman Dewangan
2334aab3fadSLaxman Dewangan if (!pdata) {
2344aab3fadSLaxman Dewangan dev_warn(tps65910->dev, "No interrupt support, no pdata\n");
2354aab3fadSLaxman Dewangan return -EINVAL;
2364aab3fadSLaxman Dewangan }
2374aab3fadSLaxman Dewangan
2384aab3fadSLaxman Dewangan switch (tps65910_chip_id(tps65910)) {
2394aab3fadSLaxman Dewangan case TPS65910:
2404aab3fadSLaxman Dewangan tps6591x_irqs_chip = &tps65910_irq_chip;
2414aab3fadSLaxman Dewangan break;
2424aab3fadSLaxman Dewangan case TPS65911:
2434aab3fadSLaxman Dewangan tps6591x_irqs_chip = &tps65911_irq_chip;
2444aab3fadSLaxman Dewangan break;
2454aab3fadSLaxman Dewangan }
2464aab3fadSLaxman Dewangan
2474aab3fadSLaxman Dewangan tps65910->chip_irq = irq;
2486167c5bcSLaxman Dewangan ret = devm_regmap_add_irq_chip(tps65910->dev, tps65910->regmap,
2496167c5bcSLaxman Dewangan tps65910->chip_irq,
2504aab3fadSLaxman Dewangan IRQF_ONESHOT, pdata->irq_base,
2514aab3fadSLaxman Dewangan tps6591x_irqs_chip, &tps65910->irq_data);
252483e2dfdSKrzysztof Kozlowski if (ret < 0) {
2534aab3fadSLaxman Dewangan dev_warn(tps65910->dev, "Failed to add irq_chip %d\n", ret);
254483e2dfdSKrzysztof Kozlowski tps65910->chip_irq = 0;
255483e2dfdSKrzysztof Kozlowski }
2564aab3fadSLaxman Dewangan return ret;
2574aab3fadSLaxman Dewangan }
2584aab3fadSLaxman Dewangan
is_volatile_reg(struct device * dev,unsigned int reg)259dc9913a0SLaxman Dewangan static bool is_volatile_reg(struct device *dev, unsigned int reg)
260dc9913a0SLaxman Dewangan {
261dc9913a0SLaxman Dewangan struct tps65910 *tps65910 = dev_get_drvdata(dev);
262dc9913a0SLaxman Dewangan
263dc9913a0SLaxman Dewangan /*
264dc9913a0SLaxman Dewangan * Caching all regulator registers.
265dc9913a0SLaxman Dewangan * All regualator register address range is same for
266dc9913a0SLaxman Dewangan * TPS65910 and TPS65911
267dc9913a0SLaxman Dewangan */
268dc9913a0SLaxman Dewangan if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
269dc9913a0SLaxman Dewangan /* Check for non-existing register */
270dc9913a0SLaxman Dewangan if (tps65910_chip_id(tps65910) == TPS65910)
271dc9913a0SLaxman Dewangan if ((reg == TPS65911_VDDCTRL_OP) ||
272dc9913a0SLaxman Dewangan (reg == TPS65911_VDDCTRL_SR))
273dc9913a0SLaxman Dewangan return true;
274dc9913a0SLaxman Dewangan return false;
275dc9913a0SLaxman Dewangan }
276dc9913a0SLaxman Dewangan return true;
277dc9913a0SLaxman Dewangan }
278dc9913a0SLaxman Dewangan
27939ecb037SLaxman Dewangan static const struct regmap_config tps65910_regmap_config = {
280dc9913a0SLaxman Dewangan .reg_bits = 8,
281dc9913a0SLaxman Dewangan .val_bits = 8,
282dc9913a0SLaxman Dewangan .volatile_reg = is_volatile_reg,
2833bf6bf9bSLaxman Dewangan .max_register = TPS65910_MAX_REGISTER - 1,
284535cd579SMark Brown .cache_type = REGCACHE_MAPLE,
285dc9913a0SLaxman Dewangan };
286dc9913a0SLaxman Dewangan
tps65910_ck32k_init(struct tps65910 * tps65910,struct tps65910_board * pmic_pdata)287f791be49SBill Pemberton static int tps65910_ck32k_init(struct tps65910 *tps65910,
288712db99dSJohan Hovold struct tps65910_board *pmic_pdata)
289712db99dSJohan Hovold {
290712db99dSJohan Hovold int ret;
291712db99dSJohan Hovold
292d02e83cbSJohan Hovold if (!pmic_pdata->en_ck32k_xtal)
293d02e83cbSJohan Hovold return 0;
294d02e83cbSJohan Hovold
29528faad77SMichał Mirosław ret = regmap_clear_bits(tps65910->regmap, TPS65910_DEVCTRL,
296712db99dSJohan Hovold DEVCTRL_CK32K_CTRL_MASK);
297712db99dSJohan Hovold if (ret < 0) {
298d02e83cbSJohan Hovold dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
299712db99dSJohan Hovold return ret;
300712db99dSJohan Hovold }
301712db99dSJohan Hovold
302712db99dSJohan Hovold return 0;
303712db99dSJohan Hovold }
304712db99dSJohan Hovold
tps65910_sleepinit(struct tps65910 * tps65910,struct tps65910_board * pmic_pdata)305f791be49SBill Pemberton static int tps65910_sleepinit(struct tps65910 *tps65910,
306201cf052SLaxman Dewangan struct tps65910_board *pmic_pdata)
307201cf052SLaxman Dewangan {
308da257efaSMarkus Elfring struct device *dev;
309dae3be36SMarkus Elfring int ret;
310201cf052SLaxman Dewangan
311201cf052SLaxman Dewangan if (!pmic_pdata->en_dev_slp)
312201cf052SLaxman Dewangan return 0;
313201cf052SLaxman Dewangan
31471bc4f1dSMarkus Elfring dev = tps65910->dev;
31571bc4f1dSMarkus Elfring
316201cf052SLaxman Dewangan /* enabling SLEEP device state */
31728faad77SMichał Mirosław ret = regmap_set_bits(tps65910->regmap, TPS65910_DEVCTRL,
318201cf052SLaxman Dewangan DEVCTRL_DEV_SLP_MASK);
319201cf052SLaxman Dewangan if (ret < 0) {
320201cf052SLaxman Dewangan dev_err(dev, "set dev_slp failed: %d\n", ret);
321201cf052SLaxman Dewangan goto err_sleep_init;
322201cf052SLaxman Dewangan }
323201cf052SLaxman Dewangan
324a9bc67deSMichał Mirosław if (pmic_pdata->slp_keepon.therm_keepon) {
32528faad77SMichał Mirosław ret = regmap_set_bits(tps65910->regmap,
3263f7e8275SRhyland Klein TPS65910_SLEEP_KEEP_RES_ON,
327201cf052SLaxman Dewangan SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
328201cf052SLaxman Dewangan if (ret < 0) {
329201cf052SLaxman Dewangan dev_err(dev, "set therm_keepon failed: %d\n", ret);
330201cf052SLaxman Dewangan goto disable_dev_slp;
331201cf052SLaxman Dewangan }
332201cf052SLaxman Dewangan }
333201cf052SLaxman Dewangan
334a9bc67deSMichał Mirosław if (pmic_pdata->slp_keepon.clkout32k_keepon) {
33528faad77SMichał Mirosław ret = regmap_set_bits(tps65910->regmap,
3363f7e8275SRhyland Klein TPS65910_SLEEP_KEEP_RES_ON,
337201cf052SLaxman Dewangan SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
338201cf052SLaxman Dewangan if (ret < 0) {
339201cf052SLaxman Dewangan dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
340201cf052SLaxman Dewangan goto disable_dev_slp;
341201cf052SLaxman Dewangan }
342201cf052SLaxman Dewangan }
343201cf052SLaxman Dewangan
344a9bc67deSMichał Mirosław if (pmic_pdata->slp_keepon.i2chs_keepon) {
34528faad77SMichał Mirosław ret = regmap_set_bits(tps65910->regmap,
3463f7e8275SRhyland Klein TPS65910_SLEEP_KEEP_RES_ON,
347201cf052SLaxman Dewangan SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
348201cf052SLaxman Dewangan if (ret < 0) {
349201cf052SLaxman Dewangan dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
350201cf052SLaxman Dewangan goto disable_dev_slp;
351201cf052SLaxman Dewangan }
352201cf052SLaxman Dewangan }
353201cf052SLaxman Dewangan
354201cf052SLaxman Dewangan return 0;
355201cf052SLaxman Dewangan
356201cf052SLaxman Dewangan disable_dev_slp:
35728faad77SMichał Mirosław regmap_clear_bits(tps65910->regmap, TPS65910_DEVCTRL,
3583f7e8275SRhyland Klein DEVCTRL_DEV_SLP_MASK);
359201cf052SLaxman Dewangan
360201cf052SLaxman Dewangan err_sleep_init:
361201cf052SLaxman Dewangan return ret;
362201cf052SLaxman Dewangan }
363201cf052SLaxman Dewangan
364cd4209ceSRhyland Klein #ifdef CONFIG_OF
365c0dfbfe2SJingoo Han static const struct of_device_id tps65910_of_match[] = {
366cd4209ceSRhyland Klein { .compatible = "ti,tps65910", .data = (void *)TPS65910},
367cd4209ceSRhyland Klein { .compatible = "ti,tps65911", .data = (void *)TPS65911},
368cd4209ceSRhyland Klein { },
369cd4209ceSRhyland Klein };
370cd4209ceSRhyland Klein
tps65910_parse_dt(struct i2c_client * client,unsigned long * chip_id)371cd4209ceSRhyland Klein static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
37201a0f4aaSLee Jones unsigned long *chip_id)
373cd4209ceSRhyland Klein {
374cd4209ceSRhyland Klein struct device_node *np = client->dev.of_node;
375cd4209ceSRhyland Klein struct tps65910_board *board_info;
376cd4209ceSRhyland Klein unsigned int prop;
377dae3be36SMarkus Elfring int ret;
378cd4209ceSRhyland Klein
379830fafceSRob Herring *chip_id = (unsigned long)device_get_match_data(&client->dev);
380cd4209ceSRhyland Klein
381cd4209ceSRhyland Klein board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
382cd4209ceSRhyland Klein GFP_KERNEL);
38393e879efSMarkus Elfring if (!board_info)
384cd4209ceSRhyland Klein return NULL;
385cd4209ceSRhyland Klein
386cd4209ceSRhyland Klein ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
387cd4209ceSRhyland Klein if (!ret)
388cd4209ceSRhyland Klein board_info->vmbch_threshold = prop;
389cd4209ceSRhyland Klein
390cd4209ceSRhyland Klein ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
391cd4209ceSRhyland Klein if (!ret)
392cd4209ceSRhyland Klein board_info->vmbch2_threshold = prop;
393cd4209ceSRhyland Klein
394bcc1dd4cSJohan Hovold prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
395bcc1dd4cSJohan Hovold board_info->en_ck32k_xtal = prop;
396bcc1dd4cSJohan Hovold
397a9bc67deSMichał Mirosław prop = of_property_read_bool(np, "ti,sleep-enable");
398a9bc67deSMichał Mirosław board_info->en_dev_slp = prop;
399a9bc67deSMichał Mirosław
400a9bc67deSMichał Mirosław prop = of_property_read_bool(np, "ti,sleep-keep-therm");
401a9bc67deSMichał Mirosław board_info->slp_keepon.therm_keepon = prop;
402a9bc67deSMichał Mirosław
403a9bc67deSMichał Mirosław prop = of_property_read_bool(np, "ti,sleep-keep-ck32k");
404a9bc67deSMichał Mirosław board_info->slp_keepon.clkout32k_keepon = prop;
405a9bc67deSMichał Mirosław
406a9bc67deSMichał Mirosław prop = of_property_read_bool(np, "ti,sleep-keep-hsclk");
407a9bc67deSMichał Mirosław board_info->slp_keepon.i2chs_keepon = prop;
408a9bc67deSMichał Mirosław
409cd4209ceSRhyland Klein board_info->irq = client->irq;
410cd4209ceSRhyland Klein board_info->irq_base = -1;
411b079fa72SBill Huang board_info->pm_off = of_property_read_bool(np,
412b079fa72SBill Huang "ti,system-power-controller");
413cd4209ceSRhyland Klein
414cd4209ceSRhyland Klein return board_info;
415cd4209ceSRhyland Klein }
416cd4209ceSRhyland Klein #else
4177f65f74cSSamuel Ortiz static inline
tps65910_parse_dt(struct i2c_client * client,unsigned long * chip_id)4187f65f74cSSamuel Ortiz struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
41901a0f4aaSLee Jones unsigned long *chip_id)
420cd4209ceSRhyland Klein {
421cd4209ceSRhyland Klein return NULL;
422cd4209ceSRhyland Klein }
423cd4209ceSRhyland Klein #endif
424201cf052SLaxman Dewangan
425b079fa72SBill Huang static struct i2c_client *tps65910_i2c_client;
tps65910_power_off(void)426b079fa72SBill Huang static void tps65910_power_off(void)
427b079fa72SBill Huang {
428b079fa72SBill Huang struct tps65910 *tps65910;
429b079fa72SBill Huang
430b079fa72SBill Huang tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
431b079fa72SBill Huang
432129989d5SDmitry Osipenko regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
433129989d5SDmitry Osipenko DEVCTRL_DEV_OFF_MASK | DEVCTRL_DEV_ON_MASK,
434129989d5SDmitry Osipenko DEVCTRL_DEV_OFF_MASK);
435b079fa72SBill Huang }
436b079fa72SBill Huang
tps65910_i2c_probe(struct i2c_client * i2c)43774e52d31SUwe Kleine-König static int tps65910_i2c_probe(struct i2c_client *i2c)
43827c6750eSGraeme Gregory {
43974e52d31SUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
44027c6750eSGraeme Gregory struct tps65910 *tps65910;
4412537df72SGraeme Gregory struct tps65910_board *pmic_plat_data;
442cb8d8654SLaxman Dewangan struct tps65910_board *of_pmic_plat_data = NULL;
443e3471bdcSGraeme Gregory struct tps65910_platform_data *init_data;
44401a0f4aaSLee Jones unsigned long chip_id = id->driver_data;
445dae3be36SMarkus Elfring int ret;
44627c6750eSGraeme Gregory
4472537df72SGraeme Gregory pmic_plat_data = dev_get_platdata(&i2c->dev);
448cd4209ceSRhyland Klein
449cb8d8654SLaxman Dewangan if (!pmic_plat_data && i2c->dev.of_node) {
450cd4209ceSRhyland Klein pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
451cb8d8654SLaxman Dewangan of_pmic_plat_data = pmic_plat_data;
452cb8d8654SLaxman Dewangan }
453cd4209ceSRhyland Klein
4542537df72SGraeme Gregory if (!pmic_plat_data)
4552537df72SGraeme Gregory return -EINVAL;
4562537df72SGraeme Gregory
45763fe7deeSLaxman Dewangan init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
458e3471bdcSGraeme Gregory if (init_data == NULL)
459e3471bdcSGraeme Gregory return -ENOMEM;
460e3471bdcSGraeme Gregory
46163fe7deeSLaxman Dewangan tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
46263fe7deeSLaxman Dewangan if (tps65910 == NULL)
46327c6750eSGraeme Gregory return -ENOMEM;
46427c6750eSGraeme Gregory
465cb8d8654SLaxman Dewangan tps65910->of_plat_data = of_pmic_plat_data;
46627c6750eSGraeme Gregory i2c_set_clientdata(i2c, tps65910);
46727c6750eSGraeme Gregory tps65910->dev = &i2c->dev;
46827c6750eSGraeme Gregory tps65910->i2c_client = i2c;
469cd4209ceSRhyland Klein tps65910->id = chip_id;
47027c6750eSGraeme Gregory
471be1c7700SArnout Vandecappelle (Essensium/Mind) /* Work around silicon erratum SWCZ010: the tps65910 may miss the
472be1c7700SArnout Vandecappelle (Essensium/Mind) * first I2C transfer. So issue a dummy transfer before the first
473be1c7700SArnout Vandecappelle (Essensium/Mind) * real transfer.
474be1c7700SArnout Vandecappelle (Essensium/Mind) */
475be1c7700SArnout Vandecappelle (Essensium/Mind) i2c_master_send(i2c, "", 1);
47663fe7deeSLaxman Dewangan tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
477dc9913a0SLaxman Dewangan if (IS_ERR(tps65910->regmap)) {
478dc9913a0SLaxman Dewangan ret = PTR_ERR(tps65910->regmap);
479dc9913a0SLaxman Dewangan dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
48063fe7deeSLaxman Dewangan return ret;
481dc9913a0SLaxman Dewangan }
482dc9913a0SLaxman Dewangan
483b1224cd1SJesper Juhl init_data->irq = pmic_plat_data->irq;
4841773140fSLaxman Dewangan init_data->irq_base = pmic_plat_data->irq_base;
485b1224cd1SJesper Juhl
4861e351a95SAfzal Mohammed tps65910_irq_init(tps65910, init_data->irq, init_data);
487d02e83cbSJohan Hovold tps65910_ck32k_init(tps65910, pmic_plat_data);
488201cf052SLaxman Dewangan tps65910_sleepinit(tps65910, pmic_plat_data);
489201cf052SLaxman Dewangan
490b079fa72SBill Huang if (pmic_plat_data->pm_off && !pm_power_off) {
4917620ad0bSDmitry Osipenko /*
4927620ad0bSDmitry Osipenko * The PWR_OFF bit needs to be set separately, before
4937620ad0bSDmitry Osipenko * transitioning to the OFF state. It enables the "sequential"
4947620ad0bSDmitry Osipenko * power-off mode on TPS65911, it's a NO-OP on TPS65910.
4957620ad0bSDmitry Osipenko */
4967620ad0bSDmitry Osipenko ret = regmap_set_bits(tps65910->regmap, TPS65910_DEVCTRL,
4977620ad0bSDmitry Osipenko DEVCTRL_PWR_OFF_MASK);
4987620ad0bSDmitry Osipenko if (ret) {
4997620ad0bSDmitry Osipenko dev_err(&i2c->dev, "failed to set power-off mode: %d\n",
5007620ad0bSDmitry Osipenko ret);
5017620ad0bSDmitry Osipenko return ret;
5027620ad0bSDmitry Osipenko }
5037620ad0bSDmitry Osipenko
504b079fa72SBill Huang tps65910_i2c_client = i2c;
505b079fa72SBill Huang pm_power_off = tps65910_power_off;
506b079fa72SBill Huang }
507b079fa72SBill Huang
508f3466e77SLaxman Dewangan ret = devm_mfd_add_devices(tps65910->dev, -1,
50910ecb80eSLaxman Dewangan tps65910s, ARRAY_SIZE(tps65910s),
51017143e38SLaxman Dewangan NULL, 0,
51117143e38SLaxman Dewangan regmap_irq_get_domain(tps65910->irq_data));
51210ecb80eSLaxman Dewangan if (ret < 0) {
51310ecb80eSLaxman Dewangan dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
51410ecb80eSLaxman Dewangan return ret;
51510ecb80eSLaxman Dewangan }
51610ecb80eSLaxman Dewangan
51727c6750eSGraeme Gregory return ret;
51827c6750eSGraeme Gregory }
51927c6750eSGraeme Gregory
52027c6750eSGraeme Gregory static const struct i2c_device_id tps65910_i2c_id[] = {
52179557056SJorge Eduardo Candelaria { "tps65910", TPS65910 },
52279557056SJorge Eduardo Candelaria { "tps65911", TPS65911 },
52327c6750eSGraeme Gregory { }
52427c6750eSGraeme Gregory };
52527c6750eSGraeme Gregory
52627c6750eSGraeme Gregory static struct i2c_driver tps65910_i2c_driver = {
52727c6750eSGraeme Gregory .driver = {
52827c6750eSGraeme Gregory .name = "tps65910",
529cd4209ceSRhyland Klein .of_match_table = of_match_ptr(tps65910_of_match),
53027c6750eSGraeme Gregory },
5319816d859SUwe Kleine-König .probe = tps65910_i2c_probe,
53227c6750eSGraeme Gregory .id_table = tps65910_i2c_id,
53327c6750eSGraeme Gregory };
53427c6750eSGraeme Gregory
tps65910_i2c_init(void)53527c6750eSGraeme Gregory static int __init tps65910_i2c_init(void)
53627c6750eSGraeme Gregory {
53727c6750eSGraeme Gregory return i2c_add_driver(&tps65910_i2c_driver);
53827c6750eSGraeme Gregory }
53927c6750eSGraeme Gregory /* init early so consumer devices can complete system boot */
54027c6750eSGraeme Gregory subsys_initcall(tps65910_i2c_init);
541