Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009-2010 Pengutronix |
| 3 | * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de> |
| 4 | * |
| 5 | * loosely based on an earlier driver that has |
| 6 | * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it under |
| 9 | * the terms of the GNU General Public License version 2 as published by the |
| 10 | * Free Software Foundation. |
| 11 | */ |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 12 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/interrupt.h> |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 18 | #include <linux/mfd/core.h> |
| 19 | #include <linux/mfd/mc13xxx.h> |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_device.h> |
| 22 | #include <linux/of_gpio.h> |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 23 | |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 24 | #include "mc13xxx.h" |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 25 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 26 | #define MC13XXX_IRQSTAT0 0 |
| 27 | #define MC13XXX_IRQSTAT0_ADCDONEI (1 << 0) |
| 28 | #define MC13XXX_IRQSTAT0_ADCBISDONEI (1 << 1) |
| 29 | #define MC13XXX_IRQSTAT0_TSI (1 << 2) |
| 30 | #define MC13783_IRQSTAT0_WHIGHI (1 << 3) |
| 31 | #define MC13783_IRQSTAT0_WLOWI (1 << 4) |
| 32 | #define MC13XXX_IRQSTAT0_CHGDETI (1 << 6) |
| 33 | #define MC13783_IRQSTAT0_CHGOVI (1 << 7) |
| 34 | #define MC13XXX_IRQSTAT0_CHGREVI (1 << 8) |
| 35 | #define MC13XXX_IRQSTAT0_CHGSHORTI (1 << 9) |
| 36 | #define MC13XXX_IRQSTAT0_CCCVI (1 << 10) |
| 37 | #define MC13XXX_IRQSTAT0_CHGCURRI (1 << 11) |
| 38 | #define MC13XXX_IRQSTAT0_BPONI (1 << 12) |
| 39 | #define MC13XXX_IRQSTAT0_LOBATLI (1 << 13) |
| 40 | #define MC13XXX_IRQSTAT0_LOBATHI (1 << 14) |
| 41 | #define MC13783_IRQSTAT0_UDPI (1 << 15) |
| 42 | #define MC13783_IRQSTAT0_USBI (1 << 16) |
| 43 | #define MC13783_IRQSTAT0_IDI (1 << 19) |
| 44 | #define MC13783_IRQSTAT0_SE1I (1 << 21) |
| 45 | #define MC13783_IRQSTAT0_CKDETI (1 << 22) |
| 46 | #define MC13783_IRQSTAT0_UDMI (1 << 23) |
| 47 | |
| 48 | #define MC13XXX_IRQMASK0 1 |
| 49 | #define MC13XXX_IRQMASK0_ADCDONEM MC13XXX_IRQSTAT0_ADCDONEI |
| 50 | #define MC13XXX_IRQMASK0_ADCBISDONEM MC13XXX_IRQSTAT0_ADCBISDONEI |
| 51 | #define MC13XXX_IRQMASK0_TSM MC13XXX_IRQSTAT0_TSI |
| 52 | #define MC13783_IRQMASK0_WHIGHM MC13783_IRQSTAT0_WHIGHI |
| 53 | #define MC13783_IRQMASK0_WLOWM MC13783_IRQSTAT0_WLOWI |
| 54 | #define MC13XXX_IRQMASK0_CHGDETM MC13XXX_IRQSTAT0_CHGDETI |
| 55 | #define MC13783_IRQMASK0_CHGOVM MC13783_IRQSTAT0_CHGOVI |
| 56 | #define MC13XXX_IRQMASK0_CHGREVM MC13XXX_IRQSTAT0_CHGREVI |
| 57 | #define MC13XXX_IRQMASK0_CHGSHORTM MC13XXX_IRQSTAT0_CHGSHORTI |
| 58 | #define MC13XXX_IRQMASK0_CCCVM MC13XXX_IRQSTAT0_CCCVI |
| 59 | #define MC13XXX_IRQMASK0_CHGCURRM MC13XXX_IRQSTAT0_CHGCURRI |
| 60 | #define MC13XXX_IRQMASK0_BPONM MC13XXX_IRQSTAT0_BPONI |
| 61 | #define MC13XXX_IRQMASK0_LOBATLM MC13XXX_IRQSTAT0_LOBATLI |
| 62 | #define MC13XXX_IRQMASK0_LOBATHM MC13XXX_IRQSTAT0_LOBATHI |
| 63 | #define MC13783_IRQMASK0_UDPM MC13783_IRQSTAT0_UDPI |
| 64 | #define MC13783_IRQMASK0_USBM MC13783_IRQSTAT0_USBI |
| 65 | #define MC13783_IRQMASK0_IDM MC13783_IRQSTAT0_IDI |
| 66 | #define MC13783_IRQMASK0_SE1M MC13783_IRQSTAT0_SE1I |
| 67 | #define MC13783_IRQMASK0_CKDETM MC13783_IRQSTAT0_CKDETI |
| 68 | #define MC13783_IRQMASK0_UDMM MC13783_IRQSTAT0_UDMI |
| 69 | |
| 70 | #define MC13XXX_IRQSTAT1 3 |
| 71 | #define MC13XXX_IRQSTAT1_1HZI (1 << 0) |
| 72 | #define MC13XXX_IRQSTAT1_TODAI (1 << 1) |
| 73 | #define MC13783_IRQSTAT1_ONOFD1I (1 << 3) |
| 74 | #define MC13783_IRQSTAT1_ONOFD2I (1 << 4) |
| 75 | #define MC13783_IRQSTAT1_ONOFD3I (1 << 5) |
| 76 | #define MC13XXX_IRQSTAT1_SYSRSTI (1 << 6) |
| 77 | #define MC13XXX_IRQSTAT1_RTCRSTI (1 << 7) |
| 78 | #define MC13XXX_IRQSTAT1_PCI (1 << 8) |
| 79 | #define MC13XXX_IRQSTAT1_WARMI (1 << 9) |
| 80 | #define MC13XXX_IRQSTAT1_MEMHLDI (1 << 10) |
| 81 | #define MC13783_IRQSTAT1_PWRRDYI (1 << 11) |
| 82 | #define MC13XXX_IRQSTAT1_THWARNLI (1 << 12) |
| 83 | #define MC13XXX_IRQSTAT1_THWARNHI (1 << 13) |
| 84 | #define MC13XXX_IRQSTAT1_CLKI (1 << 14) |
| 85 | #define MC13783_IRQSTAT1_SEMAFI (1 << 15) |
| 86 | #define MC13783_IRQSTAT1_MC2BI (1 << 17) |
| 87 | #define MC13783_IRQSTAT1_HSDETI (1 << 18) |
| 88 | #define MC13783_IRQSTAT1_HSLI (1 << 19) |
| 89 | #define MC13783_IRQSTAT1_ALSPTHI (1 << 20) |
| 90 | #define MC13783_IRQSTAT1_AHSSHORTI (1 << 21) |
| 91 | |
| 92 | #define MC13XXX_IRQMASK1 4 |
| 93 | #define MC13XXX_IRQMASK1_1HZM MC13XXX_IRQSTAT1_1HZI |
| 94 | #define MC13XXX_IRQMASK1_TODAM MC13XXX_IRQSTAT1_TODAI |
| 95 | #define MC13783_IRQMASK1_ONOFD1M MC13783_IRQSTAT1_ONOFD1I |
| 96 | #define MC13783_IRQMASK1_ONOFD2M MC13783_IRQSTAT1_ONOFD2I |
| 97 | #define MC13783_IRQMASK1_ONOFD3M MC13783_IRQSTAT1_ONOFD3I |
| 98 | #define MC13XXX_IRQMASK1_SYSRSTM MC13XXX_IRQSTAT1_SYSRSTI |
| 99 | #define MC13XXX_IRQMASK1_RTCRSTM MC13XXX_IRQSTAT1_RTCRSTI |
| 100 | #define MC13XXX_IRQMASK1_PCM MC13XXX_IRQSTAT1_PCI |
| 101 | #define MC13XXX_IRQMASK1_WARMM MC13XXX_IRQSTAT1_WARMI |
| 102 | #define MC13XXX_IRQMASK1_MEMHLDM MC13XXX_IRQSTAT1_MEMHLDI |
| 103 | #define MC13783_IRQMASK1_PWRRDYM MC13783_IRQSTAT1_PWRRDYI |
| 104 | #define MC13XXX_IRQMASK1_THWARNLM MC13XXX_IRQSTAT1_THWARNLI |
| 105 | #define MC13XXX_IRQMASK1_THWARNHM MC13XXX_IRQSTAT1_THWARNHI |
| 106 | #define MC13XXX_IRQMASK1_CLKM MC13XXX_IRQSTAT1_CLKI |
| 107 | #define MC13783_IRQMASK1_SEMAFM MC13783_IRQSTAT1_SEMAFI |
| 108 | #define MC13783_IRQMASK1_MC2BM MC13783_IRQSTAT1_MC2BI |
| 109 | #define MC13783_IRQMASK1_HSDETM MC13783_IRQSTAT1_HSDETI |
| 110 | #define MC13783_IRQMASK1_HSLM MC13783_IRQSTAT1_HSLI |
| 111 | #define MC13783_IRQMASK1_ALSPTHM MC13783_IRQSTAT1_ALSPTHI |
| 112 | #define MC13783_IRQMASK1_AHSSHORTM MC13783_IRQSTAT1_AHSSHORTI |
| 113 | |
| 114 | #define MC13XXX_REVISION 7 |
| 115 | #define MC13XXX_REVISION_REVMETAL (0x07 << 0) |
| 116 | #define MC13XXX_REVISION_REVFULL (0x03 << 3) |
| 117 | #define MC13XXX_REVISION_ICID (0x07 << 6) |
| 118 | #define MC13XXX_REVISION_FIN (0x03 << 9) |
| 119 | #define MC13XXX_REVISION_FAB (0x03 << 11) |
| 120 | #define MC13XXX_REVISION_ICIDCODE (0x3f << 13) |
| 121 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 122 | #define MC13XXX_ADC1 44 |
| 123 | #define MC13XXX_ADC1_ADEN (1 << 0) |
| 124 | #define MC13XXX_ADC1_RAND (1 << 1) |
| 125 | #define MC13XXX_ADC1_ADSEL (1 << 3) |
| 126 | #define MC13XXX_ADC1_ASC (1 << 20) |
| 127 | #define MC13XXX_ADC1_ADTRIGIGN (1 << 21) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 128 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 129 | #define MC13XXX_ADC2 45 |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 130 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 131 | void mc13xxx_lock(struct mc13xxx *mc13xxx) |
| 132 | { |
| 133 | if (!mutex_trylock(&mc13xxx->lock)) { |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 134 | dev_dbg(mc13xxx->dev, "wait for %s from %pf\n", |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 135 | __func__, __builtin_return_address(0)); |
| 136 | |
| 137 | mutex_lock(&mc13xxx->lock); |
| 138 | } |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 139 | dev_dbg(mc13xxx->dev, "%s from %pf\n", |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 140 | __func__, __builtin_return_address(0)); |
| 141 | } |
| 142 | EXPORT_SYMBOL(mc13xxx_lock); |
| 143 | |
| 144 | void mc13xxx_unlock(struct mc13xxx *mc13xxx) |
| 145 | { |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 146 | dev_dbg(mc13xxx->dev, "%s from %pf\n", |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 147 | __func__, __builtin_return_address(0)); |
| 148 | mutex_unlock(&mc13xxx->lock); |
| 149 | } |
| 150 | EXPORT_SYMBOL(mc13xxx_unlock); |
| 151 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 152 | int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val) |
| 153 | { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 154 | int ret; |
| 155 | |
| 156 | BUG_ON(!mutex_is_locked(&mc13xxx->lock)); |
| 157 | |
| 158 | if (offset > MC13XXX_NUMREGS) |
| 159 | return -EINVAL; |
| 160 | |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 161 | ret = regmap_read(mc13xxx->regmap, offset, val); |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 162 | dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 163 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 164 | return ret; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 165 | } |
| 166 | EXPORT_SYMBOL(mc13xxx_reg_read); |
| 167 | |
| 168 | int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val) |
| 169 | { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 170 | BUG_ON(!mutex_is_locked(&mc13xxx->lock)); |
| 171 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 172 | dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 173 | |
| 174 | if (offset > MC13XXX_NUMREGS || val > 0xffffff) |
| 175 | return -EINVAL; |
| 176 | |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 177 | return regmap_write(mc13xxx->regmap, offset, val); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 178 | } |
| 179 | EXPORT_SYMBOL(mc13xxx_reg_write); |
| 180 | |
| 181 | int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset, |
| 182 | u32 mask, u32 val) |
| 183 | { |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 184 | BUG_ON(!mutex_is_locked(&mc13xxx->lock)); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 185 | BUG_ON(val & ~mask); |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 186 | dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n", |
| 187 | offset, val, mask); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 188 | |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 189 | return regmap_update_bits(mc13xxx->regmap, offset, mask, val); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 190 | } |
| 191 | EXPORT_SYMBOL(mc13xxx_reg_rmw); |
| 192 | |
| 193 | int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq) |
| 194 | { |
| 195 | int ret; |
| 196 | unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1; |
| 197 | u32 irqbit = 1 << (irq < 24 ? irq : irq - 24); |
| 198 | u32 mask; |
| 199 | |
| 200 | if (irq < 0 || irq >= MC13XXX_NUM_IRQ) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | ret = mc13xxx_reg_read(mc13xxx, offmask, &mask); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | if (mask & irqbit) |
| 208 | /* already masked */ |
| 209 | return 0; |
| 210 | |
| 211 | return mc13xxx_reg_write(mc13xxx, offmask, mask | irqbit); |
| 212 | } |
| 213 | EXPORT_SYMBOL(mc13xxx_irq_mask); |
| 214 | |
| 215 | int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq) |
| 216 | { |
| 217 | int ret; |
| 218 | unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1; |
| 219 | u32 irqbit = 1 << (irq < 24 ? irq : irq - 24); |
| 220 | u32 mask; |
| 221 | |
| 222 | if (irq < 0 || irq >= MC13XXX_NUM_IRQ) |
| 223 | return -EINVAL; |
| 224 | |
| 225 | ret = mc13xxx_reg_read(mc13xxx, offmask, &mask); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
| 229 | if (!(mask & irqbit)) |
| 230 | /* already unmasked */ |
| 231 | return 0; |
| 232 | |
| 233 | return mc13xxx_reg_write(mc13xxx, offmask, mask & ~irqbit); |
| 234 | } |
| 235 | EXPORT_SYMBOL(mc13xxx_irq_unmask); |
| 236 | |
| 237 | int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq, |
| 238 | int *enabled, int *pending) |
| 239 | { |
| 240 | int ret; |
| 241 | unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1; |
| 242 | unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1; |
| 243 | u32 irqbit = 1 << (irq < 24 ? irq : irq - 24); |
| 244 | |
| 245 | if (irq < 0 || irq >= MC13XXX_NUM_IRQ) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | if (enabled) { |
| 249 | u32 mask; |
| 250 | |
| 251 | ret = mc13xxx_reg_read(mc13xxx, offmask, &mask); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | |
| 255 | *enabled = mask & irqbit; |
| 256 | } |
| 257 | |
| 258 | if (pending) { |
| 259 | u32 stat; |
| 260 | |
| 261 | ret = mc13xxx_reg_read(mc13xxx, offstat, &stat); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | *pending = stat & irqbit; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | EXPORT_SYMBOL(mc13xxx_irq_status); |
| 271 | |
| 272 | int mc13xxx_irq_ack(struct mc13xxx *mc13xxx, int irq) |
| 273 | { |
| 274 | unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1; |
| 275 | unsigned int val = 1 << (irq < 24 ? irq : irq - 24); |
| 276 | |
| 277 | BUG_ON(irq < 0 || irq >= MC13XXX_NUM_IRQ); |
| 278 | |
| 279 | return mc13xxx_reg_write(mc13xxx, offstat, val); |
| 280 | } |
| 281 | EXPORT_SYMBOL(mc13xxx_irq_ack); |
| 282 | |
| 283 | int mc13xxx_irq_request_nounmask(struct mc13xxx *mc13xxx, int irq, |
| 284 | irq_handler_t handler, const char *name, void *dev) |
| 285 | { |
| 286 | BUG_ON(!mutex_is_locked(&mc13xxx->lock)); |
| 287 | BUG_ON(!handler); |
| 288 | |
| 289 | if (irq < 0 || irq >= MC13XXX_NUM_IRQ) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | if (mc13xxx->irqhandler[irq]) |
| 293 | return -EBUSY; |
| 294 | |
| 295 | mc13xxx->irqhandler[irq] = handler; |
| 296 | mc13xxx->irqdata[irq] = dev; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | EXPORT_SYMBOL(mc13xxx_irq_request_nounmask); |
| 301 | |
| 302 | int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq, |
| 303 | irq_handler_t handler, const char *name, void *dev) |
| 304 | { |
| 305 | int ret; |
| 306 | |
| 307 | ret = mc13xxx_irq_request_nounmask(mc13xxx, irq, handler, name, dev); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | |
| 311 | ret = mc13xxx_irq_unmask(mc13xxx, irq); |
| 312 | if (ret) { |
| 313 | mc13xxx->irqhandler[irq] = NULL; |
| 314 | mc13xxx->irqdata[irq] = NULL; |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | EXPORT_SYMBOL(mc13xxx_irq_request); |
| 321 | |
| 322 | int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev) |
| 323 | { |
| 324 | int ret; |
| 325 | BUG_ON(!mutex_is_locked(&mc13xxx->lock)); |
| 326 | |
| 327 | if (irq < 0 || irq >= MC13XXX_NUM_IRQ || !mc13xxx->irqhandler[irq] || |
| 328 | mc13xxx->irqdata[irq] != dev) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | ret = mc13xxx_irq_mask(mc13xxx, irq); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | |
| 335 | mc13xxx->irqhandler[irq] = NULL; |
| 336 | mc13xxx->irqdata[irq] = NULL; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | EXPORT_SYMBOL(mc13xxx_irq_free); |
| 341 | |
| 342 | static inline irqreturn_t mc13xxx_irqhandler(struct mc13xxx *mc13xxx, int irq) |
| 343 | { |
| 344 | return mc13xxx->irqhandler[irq](irq, mc13xxx->irqdata[irq]); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * returns: number of handled irqs or negative error |
| 349 | * locking: holds mc13xxx->lock |
| 350 | */ |
| 351 | static int mc13xxx_irq_handle(struct mc13xxx *mc13xxx, |
| 352 | unsigned int offstat, unsigned int offmask, int baseirq) |
| 353 | { |
| 354 | u32 stat, mask; |
| 355 | int ret = mc13xxx_reg_read(mc13xxx, offstat, &stat); |
| 356 | int num_handled = 0; |
| 357 | |
| 358 | if (ret) |
| 359 | return ret; |
| 360 | |
| 361 | ret = mc13xxx_reg_read(mc13xxx, offmask, &mask); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | |
| 365 | while (stat & ~mask) { |
| 366 | int irq = __ffs(stat & ~mask); |
| 367 | |
| 368 | stat &= ~(1 << irq); |
| 369 | |
| 370 | if (likely(mc13xxx->irqhandler[baseirq + irq])) { |
| 371 | irqreturn_t handled; |
| 372 | |
| 373 | handled = mc13xxx_irqhandler(mc13xxx, baseirq + irq); |
| 374 | if (handled == IRQ_HANDLED) |
| 375 | num_handled++; |
| 376 | } else { |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 377 | dev_err(mc13xxx->dev, |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 378 | "BUG: irq %u but no handler\n", |
| 379 | baseirq + irq); |
| 380 | |
| 381 | mask |= 1 << irq; |
| 382 | |
| 383 | ret = mc13xxx_reg_write(mc13xxx, offmask, mask); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return num_handled; |
| 388 | } |
| 389 | |
| 390 | static irqreturn_t mc13xxx_irq_thread(int irq, void *data) |
| 391 | { |
| 392 | struct mc13xxx *mc13xxx = data; |
| 393 | irqreturn_t ret; |
| 394 | int handled = 0; |
| 395 | |
| 396 | mc13xxx_lock(mc13xxx); |
| 397 | |
| 398 | ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT0, |
| 399 | MC13XXX_IRQMASK0, 0); |
| 400 | if (ret > 0) |
| 401 | handled = 1; |
| 402 | |
| 403 | ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT1, |
| 404 | MC13XXX_IRQMASK1, 24); |
| 405 | if (ret > 0) |
| 406 | handled = 1; |
| 407 | |
| 408 | mc13xxx_unlock(mc13xxx); |
| 409 | |
| 410 | return IRQ_RETVAL(handled); |
| 411 | } |
| 412 | |
Uwe Kleine-König | bb3149a9 | 2011-09-23 21:51:42 +0200 | [diff] [blame] | 413 | static const char *mc13xxx_chipname[] = { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 414 | [MC13XXX_ID_MC13783] = "mc13783", |
| 415 | [MC13XXX_ID_MC13892] = "mc13892", |
| 416 | }; |
| 417 | |
| 418 | #define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask)) |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 419 | static int mc13xxx_identify(struct mc13xxx *mc13xxx) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 420 | { |
| 421 | u32 icid; |
| 422 | u32 revision; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 423 | int ret; |
| 424 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 425 | /* |
| 426 | * Get the generation ID from register 46, as apparently some older |
| 427 | * IC revisions only have this info at this location. Newer ICs seem to |
| 428 | * have both. |
| 429 | */ |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 430 | ret = mc13xxx_reg_read(mc13xxx, 46, &icid); |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | |
| 434 | icid = (icid >> 6) & 0x7; |
| 435 | |
| 436 | switch (icid) { |
| 437 | case 2: |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 438 | mc13xxx->ictype = MC13XXX_ID_MC13783; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 439 | break; |
| 440 | case 7: |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 441 | mc13xxx->ictype = MC13XXX_ID_MC13892; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 442 | break; |
| 443 | default: |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 444 | mc13xxx->ictype = MC13XXX_ID_INVALID; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 448 | if (mc13xxx->ictype == MC13XXX_ID_MC13783 || |
| 449 | mc13xxx->ictype == MC13XXX_ID_MC13892) { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 450 | ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 451 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 452 | dev_info(mc13xxx->dev, "%s: rev: %d.%d, " |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 453 | "fin: %d, fab: %d, icid: %d/%d\n", |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 454 | mc13xxx_chipname[mc13xxx->ictype], |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 455 | maskval(revision, MC13XXX_REVISION_REVFULL), |
| 456 | maskval(revision, MC13XXX_REVISION_REVMETAL), |
| 457 | maskval(revision, MC13XXX_REVISION_FIN), |
| 458 | maskval(revision, MC13XXX_REVISION_FAB), |
| 459 | maskval(revision, MC13XXX_REVISION_ICID), |
| 460 | maskval(revision, MC13XXX_REVISION_ICIDCODE)); |
| 461 | } |
| 462 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 463 | return (mc13xxx->ictype == MC13XXX_ID_INVALID) ? -ENODEV : 0; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx) |
| 467 | { |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 468 | return mc13xxx_chipname[mc13xxx->ictype]; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 471 | int mc13xxx_get_flags(struct mc13xxx *mc13xxx) |
| 472 | { |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 473 | return mc13xxx->flags; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 474 | } |
| 475 | EXPORT_SYMBOL(mc13xxx_get_flags); |
| 476 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 477 | #define MC13XXX_ADC1_CHAN0_SHIFT 5 |
| 478 | #define MC13XXX_ADC1_CHAN1_SHIFT 8 |
Michael Thalmeier | 1039d76 | 2012-02-20 12:18:13 +0100 | [diff] [blame] | 479 | #define MC13783_ADC1_ATO_SHIFT 11 |
| 480 | #define MC13783_ADC1_ATOX (1 << 19) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 481 | |
| 482 | struct mc13xxx_adcdone_data { |
| 483 | struct mc13xxx *mc13xxx; |
| 484 | struct completion done; |
| 485 | }; |
| 486 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 487 | static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 488 | { |
| 489 | struct mc13xxx_adcdone_data *adcdone_data = data; |
| 490 | |
| 491 | mc13xxx_irq_ack(adcdone_data->mc13xxx, irq); |
| 492 | |
| 493 | complete_all(&adcdone_data->done); |
| 494 | |
| 495 | return IRQ_HANDLED; |
| 496 | } |
| 497 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 498 | #define MC13XXX_ADC_WORKING (1 << 0) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 499 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 500 | int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode, |
Michael Thalmeier | 1039d76 | 2012-02-20 12:18:13 +0100 | [diff] [blame] | 501 | unsigned int channel, u8 ato, bool atox, |
| 502 | unsigned int *sample) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 503 | { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 504 | u32 adc0, adc1, old_adc0; |
| 505 | int i, ret; |
| 506 | struct mc13xxx_adcdone_data adcdone_data = { |
| 507 | .mc13xxx = mc13xxx, |
| 508 | }; |
| 509 | init_completion(&adcdone_data.done); |
| 510 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 511 | dev_dbg(mc13xxx->dev, "%s\n", __func__); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 512 | |
| 513 | mc13xxx_lock(mc13xxx); |
| 514 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 515 | if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 516 | ret = -EBUSY; |
| 517 | goto out; |
| 518 | } |
| 519 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 520 | mc13xxx->adcflags |= MC13XXX_ADC_WORKING; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 521 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 522 | mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 523 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 524 | adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2; |
| 525 | adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 526 | |
| 527 | if (channel > 7) |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 528 | adc1 |= MC13XXX_ADC1_ADSEL; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 529 | |
| 530 | switch (mode) { |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 531 | case MC13XXX_ADC_MODE_TS: |
| 532 | adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 | |
| 533 | MC13XXX_ADC0_TSMOD1; |
| 534 | adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 535 | break; |
| 536 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 537 | case MC13XXX_ADC_MODE_SINGLE_CHAN: |
Robin van der Gracht | 2161891 | 2011-11-29 12:09:03 +0100 | [diff] [blame] | 538 | adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK; |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 539 | adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT; |
| 540 | adc1 |= MC13XXX_ADC1_RAND; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 541 | break; |
| 542 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 543 | case MC13XXX_ADC_MODE_MULT_CHAN: |
Robin van der Gracht | 2161891 | 2011-11-29 12:09:03 +0100 | [diff] [blame] | 544 | adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK; |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 545 | adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | default: |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 549 | mc13xxx_unlock(mc13xxx); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 550 | return -EINVAL; |
| 551 | } |
| 552 | |
Michael Thalmeier | 1039d76 | 2012-02-20 12:18:13 +0100 | [diff] [blame] | 553 | adc1 |= ato << MC13783_ADC1_ATO_SHIFT; |
| 554 | if (atox) |
| 555 | adc1 |= MC13783_ADC1_ATOX; |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 556 | |
| 557 | dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__); |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 558 | mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE, |
| 559 | mc13xxx_handler_adcdone, __func__, &adcdone_data); |
| 560 | mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_ADCDONE); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 561 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 562 | mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0); |
| 563 | mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 564 | |
| 565 | mc13xxx_unlock(mc13xxx); |
| 566 | |
| 567 | ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ); |
| 568 | |
| 569 | if (!ret) |
| 570 | ret = -ETIMEDOUT; |
| 571 | |
| 572 | mc13xxx_lock(mc13xxx); |
| 573 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 574 | mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 575 | |
| 576 | if (ret > 0) |
| 577 | for (i = 0; i < 4; ++i) { |
| 578 | ret = mc13xxx_reg_read(mc13xxx, |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 579 | MC13XXX_ADC2, &sample[i]); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 580 | if (ret) |
| 581 | break; |
| 582 | } |
| 583 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 584 | if (mode == MC13XXX_ADC_MODE_TS) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 585 | /* restore TSMOD */ |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 586 | mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 587 | |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 588 | mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING; |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 589 | out: |
| 590 | mc13xxx_unlock(mc13xxx); |
| 591 | |
| 592 | return ret; |
| 593 | } |
Uwe Kleine-König | fec316d | 2011-08-24 15:28:21 +0200 | [diff] [blame] | 594 | EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 595 | |
| 596 | static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx, |
Samuel Ortiz | c8a03c9 | 2011-04-08 01:55:01 +0200 | [diff] [blame] | 597 | const char *format, void *pdata, size_t pdata_size) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 598 | { |
| 599 | char buf[30]; |
| 600 | const char *name = mc13xxx_get_chipname(mc13xxx); |
| 601 | |
| 602 | struct mfd_cell cell = { |
Samuel Ortiz | c8a03c9 | 2011-04-08 01:55:01 +0200 | [diff] [blame] | 603 | .platform_data = pdata, |
| 604 | .pdata_size = pdata_size, |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | /* there is no asnprintf in the kernel :-( */ |
| 608 | if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf)) |
| 609 | return -E2BIG; |
| 610 | |
| 611 | cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL); |
| 612 | if (!cell.name) |
| 613 | return -ENOMEM; |
| 614 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 615 | return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format) |
| 619 | { |
Samuel Ortiz | c8a03c9 | 2011-04-08 01:55:01 +0200 | [diff] [blame] | 620 | return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 623 | #ifdef CONFIG_OF |
| 624 | static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx) |
| 625 | { |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 626 | struct device_node *np = mc13xxx->dev->of_node; |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 627 | |
| 628 | if (!np) |
| 629 | return -ENODEV; |
| 630 | |
| 631 | if (of_get_property(np, "fsl,mc13xxx-uses-adc", NULL)) |
| 632 | mc13xxx->flags |= MC13XXX_USE_ADC; |
| 633 | |
| 634 | if (of_get_property(np, "fsl,mc13xxx-uses-codec", NULL)) |
| 635 | mc13xxx->flags |= MC13XXX_USE_CODEC; |
| 636 | |
| 637 | if (of_get_property(np, "fsl,mc13xxx-uses-rtc", NULL)) |
| 638 | mc13xxx->flags |= MC13XXX_USE_RTC; |
| 639 | |
| 640 | if (of_get_property(np, "fsl,mc13xxx-uses-touch", NULL)) |
| 641 | mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN; |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | #else |
| 646 | static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx) |
| 647 | { |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | #endif |
| 651 | |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 652 | int mc13xxx_common_init(struct mc13xxx *mc13xxx, |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 653 | struct mc13xxx_platform_data *pdata, int irq) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 654 | { |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 655 | int ret; |
| 656 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 657 | mc13xxx_lock(mc13xxx); |
| 658 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 659 | ret = mc13xxx_identify(mc13xxx); |
| 660 | if (ret) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 661 | goto err_revision; |
| 662 | |
| 663 | /* mask all irqs */ |
| 664 | ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK0, 0x00ffffff); |
| 665 | if (ret) |
| 666 | goto err_mask; |
| 667 | |
| 668 | ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK1, 0x00ffffff); |
| 669 | if (ret) |
| 670 | goto err_mask; |
| 671 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 672 | ret = request_threaded_irq(irq, NULL, mc13xxx_irq_thread, |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 673 | IRQF_ONESHOT | IRQF_TRIGGER_HIGH, "mc13xxx", mc13xxx); |
| 674 | |
| 675 | if (ret) { |
| 676 | err_mask: |
| 677 | err_revision: |
Uwe Kleine-König | e1b88eb | 2010-11-11 16:47:50 +0100 | [diff] [blame] | 678 | mc13xxx_unlock(mc13xxx); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 679 | kfree(mc13xxx); |
| 680 | return ret; |
| 681 | } |
| 682 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 683 | mc13xxx->irq = irq; |
| 684 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 685 | mc13xxx_unlock(mc13xxx); |
| 686 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 687 | if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata) |
| 688 | mc13xxx->flags = pdata->flags; |
| 689 | |
| 690 | if (mc13xxx->flags & MC13XXX_USE_ADC) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 691 | mc13xxx_add_subdevice(mc13xxx, "%s-adc"); |
| 692 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 693 | if (mc13xxx->flags & MC13XXX_USE_CODEC) |
Philippe Rétornaz | e3a0871 | 2012-05-15 13:53:49 +0200 | [diff] [blame] | 694 | mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec", |
| 695 | pdata->codec, sizeof(*pdata->codec)); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 696 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 697 | if (mc13xxx->flags & MC13XXX_USE_RTC) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 698 | mc13xxx_add_subdevice(mc13xxx, "%s-rtc"); |
| 699 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 700 | if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN) |
Michael Thalmeier | 1039d76 | 2012-02-20 12:18:13 +0100 | [diff] [blame] | 701 | mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts", |
| 702 | &pdata->touch, sizeof(pdata->touch)); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 703 | |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 704 | if (pdata) { |
| 705 | mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator", |
| 706 | &pdata->regulators, sizeof(pdata->regulators)); |
Samuel Ortiz | c8a03c9 | 2011-04-08 01:55:01 +0200 | [diff] [blame] | 707 | mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led", |
| 708 | pdata->leds, sizeof(*pdata->leds)); |
Philippe Rétornaz | 30fc7ac | 2011-09-18 18:10:53 +0200 | [diff] [blame] | 709 | mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton", |
| 710 | pdata->buttons, sizeof(*pdata->buttons)); |
Shawn Guo | 876989d | 2011-12-12 18:52:57 +0100 | [diff] [blame] | 711 | } else { |
| 712 | mc13xxx_add_subdevice(mc13xxx, "%s-regulator"); |
| 713 | mc13xxx_add_subdevice(mc13xxx, "%s-led"); |
| 714 | mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton"); |
| 715 | } |
Philippe Rétornaz | 30fc7ac | 2011-09-18 18:10:53 +0200 | [diff] [blame] | 716 | |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 717 | return 0; |
| 718 | } |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 719 | EXPORT_SYMBOL_GPL(mc13xxx_common_init); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 720 | |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 721 | void mc13xxx_common_cleanup(struct mc13xxx *mc13xxx) |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 722 | { |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 723 | free_irq(mc13xxx->irq, mc13xxx); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 724 | |
Marc Reilly | 5006fe5 | 2012-05-01 12:26:46 +0200 | [diff] [blame] | 725 | mfd_remove_devices(mc13xxx->dev); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 726 | |
Marc Reilly | 91b5e74 | 2012-04-01 16:41:37 +1000 | [diff] [blame] | 727 | regmap_exit(mc13xxx->regmap); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 728 | |
Axel Lin | cef92fe | 2010-10-22 12:29:21 +0200 | [diff] [blame] | 729 | kfree(mc13xxx); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 730 | } |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 731 | EXPORT_SYMBOL_GPL(mc13xxx_common_cleanup); |
Uwe Kleine-König | 8e00593 | 2010-09-28 16:37:20 +0200 | [diff] [blame] | 732 | |
| 733 | MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC"); |
| 734 | MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>"); |
| 735 | MODULE_LICENSE("GPL v2"); |