18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright 2009-2010 Pengutronix 48c2ecf20Sopenharmony_ci * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * loosely based on an earlier driver that has 78c2ecf20Sopenharmony_ci * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/of.h> 128c2ecf20Sopenharmony_ci#include <linux/of_device.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/mfd/core.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "mc13xxx.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define MC13XXX_IRQSTAT0 0 198c2ecf20Sopenharmony_ci#define MC13XXX_IRQMASK0 1 208c2ecf20Sopenharmony_ci#define MC13XXX_IRQSTAT1 3 218c2ecf20Sopenharmony_ci#define MC13XXX_IRQMASK1 4 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define MC13XXX_REVISION 7 248c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_REVMETAL (0x07 << 0) 258c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_REVFULL (0x03 << 3) 268c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_ICID (0x07 << 6) 278c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_FIN (0x03 << 9) 288c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_FAB (0x03 << 11) 298c2ecf20Sopenharmony_ci#define MC13XXX_REVISION_ICIDCODE (0x3f << 13) 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define MC34708_REVISION_REVMETAL (0x07 << 0) 328c2ecf20Sopenharmony_ci#define MC34708_REVISION_REVFULL (0x07 << 3) 338c2ecf20Sopenharmony_ci#define MC34708_REVISION_FIN (0x07 << 6) 348c2ecf20Sopenharmony_ci#define MC34708_REVISION_FAB (0x07 << 9) 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define MC13XXX_PWRCTRL 15 378c2ecf20Sopenharmony_ci#define MC13XXX_PWRCTRL_WDIRESET (1 << 12) 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define MC13XXX_ADC1 44 408c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_ADEN (1 << 0) 418c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_RAND (1 << 1) 428c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_ADSEL (1 << 3) 438c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_ASC (1 << 20) 448c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_ADTRIGIGN (1 << 21) 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define MC13XXX_ADC2 45 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_civoid mc13xxx_lock(struct mc13xxx *mc13xxx) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci if (!mutex_trylock(&mc13xxx->lock)) { 518c2ecf20Sopenharmony_ci dev_dbg(mc13xxx->dev, "wait for %s from %ps\n", 528c2ecf20Sopenharmony_ci __func__, __builtin_return_address(0)); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci mutex_lock(&mc13xxx->lock); 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci dev_dbg(mc13xxx->dev, "%s from %ps\n", 578c2ecf20Sopenharmony_ci __func__, __builtin_return_address(0)); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_lock); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_civoid mc13xxx_unlock(struct mc13xxx *mc13xxx) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci dev_dbg(mc13xxx->dev, "%s from %ps\n", 648c2ecf20Sopenharmony_ci __func__, __builtin_return_address(0)); 658c2ecf20Sopenharmony_ci mutex_unlock(&mc13xxx->lock); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_unlock); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciint mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci int ret; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci ret = regmap_read(mc13xxx->regmap, offset, val); 748c2ecf20Sopenharmony_ci dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return ret; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_reg_read); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciint mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (val >= BIT(24)) 858c2ecf20Sopenharmony_ci return -EINVAL; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci return regmap_write(mc13xxx->regmap, offset, val); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_reg_write); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciint mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset, 928c2ecf20Sopenharmony_ci u32 mask, u32 val) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci BUG_ON(val & ~mask); 958c2ecf20Sopenharmony_ci dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n", 968c2ecf20Sopenharmony_ci offset, val, mask); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci return regmap_update_bits(mc13xxx->regmap, offset, mask, val); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_reg_rmw); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciint mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci disable_irq_nosync(virq); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return 0; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_irq_mask); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ciint mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci enable_irq(virq); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci return 0; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_irq_unmask); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ciint mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq, 1238c2ecf20Sopenharmony_ci int *enabled, int *pending) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci int ret; 1268c2ecf20Sopenharmony_ci unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1; 1278c2ecf20Sopenharmony_ci unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1; 1288c2ecf20Sopenharmony_ci u32 irqbit = 1 << (irq < 24 ? irq : irq - 24); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci if (irq < 0 || irq >= ARRAY_SIZE(mc13xxx->irqs)) 1318c2ecf20Sopenharmony_ci return -EINVAL; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci if (enabled) { 1348c2ecf20Sopenharmony_ci u32 mask; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci ret = mc13xxx_reg_read(mc13xxx, offmask, &mask); 1378c2ecf20Sopenharmony_ci if (ret) 1388c2ecf20Sopenharmony_ci return ret; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci *enabled = mask & irqbit; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (pending) { 1448c2ecf20Sopenharmony_ci u32 stat; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci ret = mc13xxx_reg_read(mc13xxx, offstat, &stat); 1478c2ecf20Sopenharmony_ci if (ret) 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci *pending = stat & irqbit; 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_irq_status); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciint mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq, 1588c2ecf20Sopenharmony_ci irq_handler_t handler, const char *name, void *dev) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return devm_request_threaded_irq(mc13xxx->dev, virq, NULL, handler, 1638c2ecf20Sopenharmony_ci IRQF_ONESHOT, name, dev); 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_irq_request); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ciint mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev) 1688c2ecf20Sopenharmony_ci{ 1698c2ecf20Sopenharmony_ci int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci devm_free_irq(mc13xxx->dev, virq, dev); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return 0; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_irq_free); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci#define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask)) 1788c2ecf20Sopenharmony_cistatic void mc13xxx_print_revision(struct mc13xxx *mc13xxx, u32 revision) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci dev_info(mc13xxx->dev, "%s: rev: %d.%d, " 1818c2ecf20Sopenharmony_ci "fin: %d, fab: %d, icid: %d/%d\n", 1828c2ecf20Sopenharmony_ci mc13xxx->variant->name, 1838c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_REVFULL), 1848c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_REVMETAL), 1858c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_FIN), 1868c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_FAB), 1878c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_ICID), 1888c2ecf20Sopenharmony_ci maskval(revision, MC13XXX_REVISION_ICIDCODE)); 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic void mc34708_print_revision(struct mc13xxx *mc13xxx, u32 revision) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci dev_info(mc13xxx->dev, "%s: rev %d.%d, fin: %d, fab: %d\n", 1948c2ecf20Sopenharmony_ci mc13xxx->variant->name, 1958c2ecf20Sopenharmony_ci maskval(revision, MC34708_REVISION_REVFULL), 1968c2ecf20Sopenharmony_ci maskval(revision, MC34708_REVISION_REVMETAL), 1978c2ecf20Sopenharmony_ci maskval(revision, MC34708_REVISION_FIN), 1988c2ecf20Sopenharmony_ci maskval(revision, MC34708_REVISION_FAB)); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* These are only exported for mc13xxx-i2c and mc13xxx-spi */ 2028c2ecf20Sopenharmony_cistruct mc13xxx_variant mc13xxx_variant_mc13783 = { 2038c2ecf20Sopenharmony_ci .name = "mc13783", 2048c2ecf20Sopenharmony_ci .print_revision = mc13xxx_print_revision, 2058c2ecf20Sopenharmony_ci}; 2068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_variant_mc13783); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistruct mc13xxx_variant mc13xxx_variant_mc13892 = { 2098c2ecf20Sopenharmony_ci .name = "mc13892", 2108c2ecf20Sopenharmony_ci .print_revision = mc13xxx_print_revision, 2118c2ecf20Sopenharmony_ci}; 2128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_variant_mc13892); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistruct mc13xxx_variant mc13xxx_variant_mc34708 = { 2158c2ecf20Sopenharmony_ci .name = "mc34708", 2168c2ecf20Sopenharmony_ci .print_revision = mc34708_print_revision, 2178c2ecf20Sopenharmony_ci}; 2188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_variant_mc34708); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci return mc13xxx->variant->name; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ciint mc13xxx_get_flags(struct mc13xxx *mc13xxx) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci return mc13xxx->flags; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mc13xxx_get_flags); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_CHAN0_SHIFT 5 2328c2ecf20Sopenharmony_ci#define MC13XXX_ADC1_CHAN1_SHIFT 8 2338c2ecf20Sopenharmony_ci#define MC13783_ADC1_ATO_SHIFT 11 2348c2ecf20Sopenharmony_ci#define MC13783_ADC1_ATOX (1 << 19) 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistruct mc13xxx_adcdone_data { 2378c2ecf20Sopenharmony_ci struct mc13xxx *mc13xxx; 2388c2ecf20Sopenharmony_ci struct completion done; 2398c2ecf20Sopenharmony_ci}; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic irqreturn_t mc13xxx_handler_adcdone(int irq, void *data) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct mc13xxx_adcdone_data *adcdone_data = data; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci complete_all(&adcdone_data->done); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci#define MC13XXX_ADC_WORKING (1 << 0) 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ciint mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode, 2538c2ecf20Sopenharmony_ci unsigned int channel, u8 ato, bool atox, 2548c2ecf20Sopenharmony_ci unsigned int *sample) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci u32 adc0, adc1, old_adc0; 2578c2ecf20Sopenharmony_ci int i, ret; 2588c2ecf20Sopenharmony_ci struct mc13xxx_adcdone_data adcdone_data = { 2598c2ecf20Sopenharmony_ci .mc13xxx = mc13xxx, 2608c2ecf20Sopenharmony_ci }; 2618c2ecf20Sopenharmony_ci init_completion(&adcdone_data.done); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci dev_dbg(mc13xxx->dev, "%s\n", __func__); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci mc13xxx_lock(mc13xxx); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) { 2688c2ecf20Sopenharmony_ci ret = -EBUSY; 2698c2ecf20Sopenharmony_ci goto out; 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci mc13xxx->adcflags |= MC13XXX_ADC_WORKING; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci ret = mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0); 2758c2ecf20Sopenharmony_ci if (ret) 2768c2ecf20Sopenharmony_ci goto out; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2 | 2798c2ecf20Sopenharmony_ci MC13XXX_ADC0_CHRGRAWDIV; 2808c2ecf20Sopenharmony_ci adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* 2838c2ecf20Sopenharmony_ci * Channels mapped through ADIN7: 2848c2ecf20Sopenharmony_ci * 7 - General purpose ADIN7 2858c2ecf20Sopenharmony_ci * 16 - UID 2868c2ecf20Sopenharmony_ci * 17 - Die temperature 2878c2ecf20Sopenharmony_ci */ 2888c2ecf20Sopenharmony_ci if (channel > 7 && channel < 16) { 2898c2ecf20Sopenharmony_ci adc1 |= MC13XXX_ADC1_ADSEL; 2908c2ecf20Sopenharmony_ci } else if (channel == 16) { 2918c2ecf20Sopenharmony_ci adc0 |= MC13XXX_ADC0_ADIN7SEL_UID; 2928c2ecf20Sopenharmony_ci channel = 7; 2938c2ecf20Sopenharmony_ci } else if (channel == 17) { 2948c2ecf20Sopenharmony_ci adc0 |= MC13XXX_ADC0_ADIN7SEL_DIE; 2958c2ecf20Sopenharmony_ci channel = 7; 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci switch (mode) { 2998c2ecf20Sopenharmony_ci case MC13XXX_ADC_MODE_TS: 3008c2ecf20Sopenharmony_ci adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 | 3018c2ecf20Sopenharmony_ci MC13XXX_ADC0_TSMOD1; 3028c2ecf20Sopenharmony_ci adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT; 3038c2ecf20Sopenharmony_ci break; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci case MC13XXX_ADC_MODE_SINGLE_CHAN: 3068c2ecf20Sopenharmony_ci adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK; 3078c2ecf20Sopenharmony_ci adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT; 3088c2ecf20Sopenharmony_ci adc1 |= MC13XXX_ADC1_RAND; 3098c2ecf20Sopenharmony_ci break; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci case MC13XXX_ADC_MODE_MULT_CHAN: 3128c2ecf20Sopenharmony_ci adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK; 3138c2ecf20Sopenharmony_ci adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT; 3148c2ecf20Sopenharmony_ci break; 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci default: 3178c2ecf20Sopenharmony_ci mc13xxx_unlock(mc13xxx); 3188c2ecf20Sopenharmony_ci return -EINVAL; 3198c2ecf20Sopenharmony_ci } 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci adc1 |= ato << MC13783_ADC1_ATO_SHIFT; 3228c2ecf20Sopenharmony_ci if (atox) 3238c2ecf20Sopenharmony_ci adc1 |= MC13783_ADC1_ATOX; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__); 3268c2ecf20Sopenharmony_ci ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE, 3278c2ecf20Sopenharmony_ci mc13xxx_handler_adcdone, __func__, &adcdone_data); 3288c2ecf20Sopenharmony_ci if (ret) 3298c2ecf20Sopenharmony_ci goto out; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0); 3328c2ecf20Sopenharmony_ci mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci mc13xxx_unlock(mc13xxx); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (!ret) 3398c2ecf20Sopenharmony_ci ret = -ETIMEDOUT; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci mc13xxx_lock(mc13xxx); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci if (ret > 0) 3468c2ecf20Sopenharmony_ci for (i = 0; i < 4; ++i) { 3478c2ecf20Sopenharmony_ci ret = mc13xxx_reg_read(mc13xxx, 3488c2ecf20Sopenharmony_ci MC13XXX_ADC2, &sample[i]); 3498c2ecf20Sopenharmony_ci if (ret) 3508c2ecf20Sopenharmony_ci break; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci if (mode == MC13XXX_ADC_MODE_TS) 3548c2ecf20Sopenharmony_ci /* restore TSMOD */ 3558c2ecf20Sopenharmony_ci mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0); 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING; 3588c2ecf20Sopenharmony_ciout: 3598c2ecf20Sopenharmony_ci mc13xxx_unlock(mc13xxx); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci} 3638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cistatic int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx, 3668c2ecf20Sopenharmony_ci const char *format, void *pdata, size_t pdata_size) 3678c2ecf20Sopenharmony_ci{ 3688c2ecf20Sopenharmony_ci char buf[30]; 3698c2ecf20Sopenharmony_ci const char *name = mc13xxx_get_chipname(mc13xxx); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci struct mfd_cell cell = { 3728c2ecf20Sopenharmony_ci .platform_data = pdata, 3738c2ecf20Sopenharmony_ci .pdata_size = pdata_size, 3748c2ecf20Sopenharmony_ci }; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci /* there is no asnprintf in the kernel :-( */ 3778c2ecf20Sopenharmony_ci if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf)) 3788c2ecf20Sopenharmony_ci return -E2BIG; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL); 3818c2ecf20Sopenharmony_ci if (!cell.name) 3828c2ecf20Sopenharmony_ci return -ENOMEM; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0, 3858c2ecf20Sopenharmony_ci regmap_irq_get_domain(mc13xxx->irq_data)); 3868c2ecf20Sopenharmony_ci} 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_cistatic int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format) 3898c2ecf20Sopenharmony_ci{ 3908c2ecf20Sopenharmony_ci return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 3948c2ecf20Sopenharmony_cistatic int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx) 3958c2ecf20Sopenharmony_ci{ 3968c2ecf20Sopenharmony_ci struct device_node *np = mc13xxx->dev->of_node; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci if (!np) 3998c2ecf20Sopenharmony_ci return -ENODEV; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci if (of_property_read_bool(np, "fsl,mc13xxx-uses-adc")) 4028c2ecf20Sopenharmony_ci mc13xxx->flags |= MC13XXX_USE_ADC; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci if (of_property_read_bool(np, "fsl,mc13xxx-uses-codec")) 4058c2ecf20Sopenharmony_ci mc13xxx->flags |= MC13XXX_USE_CODEC; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci if (of_property_read_bool(np, "fsl,mc13xxx-uses-rtc")) 4088c2ecf20Sopenharmony_ci mc13xxx->flags |= MC13XXX_USE_RTC; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci if (of_property_read_bool(np, "fsl,mc13xxx-uses-touch")) 4118c2ecf20Sopenharmony_ci mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci return 0; 4148c2ecf20Sopenharmony_ci} 4158c2ecf20Sopenharmony_ci#else 4168c2ecf20Sopenharmony_cistatic inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx) 4178c2ecf20Sopenharmony_ci{ 4188c2ecf20Sopenharmony_ci return -ENODEV; 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci#endif 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ciint mc13xxx_common_init(struct device *dev) 4238c2ecf20Sopenharmony_ci{ 4248c2ecf20Sopenharmony_ci struct mc13xxx_platform_data *pdata = dev_get_platdata(dev); 4258c2ecf20Sopenharmony_ci struct mc13xxx *mc13xxx = dev_get_drvdata(dev); 4268c2ecf20Sopenharmony_ci u32 revision; 4278c2ecf20Sopenharmony_ci int i, ret; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci mc13xxx->dev = dev; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision); 4328c2ecf20Sopenharmony_ci if (ret) 4338c2ecf20Sopenharmony_ci return ret; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci mc13xxx->variant->print_revision(mc13xxx, revision); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci ret = mc13xxx_reg_rmw(mc13xxx, MC13XXX_PWRCTRL, 4388c2ecf20Sopenharmony_ci MC13XXX_PWRCTRL_WDIRESET, MC13XXX_PWRCTRL_WDIRESET); 4398c2ecf20Sopenharmony_ci if (ret) 4408c2ecf20Sopenharmony_ci return ret; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(mc13xxx->irqs); i++) { 4438c2ecf20Sopenharmony_ci mc13xxx->irqs[i].reg_offset = i / MC13XXX_IRQ_PER_REG; 4448c2ecf20Sopenharmony_ci mc13xxx->irqs[i].mask = BIT(i % MC13XXX_IRQ_PER_REG); 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci mc13xxx->irq_chip.name = dev_name(dev); 4488c2ecf20Sopenharmony_ci mc13xxx->irq_chip.status_base = MC13XXX_IRQSTAT0; 4498c2ecf20Sopenharmony_ci mc13xxx->irq_chip.mask_base = MC13XXX_IRQMASK0; 4508c2ecf20Sopenharmony_ci mc13xxx->irq_chip.ack_base = MC13XXX_IRQSTAT0; 4518c2ecf20Sopenharmony_ci mc13xxx->irq_chip.irq_reg_stride = MC13XXX_IRQSTAT1 - MC13XXX_IRQSTAT0; 4528c2ecf20Sopenharmony_ci mc13xxx->irq_chip.init_ack_masked = true; 4538c2ecf20Sopenharmony_ci mc13xxx->irq_chip.use_ack = true; 4548c2ecf20Sopenharmony_ci mc13xxx->irq_chip.num_regs = MC13XXX_IRQ_REG_CNT; 4558c2ecf20Sopenharmony_ci mc13xxx->irq_chip.irqs = mc13xxx->irqs; 4568c2ecf20Sopenharmony_ci mc13xxx->irq_chip.num_irqs = ARRAY_SIZE(mc13xxx->irqs); 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci ret = regmap_add_irq_chip(mc13xxx->regmap, mc13xxx->irq, IRQF_ONESHOT, 4598c2ecf20Sopenharmony_ci 0, &mc13xxx->irq_chip, &mc13xxx->irq_data); 4608c2ecf20Sopenharmony_ci if (ret) 4618c2ecf20Sopenharmony_ci return ret; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci mutex_init(&mc13xxx->lock); 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata) 4668c2ecf20Sopenharmony_ci mc13xxx->flags = pdata->flags; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci if (pdata) { 4698c2ecf20Sopenharmony_ci mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator", 4708c2ecf20Sopenharmony_ci &pdata->regulators, sizeof(pdata->regulators)); 4718c2ecf20Sopenharmony_ci mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led", 4728c2ecf20Sopenharmony_ci pdata->leds, sizeof(*pdata->leds)); 4738c2ecf20Sopenharmony_ci mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton", 4748c2ecf20Sopenharmony_ci pdata->buttons, sizeof(*pdata->buttons)); 4758c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_CODEC) 4768c2ecf20Sopenharmony_ci mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec", 4778c2ecf20Sopenharmony_ci pdata->codec, sizeof(*pdata->codec)); 4788c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN) 4798c2ecf20Sopenharmony_ci mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts", 4808c2ecf20Sopenharmony_ci &pdata->touch, sizeof(pdata->touch)); 4818c2ecf20Sopenharmony_ci } else { 4828c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-regulator"); 4838c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-led"); 4848c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton"); 4858c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_CODEC) 4868c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-codec"); 4878c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN) 4888c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-ts"); 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_ADC) 4928c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-adc"); 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci if (mc13xxx->flags & MC13XXX_USE_RTC) 4958c2ecf20Sopenharmony_ci mc13xxx_add_subdevice(mc13xxx, "%s-rtc"); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci return 0; 4988c2ecf20Sopenharmony_ci} 4998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_common_init); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ciint mc13xxx_common_exit(struct device *dev) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci struct mc13xxx *mc13xxx = dev_get_drvdata(dev); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci mfd_remove_devices(dev); 5068c2ecf20Sopenharmony_ci regmap_del_irq_chip(mc13xxx->irq, mc13xxx->irq_data); 5078c2ecf20Sopenharmony_ci mutex_destroy(&mc13xxx->lock); 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci return 0; 5108c2ecf20Sopenharmony_ci} 5118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mc13xxx_common_exit); 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC"); 5148c2ecf20Sopenharmony_ciMODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>"); 5158c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 516