diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/mfd | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'drivers/mfd')
| -rw-r--r-- | drivers/mfd/ab3550-core.c | 1380 | ||||
| -rw-r--r-- | drivers/mfd/ab8500-i2c.c | 104 | ||||
| -rw-r--r-- | drivers/mfd/db5500-prcmu-regs.h | 115 | ||||
| -rw-r--r-- | drivers/mfd/db5500-prcmu.c | 448 | ||||
| -rw-r--r-- | drivers/mfd/db8500-prcmu-regs.h | 166 | ||||
| -rw-r--r-- | drivers/mfd/max77663-core.c | 1445 | ||||
| -rw-r--r-- | drivers/mfd/max8907c-irq.c | 425 | ||||
| -rw-r--r-- | drivers/mfd/max8907c.c | 376 | ||||
| -rw-r--r-- | drivers/mfd/ricoh583.c | 1213 | ||||
| -rw-r--r-- | drivers/mfd/tps65910-irq.c | 232 | ||||
| -rw-r--r-- | drivers/mfd/tps6591x.c | 930 | ||||
| -rw-r--r-- | drivers/mfd/tps8003x-gpadc.c | 650 | ||||
| -rw-r--r-- | drivers/mfd/twl6030-pwm.c | 165 | ||||
| -rw-r--r-- | drivers/mfd/twl6040-core.c | 620 | ||||
| -rw-r--r-- | drivers/mfd/twl6040-irq.c | 191 |
15 files changed, 8460 insertions, 0 deletions
diff --git a/drivers/mfd/ab3550-core.c b/drivers/mfd/ab3550-core.c new file mode 100644 index 00000000000..56ba1943c91 --- /dev/null +++ b/drivers/mfd/ab3550-core.c | |||
| @@ -0,0 +1,1380 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2010 ST-Ericsson | ||
| 3 | * License terms: GNU General Public License (GPL) version 2 | ||
| 4 | * Low-level core for exclusive access to the AB3550 IC on the I2C bus | ||
| 5 | * and some basic chip-configuration. | ||
| 6 | * Author: Bengt Jonsson <bengt.g.jonsson@stericsson.com> | ||
| 7 | * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> | ||
| 8 | * Author: Mattias Wallin <mattias.wallin@stericsson.com> | ||
| 9 | * Author: Rickard Andersson <rickard.andersson@stericsson.com> | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/i2c.h> | ||
| 13 | #include <linux/mutex.h> | ||
| 14 | #include <linux/err.h> | ||
| 15 | #include <linux/platform_device.h> | ||
| 16 | #include <linux/slab.h> | ||
| 17 | #include <linux/device.h> | ||
| 18 | #include <linux/irq.h> | ||
| 19 | #include <linux/interrupt.h> | ||
| 20 | #include <linux/random.h> | ||
| 21 | #include <linux/workqueue.h> | ||
| 22 | #include <linux/debugfs.h> | ||
| 23 | #include <linux/seq_file.h> | ||
| 24 | #include <linux/uaccess.h> | ||
| 25 | #include <linux/mfd/abx500.h> | ||
| 26 | #include <linux/list.h> | ||
| 27 | #include <linux/bitops.h> | ||
| 28 | #include <linux/spinlock.h> | ||
| 29 | #include <linux/mfd/core.h> | ||
| 30 | |||
| 31 | #define AB3550_NAME_STRING "ab3550" | ||
| 32 | #define AB3550_ID_FORMAT_STRING "AB3550 %s" | ||
| 33 | #define AB3550_NUM_BANKS 2 | ||
| 34 | #define AB3550_NUM_EVENT_REG 5 | ||
| 35 | |||
| 36 | /* These are the only registers inside AB3550 used in this main file */ | ||
| 37 | |||
| 38 | /* Chip ID register */ | ||
| 39 | #define AB3550_CID_REG 0x20 | ||
| 40 | |||
| 41 | /* Interrupt event registers */ | ||
| 42 | #define AB3550_EVENT_BANK 0 | ||
| 43 | #define AB3550_EVENT_REG 0x22 | ||
| 44 | |||
| 45 | /* Read/write operation values. */ | ||
| 46 | #define AB3550_PERM_RD (0x01) | ||
| 47 | #define AB3550_PERM_WR (0x02) | ||
| 48 | |||
| 49 | /* Read/write permissions. */ | ||
| 50 | #define AB3550_PERM_RO (AB3550_PERM_RD) | ||
| 51 | #define AB3550_PERM_RW (AB3550_PERM_RD | AB3550_PERM_WR) | ||
| 52 | |||
| 53 | /** | ||
| 54 | * struct ab3550 | ||
| 55 | * @access_mutex: lock out concurrent accesses to the AB registers | ||
| 56 | * @i2c_client: I2C client for this chip | ||
| 57 | * @chip_name: name of this chip variant | ||
| 58 | * @chip_id: 8 bit chip ID for this chip variant | ||
| 59 | * @mask_work: a worker for writing to mask registers | ||
| 60 | * @event_lock: a lock to protect the event_mask | ||
| 61 | * @event_mask: a local copy of the mask event registers | ||
| 62 | * @startup_events: a copy of the first reading of the event registers | ||
| 63 | * @startup_events_read: whether the first events have been read | ||
| 64 | */ | ||
| 65 | struct ab3550 { | ||
| 66 | struct mutex access_mutex; | ||
| 67 | struct i2c_client *i2c_client[AB3550_NUM_BANKS]; | ||
| 68 | char chip_name[32]; | ||
| 69 | u8 chip_id; | ||
| 70 | struct work_struct mask_work; | ||
| 71 | spinlock_t event_lock; | ||
| 72 | u8 event_mask[AB3550_NUM_EVENT_REG]; | ||
| 73 | u8 startup_events[AB3550_NUM_EVENT_REG]; | ||
| 74 | bool startup_events_read; | ||
| 75 | #ifdef CONFIG_DEBUG_FS | ||
| 76 | unsigned int debug_bank; | ||
| 77 | unsigned int debug_address; | ||
| 78 | #endif | ||
| 79 | }; | ||
| 80 | |||
| 81 | /** | ||
| 82 | * struct ab3550_reg_range | ||
| 83 | * @first: the first address of the range | ||
| 84 | * @last: the last address of the range | ||
| 85 | * @perm: access permissions for the range | ||
| 86 | */ | ||
| 87 | struct ab3550_reg_range { | ||
| 88 | u8 first; | ||
| 89 | u8 last; | ||
| 90 | u8 perm; | ||
| 91 | }; | ||
| 92 | |||
| 93 | /** | ||
| 94 | * struct ab3550_reg_ranges | ||
| 95 | * @count: the number of ranges in the list | ||
| 96 | * @range: the list of register ranges | ||
| 97 | */ | ||
| 98 | struct ab3550_reg_ranges { | ||
| 99 | u8 count; | ||
| 100 | const struct ab3550_reg_range *range; | ||
| 101 | }; | ||
| 102 | |||
| 103 | /* | ||
| 104 | * Permissible register ranges for reading and writing per device and bank. | ||
| 105 | * | ||
| 106 | * The ranges must be listed in increasing address order, and no overlaps are | ||
| 107 | * allowed. It is assumed that write permission implies read permission | ||
| 108 | * (i.e. only RO and RW permissions should be used). Ranges with write | ||
| 109 | * permission must not be split up. | ||
| 110 | */ | ||
| 111 | |||
| 112 | #define NO_RANGE {.count = 0, .range = NULL,} | ||
| 113 | |||
| 114 | static struct | ||
| 115 | ab3550_reg_ranges ab3550_reg_ranges[AB3550_NUM_DEVICES][AB3550_NUM_BANKS] = { | ||
| 116 | [AB3550_DEVID_DAC] = { | ||
| 117 | NO_RANGE, | ||
| 118 | { | ||
| 119 | .count = 2, | ||
| 120 | .range = (struct ab3550_reg_range[]) { | ||
| 121 | { | ||
| 122 | .first = 0xb0, | ||
| 123 | .last = 0xba, | ||
| 124 | .perm = AB3550_PERM_RW, | ||
| 125 | }, | ||
| 126 | { | ||
| 127 | .first = 0xbc, | ||
| 128 | .last = 0xc3, | ||
| 129 | .perm = AB3550_PERM_RW, | ||
| 130 | }, | ||
| 131 | }, | ||
| 132 | }, | ||
| 133 | }, | ||
| 134 | [AB3550_DEVID_LEDS] = { | ||
| 135 | NO_RANGE, | ||
| 136 | { | ||
| 137 | .count = 2, | ||
| 138 | .range = (struct ab3550_reg_range[]) { | ||
| 139 | { | ||
| 140 | .first = 0x5a, | ||
| 141 | .last = 0x88, | ||
| 142 | .perm = AB3550_PERM_RW, | ||
| 143 | }, | ||
| 144 | { | ||
| 145 | .first = 0x8a, | ||
| 146 | .last = 0xad, | ||
| 147 | .perm = AB3550_PERM_RW, | ||
| 148 | }, | ||
| 149 | } | ||
| 150 | }, | ||
| 151 | }, | ||
| 152 | [AB3550_DEVID_POWER] = { | ||
| 153 | { | ||
| 154 | .count = 1, | ||
| 155 | .range = (struct ab3550_reg_range[]) { | ||
| 156 | { | ||
| 157 | .first = 0x21, | ||
| 158 | .last = 0x21, | ||
| 159 | .perm = AB3550_PERM_RO, | ||
| 160 | }, | ||
| 161 | } | ||
| 162 | }, | ||
| 163 | NO_RANGE, | ||
| 164 | }, | ||
| 165 | [AB3550_DEVID_REGULATORS] = { | ||
| 166 | { | ||
| 167 | .count = 1, | ||
| 168 | .range = (struct ab3550_reg_range[]) { | ||
| 169 | { | ||
| 170 | .first = 0x69, | ||
| 171 | .last = 0xa3, | ||
| 172 | .perm = AB3550_PERM_RW, | ||
| 173 | }, | ||
| 174 | } | ||
| 175 | }, | ||
| 176 | { | ||
| 177 | .count = 1, | ||
| 178 | .range = (struct ab3550_reg_range[]) { | ||
| 179 | { | ||
| 180 | .first = 0x14, | ||
| 181 | .last = 0x16, | ||
| 182 | .perm = AB3550_PERM_RW, | ||
| 183 | }, | ||
| 184 | } | ||
| 185 | }, | ||
| 186 | }, | ||
| 187 | [AB3550_DEVID_SIM] = { | ||
| 188 | { | ||
| 189 | .count = 1, | ||
| 190 | .range = (struct ab3550_reg_range[]) { | ||
| 191 | { | ||
| 192 | .first = 0x21, | ||
| 193 | .last = 0x21, | ||
| 194 | .perm = AB3550_PERM_RO, | ||
| 195 | }, | ||
| 196 | } | ||
| 197 | }, | ||
| 198 | { | ||
| 199 | .count = 1, | ||
| 200 | .range = (struct ab3550_reg_range[]) { | ||
| 201 | { | ||
| 202 | .first = 0x14, | ||
| 203 | .last = 0x17, | ||
| 204 | .perm = AB3550_PERM_RW, | ||
| 205 | }, | ||
| 206 | } | ||
| 207 | |||
| 208 | }, | ||
| 209 | }, | ||
| 210 | [AB3550_DEVID_UART] = { | ||
| 211 | NO_RANGE, | ||
| 212 | NO_RANGE, | ||
| 213 | }, | ||
| 214 | [AB3550_DEVID_RTC] = { | ||
| 215 | { | ||
| 216 | .count = 1, | ||
| 217 | .range = (struct ab3550_reg_range[]) { | ||
| 218 | { | ||
| 219 | .first = 0x00, | ||
| 220 | .last = 0x0c, | ||
| 221 | .perm = AB3550_PERM_RW, | ||
| 222 | }, | ||
| 223 | } | ||
| 224 | }, | ||
| 225 | NO_RANGE, | ||
| 226 | }, | ||
| 227 | [AB3550_DEVID_CHARGER] = { | ||
| 228 | { | ||
| 229 | .count = 2, | ||
| 230 | .range = (struct ab3550_reg_range[]) { | ||
| 231 | { | ||
| 232 | .first = 0x10, | ||
| 233 | .last = 0x1a, | ||
| 234 | .perm = AB3550_PERM_RW, | ||
| 235 | }, | ||
| 236 | { | ||
| 237 | .first = 0x21, | ||
| 238 | .last = 0x21, | ||
| 239 | .perm = AB3550_PERM_RO, | ||
| 240 | }, | ||
| 241 | } | ||
| 242 | }, | ||
| 243 | NO_RANGE, | ||
| 244 | }, | ||
| 245 | [AB3550_DEVID_ADC] = { | ||
| 246 | NO_RANGE, | ||
| 247 | { | ||
| 248 | .count = 1, | ||
| 249 | .range = (struct ab3550_reg_range[]) { | ||
| 250 | { | ||
| 251 | .first = 0x20, | ||
| 252 | .last = 0x56, | ||
| 253 | .perm = AB3550_PERM_RW, | ||
| 254 | }, | ||
| 255 | |||
| 256 | } | ||
| 257 | }, | ||
| 258 | }, | ||
| 259 | [AB3550_DEVID_FUELGAUGE] = { | ||
| 260 | { | ||
| 261 | .count = 1, | ||
| 262 | .range = (struct ab3550_reg_range[]) { | ||
| 263 | { | ||
| 264 | .first = 0x21, | ||
| 265 | .last = 0x21, | ||
| 266 | .perm = AB3550_PERM_RO, | ||
| 267 | }, | ||
| 268 | } | ||
| 269 | }, | ||
| 270 | { | ||
| 271 | .count = 1, | ||
| 272 | .range = (struct ab3550_reg_range[]) { | ||
| 273 | { | ||
| 274 | .first = 0x00, | ||
| 275 | .last = 0x0e, | ||
| 276 | .perm = AB3550_PERM_RW, | ||
| 277 | }, | ||
| 278 | } | ||
| 279 | }, | ||
| 280 | }, | ||
| 281 | [AB3550_DEVID_VIBRATOR] = { | ||
| 282 | NO_RANGE, | ||
| 283 | { | ||
| 284 | .count = 1, | ||
| 285 | .range = (struct ab3550_reg_range[]) { | ||
| 286 | { | ||
| 287 | .first = 0x10, | ||
| 288 | .last = 0x13, | ||
| 289 | .perm = AB3550_PERM_RW, | ||
| 290 | }, | ||
| 291 | |||
| 292 | } | ||
| 293 | }, | ||
| 294 | }, | ||
| 295 | [AB3550_DEVID_CODEC] = { | ||
| 296 | { | ||
| 297 | .count = 2, | ||
| 298 | .range = (struct ab3550_reg_range[]) { | ||
| 299 | { | ||
| 300 | .first = 0x31, | ||
| 301 | .last = 0x63, | ||
| 302 | .perm = AB3550_PERM_RW, | ||
| 303 | }, | ||
| 304 | { | ||
| 305 | .first = 0x65, | ||
| 306 | .last = 0x68, | ||
| 307 | .perm = AB3550_PERM_RW, | ||
| 308 | }, | ||
| 309 | } | ||
| 310 | }, | ||
| 311 | NO_RANGE, | ||
| 312 | }, | ||
| 313 | }; | ||
| 314 | |||
| 315 | static struct mfd_cell ab3550_devs[AB3550_NUM_DEVICES] = { | ||
| 316 | [AB3550_DEVID_DAC] = { | ||
| 317 | .name = "ab3550-dac", | ||
| 318 | .id = AB3550_DEVID_DAC, | ||
| 319 | .num_resources = 0, | ||
| 320 | }, | ||
| 321 | [AB3550_DEVID_LEDS] = { | ||
| 322 | .name = "ab3550-leds", | ||
| 323 | .id = AB3550_DEVID_LEDS, | ||
| 324 | }, | ||
| 325 | [AB3550_DEVID_POWER] = { | ||
| 326 | .name = "ab3550-power", | ||
| 327 | .id = AB3550_DEVID_POWER, | ||
| 328 | }, | ||
| 329 | [AB3550_DEVID_REGULATORS] = { | ||
| 330 | .name = "ab3550-regulators", | ||
| 331 | .id = AB3550_DEVID_REGULATORS, | ||
| 332 | }, | ||
| 333 | [AB3550_DEVID_SIM] = { | ||
| 334 | .name = "ab3550-sim", | ||
| 335 | .id = AB3550_DEVID_SIM, | ||
| 336 | }, | ||
| 337 | [AB3550_DEVID_UART] = { | ||
| 338 | .name = "ab3550-uart", | ||
| 339 | .id = AB3550_DEVID_UART, | ||
| 340 | }, | ||
| 341 | [AB3550_DEVID_RTC] = { | ||
| 342 | .name = "ab3550-rtc", | ||
| 343 | .id = AB3550_DEVID_RTC, | ||
| 344 | }, | ||
| 345 | [AB3550_DEVID_CHARGER] = { | ||
| 346 | .name = "ab3550-charger", | ||
| 347 | .id = AB3550_DEVID_CHARGER, | ||
| 348 | }, | ||
| 349 | [AB3550_DEVID_ADC] = { | ||
| 350 | .name = "ab3550-adc", | ||
| 351 | .id = AB3550_DEVID_ADC, | ||
| 352 | .num_resources = 10, | ||
| 353 | .resources = (struct resource[]) { | ||
| 354 | { | ||
| 355 | .name = "TRIGGER-0", | ||
| 356 | .flags = IORESOURCE_IRQ, | ||
| 357 | .start = 16, | ||
| 358 | .end = 16, | ||
| 359 | }, | ||
| 360 | { | ||
| 361 | .name = "TRIGGER-1", | ||
| 362 | .flags = IORESOURCE_IRQ, | ||
| 363 | .start = 17, | ||
| 364 | .end = 17, | ||
| 365 | }, | ||
| 366 | { | ||
| 367 | .name = "TRIGGER-2", | ||
| 368 | .flags = IORESOURCE_IRQ, | ||
| 369 | .start = 18, | ||
| 370 | .end = 18, | ||
| 371 | }, | ||
| 372 | { | ||
| 373 | .name = "TRIGGER-3", | ||
| 374 | .flags = IORESOURCE_IRQ, | ||
| 375 | .start = 19, | ||
| 376 | .end = 19, | ||
| 377 | }, | ||
| 378 | { | ||
| 379 | .name = "TRIGGER-4", | ||
| 380 | .flags = IORESOURCE_IRQ, | ||
| 381 | .start = 20, | ||
| 382 | .end = 20, | ||
| 383 | }, | ||
| 384 | { | ||
| 385 | .name = "TRIGGER-5", | ||
| 386 | .flags = IORESOURCE_IRQ, | ||
| 387 | .start = 21, | ||
| 388 | .end = 21, | ||
| 389 | }, | ||
| 390 | { | ||
| 391 | .name = "TRIGGER-6", | ||
| 392 | .flags = IORESOURCE_IRQ, | ||
| 393 | .start = 22, | ||
| 394 | .end = 22, | ||
| 395 | }, | ||
| 396 | { | ||
| 397 | .name = "TRIGGER-7", | ||
| 398 | .flags = IORESOURCE_IRQ, | ||
| 399 | .start = 23, | ||
| 400 | .end = 23, | ||
| 401 | }, | ||
| 402 | { | ||
| 403 | .name = "TRIGGER-VBAT-TXON", | ||
| 404 | .flags = IORESOURCE_IRQ, | ||
| 405 | .start = 13, | ||
| 406 | .end = 13, | ||
| 407 | }, | ||
| 408 | { | ||
| 409 | .name = "TRIGGER-VBAT", | ||
| 410 | .flags = IORESOURCE_IRQ, | ||
| 411 | .start = 12, | ||
| 412 | .end = 12, | ||
| 413 | }, | ||
| 414 | }, | ||
| 415 | }, | ||
| 416 | [AB3550_DEVID_FUELGAUGE] = { | ||
| 417 | .name = "ab3550-fuelgauge", | ||
| 418 | .id = AB3550_DEVID_FUELGAUGE, | ||
| 419 | }, | ||
| 420 | [AB3550_DEVID_VIBRATOR] = { | ||
| 421 | .name = "ab3550-vibrator", | ||
| 422 | .id = AB3550_DEVID_VIBRATOR, | ||
| 423 | }, | ||
| 424 | [AB3550_DEVID_CODEC] = { | ||
| 425 | .name = "ab3550-codec", | ||
| 426 | .id = AB3550_DEVID_CODEC, | ||
| 427 | }, | ||
| 428 | }; | ||
| 429 | |||
| 430 | /* | ||
| 431 | * I2C transactions with error messages. | ||
| 432 | */ | ||
| 433 | static int ab3550_i2c_master_send(struct ab3550 *ab, u8 bank, u8 *data, | ||
| 434 | u8 count) | ||
| 435 | { | ||
| 436 | int err; | ||
| 437 | |||
| 438 | err = i2c_master_send(ab->i2c_client[bank], data, count); | ||
| 439 | if (err < 0) { | ||
| 440 | dev_err(&ab->i2c_client[0]->dev, "send error: %d\n", err); | ||
| 441 | return err; | ||
| 442 | } | ||
| 443 | return 0; | ||
| 444 | } | ||
| 445 | |||
| 446 | static int ab3550_i2c_master_recv(struct ab3550 *ab, u8 bank, u8 *data, | ||
| 447 | u8 count) | ||
| 448 | { | ||
| 449 | int err; | ||
| 450 | |||
| 451 | err = i2c_master_recv(ab->i2c_client[bank], data, count); | ||
| 452 | if (err < 0) { | ||
| 453 | dev_err(&ab->i2c_client[0]->dev, "receive error: %d\n", err); | ||
| 454 | return err; | ||
| 455 | } | ||
| 456 | return 0; | ||
| 457 | } | ||
| 458 | |||
| 459 | /* | ||
| 460 | * Functionality for getting/setting register values. | ||
| 461 | */ | ||
| 462 | static int get_register_interruptible(struct ab3550 *ab, u8 bank, u8 reg, | ||
| 463 | u8 *value) | ||
| 464 | { | ||
| 465 | int err; | ||
| 466 | |||
| 467 | err = mutex_lock_interruptible(&ab->access_mutex); | ||
| 468 | if (err) | ||
| 469 | return err; | ||
| 470 | |||
| 471 | err = ab3550_i2c_master_send(ab, bank, ®, 1); | ||
| 472 | if (!err) | ||
| 473 | err = ab3550_i2c_master_recv(ab, bank, value, 1); | ||
| 474 | |||
| 475 | mutex_unlock(&ab->access_mutex); | ||
| 476 | return err; | ||
| 477 | } | ||
| 478 | |||
| 479 | static int get_register_page_interruptible(struct ab3550 *ab, u8 bank, | ||
| 480 | u8 first_reg, u8 *regvals, u8 numregs) | ||
| 481 | { | ||
| 482 | int err; | ||
| 483 | |||
| 484 | err = mutex_lock_interruptible(&ab->access_mutex); | ||
| 485 | if (err) | ||
| 486 | return err; | ||
| 487 | |||
| 488 | err = ab3550_i2c_master_send(ab, bank, &first_reg, 1); | ||
| 489 | if (!err) | ||
| 490 | err = ab3550_i2c_master_recv(ab, bank, regvals, numregs); | ||
| 491 | |||
| 492 | mutex_unlock(&ab->access_mutex); | ||
| 493 | return err; | ||
| 494 | } | ||
| 495 | |||
| 496 | static int mask_and_set_register_interruptible(struct ab3550 *ab, u8 bank, | ||
| 497 | u8 reg, u8 bitmask, u8 bitvalues) | ||
| 498 | { | ||
| 499 | int err = 0; | ||
| 500 | |||
| 501 | if (likely(bitmask)) { | ||
| 502 | u8 reg_bits[2] = {reg, 0}; | ||
| 503 | |||
| 504 | err = mutex_lock_interruptible(&ab->access_mutex); | ||
| 505 | if (err) | ||
| 506 | return err; | ||
| 507 | |||
| 508 | if (bitmask == 0xFF) /* No need to read in this case. */ | ||
| 509 | reg_bits[1] = bitvalues; | ||
| 510 | else { /* Read and modify the register value. */ | ||
| 511 | u8 bits; | ||
| 512 | |||
| 513 | err = ab3550_i2c_master_send(ab, bank, ®, 1); | ||
| 514 | if (err) | ||
| 515 | goto unlock_and_return; | ||
| 516 | err = ab3550_i2c_master_recv(ab, bank, &bits, 1); | ||
| 517 | if (err) | ||
| 518 | goto unlock_and_return; | ||
| 519 | reg_bits[1] = ((~bitmask & bits) | | ||
| 520 | (bitmask & bitvalues)); | ||
| 521 | } | ||
| 522 | /* Write the new value. */ | ||
| 523 | err = ab3550_i2c_master_send(ab, bank, reg_bits, 2); | ||
| 524 | unlock_and_return: | ||
| 525 | mutex_unlock(&ab->access_mutex); | ||
| 526 | } | ||
| 527 | return err; | ||
| 528 | } | ||
| 529 | |||
| 530 | /* | ||
| 531 | * Read/write permission checking functions. | ||
| 532 | */ | ||
| 533 | static bool page_write_allowed(const struct ab3550_reg_ranges *ranges, | ||
| 534 | u8 first_reg, u8 last_reg) | ||
| 535 | { | ||
| 536 | u8 i; | ||
| 537 | |||
| 538 | if (last_reg < first_reg) | ||
| 539 | return false; | ||
| 540 | |||
| 541 | for (i = 0; i < ranges->count; i++) { | ||
| 542 | if (first_reg < ranges->range[i].first) | ||
| 543 | break; | ||
| 544 | if ((last_reg <= ranges->range[i].last) && | ||
| 545 | (ranges->range[i].perm & AB3550_PERM_WR)) | ||
| 546 | return true; | ||
| 547 | } | ||
| 548 | return false; | ||
| 549 | } | ||
| 550 | |||
| 551 | static bool reg_write_allowed(const struct ab3550_reg_ranges *ranges, u8 reg) | ||
| 552 | { | ||
| 553 | return page_write_allowed(ranges, reg, reg); | ||
| 554 | } | ||
| 555 | |||
| 556 | static bool page_read_allowed(const struct ab3550_reg_ranges *ranges, | ||
| 557 | u8 first_reg, u8 last_reg) | ||
| 558 | { | ||
| 559 | u8 i; | ||
| 560 | |||
| 561 | if (last_reg < first_reg) | ||
| 562 | return false; | ||
| 563 | /* Find the range (if it exists in the list) that includes first_reg. */ | ||
| 564 | for (i = 0; i < ranges->count; i++) { | ||
| 565 | if (first_reg < ranges->range[i].first) | ||
| 566 | return false; | ||
| 567 | if (first_reg <= ranges->range[i].last) | ||
| 568 | break; | ||
| 569 | } | ||
| 570 | /* Make sure that the entire range up to and including last_reg is | ||
| 571 | * readable. This may span several of the ranges in the list. | ||
| 572 | */ | ||
| 573 | while ((i < ranges->count) && | ||
| 574 | (ranges->range[i].perm & AB3550_PERM_RD)) { | ||
| 575 | if (last_reg <= ranges->range[i].last) | ||
| 576 | return true; | ||
| 577 | if ((++i >= ranges->count) || | ||
| 578 | (ranges->range[i].first != | ||
| 579 | (ranges->range[i - 1].last + 1))) { | ||
| 580 | break; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | return false; | ||
| 584 | } | ||
| 585 | |||
| 586 | static bool reg_read_allowed(const struct ab3550_reg_ranges *ranges, u8 reg) | ||
| 587 | { | ||
| 588 | return page_read_allowed(ranges, reg, reg); | ||
| 589 | } | ||
| 590 | |||
| 591 | /* | ||
| 592 | * The register access functionality. | ||
| 593 | */ | ||
| 594 | static int ab3550_get_chip_id(struct device *dev) | ||
| 595 | { | ||
| 596 | struct ab3550 *ab = dev_get_drvdata(dev->parent); | ||
| 597 | return (int)ab->chip_id; | ||
| 598 | } | ||
| 599 | |||
| 600 | static int ab3550_mask_and_set_register_interruptible(struct device *dev, | ||
| 601 | u8 bank, u8 reg, u8 bitmask, u8 bitvalues) | ||
| 602 | { | ||
| 603 | struct ab3550 *ab; | ||
| 604 | struct platform_device *pdev = to_platform_device(dev); | ||
| 605 | |||
| 606 | if ((AB3550_NUM_BANKS <= bank) || | ||
| 607 | !reg_write_allowed(&ab3550_reg_ranges[pdev->id][bank], reg)) | ||
| 608 | return -EINVAL; | ||
| 609 | |||
| 610 | ab = dev_get_drvdata(dev->parent); | ||
| 611 | return mask_and_set_register_interruptible(ab, bank, reg, | ||
| 612 | bitmask, bitvalues); | ||
| 613 | } | ||
| 614 | |||
| 615 | static int ab3550_set_register_interruptible(struct device *dev, u8 bank, | ||
| 616 | u8 reg, u8 value) | ||
| 617 | { | ||
| 618 | return ab3550_mask_and_set_register_interruptible(dev, bank, reg, 0xFF, | ||
| 619 | value); | ||
| 620 | } | ||
| 621 | |||
| 622 | static int ab3550_get_register_interruptible(struct device *dev, u8 bank, | ||
| 623 | u8 reg, u8 *value) | ||
| 624 | { | ||
| 625 | struct ab3550 *ab; | ||
| 626 | struct platform_device *pdev = to_platform_device(dev); | ||
| 627 | |||
| 628 | if ((AB3550_NUM_BANKS <= bank) || | ||
| 629 | !reg_read_allowed(&ab3550_reg_ranges[pdev->id][bank], reg)) | ||
| 630 | return -EINVAL; | ||
| 631 | |||
| 632 | ab = dev_get_drvdata(dev->parent); | ||
| 633 | return get_register_interruptible(ab, bank, reg, value); | ||
| 634 | } | ||
| 635 | |||
| 636 | static int ab3550_get_register_page_interruptible(struct device *dev, u8 bank, | ||
| 637 | u8 first_reg, u8 *regvals, u8 numregs) | ||
| 638 | { | ||
| 639 | struct ab3550 *ab; | ||
| 640 | struct platform_device *pdev = to_platform_device(dev); | ||
| 641 | |||
| 642 | if ((AB3550_NUM_BANKS <= bank) || | ||
| 643 | !page_read_allowed(&ab3550_reg_ranges[pdev->id][bank], | ||
| 644 | first_reg, (first_reg + numregs - 1))) | ||
| 645 | return -EINVAL; | ||
| 646 | |||
| 647 | ab = dev_get_drvdata(dev->parent); | ||
| 648 | return get_register_page_interruptible(ab, bank, first_reg, regvals, | ||
| 649 | numregs); | ||
| 650 | } | ||
| 651 | |||
| 652 | static int ab3550_event_registers_startup_state_get(struct device *dev, | ||
| 653 | u8 *event) | ||
| 654 | { | ||
| 655 | struct ab3550 *ab; | ||
| 656 | |||
| 657 | ab = dev_get_drvdata(dev->parent); | ||
| 658 | if (!ab->startup_events_read) | ||
| 659 | return -EAGAIN; /* Try again later */ | ||
| 660 | |||
| 661 | memcpy(event, ab->startup_events, AB3550_NUM_EVENT_REG); | ||
| 662 | return 0; | ||
| 663 | } | ||
| 664 | |||
| 665 | static int ab3550_startup_irq_enabled(struct device *dev, unsigned int irq) | ||
| 666 | { | ||
| 667 | struct ab3550 *ab; | ||
| 668 | struct ab3550_platform_data *plf_data; | ||
| 669 | bool val; | ||
| 670 | |||
| 671 | ab = irq_get_chip_data(irq); | ||
| 672 | plf_data = ab->i2c_client[0]->dev.platform_data; | ||
| 673 | irq -= plf_data->irq.base; | ||
| 674 | val = ((ab->startup_events[irq / 8] & BIT(irq % 8)) != 0); | ||
| 675 | |||
| 676 | return val; | ||
| 677 | } | ||
| 678 | |||
| 679 | static struct abx500_ops ab3550_ops = { | ||
| 680 | .get_chip_id = ab3550_get_chip_id, | ||
| 681 | .get_register = ab3550_get_register_interruptible, | ||
| 682 | .set_register = ab3550_set_register_interruptible, | ||
| 683 | .get_register_page = ab3550_get_register_page_interruptible, | ||
| 684 | .set_register_page = NULL, | ||
| 685 | .mask_and_set_register = ab3550_mask_and_set_register_interruptible, | ||
| 686 | .event_registers_startup_state_get = | ||
| 687 | ab3550_event_registers_startup_state_get, | ||
| 688 | .startup_irq_enabled = ab3550_startup_irq_enabled, | ||
| 689 | }; | ||
| 690 | |||
| 691 | static irqreturn_t ab3550_irq_handler(int irq, void *data) | ||
| 692 | { | ||
| 693 | struct ab3550 *ab = data; | ||
| 694 | int err; | ||
| 695 | unsigned int i; | ||
| 696 | u8 e[AB3550_NUM_EVENT_REG]; | ||
| 697 | u8 *events; | ||
| 698 | unsigned long flags; | ||
| 699 | |||
| 700 | events = (ab->startup_events_read ? e : ab->startup_events); | ||
| 701 | |||
| 702 | err = get_register_page_interruptible(ab, AB3550_EVENT_BANK, | ||
| 703 | AB3550_EVENT_REG, events, AB3550_NUM_EVENT_REG); | ||
| 704 | if (err) | ||
| 705 | goto err_event_rd; | ||
| 706 | |||
| 707 | if (!ab->startup_events_read) { | ||
| 708 | dev_info(&ab->i2c_client[0]->dev, | ||
| 709 | "startup events 0x%x,0x%x,0x%x,0x%x,0x%x\n", | ||
| 710 | ab->startup_events[0], ab->startup_events[1], | ||
| 711 | ab->startup_events[2], ab->startup_events[3], | ||
| 712 | ab->startup_events[4]); | ||
| 713 | ab->startup_events_read = true; | ||
| 714 | goto out; | ||
| 715 | } | ||
| 716 | |||
| 717 | /* The two highest bits in event[4] are not used. */ | ||
| 718 | events[4] &= 0x3f; | ||
| 719 | |||
| 720 | spin_lock_irqsave(&ab->event_lock, flags); | ||
| 721 | for (i = 0; i < AB3550_NUM_EVENT_REG; i++) | ||
| 722 | events[i] &= ~ab->event_mask[i]; | ||
| 723 | spin_unlock_irqrestore(&ab->event_lock, flags); | ||
| 724 | |||
| 725 | for (i = 0; i < AB3550_NUM_EVENT_REG; i++) { | ||
| 726 | u8 bit; | ||
| 727 | u8 event_reg; | ||
| 728 | |||
| 729 | dev_dbg(&ab->i2c_client[0]->dev, "IRQ Event[%d]: 0x%2x\n", | ||
| 730 | i, events[i]); | ||
| 731 | |||
| 732 | event_reg = events[i]; | ||
| 733 | for (bit = 0; event_reg; bit++, event_reg /= 2) { | ||
| 734 | if (event_reg % 2) { | ||
| 735 | unsigned int irq; | ||
| 736 | struct ab3550_platform_data *plf_data; | ||
| 737 | |||
| 738 | plf_data = ab->i2c_client[0]->dev.platform_data; | ||
| 739 | irq = plf_data->irq.base + (i * 8) + bit; | ||
| 740 | handle_nested_irq(irq); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | } | ||
| 744 | out: | ||
| 745 | return IRQ_HANDLED; | ||
| 746 | |||
| 747 | err_event_rd: | ||
| 748 | dev_dbg(&ab->i2c_client[0]->dev, "error reading event registers\n"); | ||
| 749 | return IRQ_HANDLED; | ||
| 750 | } | ||
| 751 | |||
| 752 | #ifdef CONFIG_DEBUG_FS | ||
| 753 | static struct ab3550_reg_ranges debug_ranges[AB3550_NUM_BANKS] = { | ||
| 754 | { | ||
| 755 | .count = 6, | ||
| 756 | .range = (struct ab3550_reg_range[]) { | ||
| 757 | { | ||
| 758 | .first = 0x00, | ||
| 759 | .last = 0x0e, | ||
| 760 | }, | ||
| 761 | { | ||
| 762 | .first = 0x10, | ||
| 763 | .last = 0x1a, | ||
| 764 | }, | ||
| 765 | { | ||
| 766 | .first = 0x1e, | ||
| 767 | .last = 0x4f, | ||
| 768 | }, | ||
| 769 | { | ||
| 770 | .first = 0x51, | ||
| 771 | .last = 0x63, | ||
| 772 | }, | ||
| 773 | { | ||
| 774 | .first = 0x65, | ||
| 775 | .last = 0xa3, | ||
| 776 | }, | ||
| 777 | { | ||
| 778 | .first = 0xa5, | ||
| 779 | .last = 0xa8, | ||
| 780 | }, | ||
| 781 | } | ||
| 782 | }, | ||
| 783 | { | ||
| 784 | .count = 8, | ||
| 785 | .range = (struct ab3550_reg_range[]) { | ||
| 786 | { | ||
| 787 | .first = 0x00, | ||
| 788 | .last = 0x0e, | ||
| 789 | }, | ||
| 790 | { | ||
| 791 | .first = 0x10, | ||
| 792 | .last = 0x17, | ||
| 793 | }, | ||
| 794 | { | ||
| 795 | .first = 0x1a, | ||
| 796 | .last = 0x1c, | ||
| 797 | }, | ||
| 798 | { | ||
| 799 | .first = 0x20, | ||
| 800 | .last = 0x56, | ||
| 801 | }, | ||
| 802 | { | ||
| 803 | .first = 0x5a, | ||
| 804 | .last = 0x88, | ||
| 805 | }, | ||
| 806 | { | ||
| 807 | .first = 0x8a, | ||
| 808 | .last = 0xad, | ||
| 809 | }, | ||
| 810 | { | ||
| 811 | .first = 0xb0, | ||
| 812 | .last = 0xba, | ||
| 813 | }, | ||
| 814 | { | ||
| 815 | .first = 0xbc, | ||
| 816 | .last = 0xc3, | ||
| 817 | }, | ||
| 818 | } | ||
| 819 | }, | ||
| 820 | }; | ||
| 821 | |||
| 822 | static int ab3550_registers_print(struct seq_file *s, void *p) | ||
| 823 | { | ||
| 824 | struct ab3550 *ab = s->private; | ||
| 825 | int bank; | ||
| 826 | |||
| 827 | seq_printf(s, AB3550_NAME_STRING " register values:\n"); | ||
| 828 | |||
| 829 | for (bank = 0; bank < AB3550_NUM_BANKS; bank++) { | ||
| 830 | unsigned int i; | ||
| 831 | |||
| 832 | seq_printf(s, " bank %d:\n", bank); | ||
| 833 | for (i = 0; i < debug_ranges[bank].count; i++) { | ||
| 834 | u8 reg; | ||
| 835 | |||
| 836 | for (reg = debug_ranges[bank].range[i].first; | ||
| 837 | reg <= debug_ranges[bank].range[i].last; | ||
| 838 | reg++) { | ||
| 839 | u8 value; | ||
| 840 | |||
| 841 | get_register_interruptible(ab, bank, reg, | ||
| 842 | &value); | ||
| 843 | seq_printf(s, " [%d/0x%02X]: 0x%02X\n", bank, | ||
| 844 | reg, value); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | } | ||
| 848 | return 0; | ||
| 849 | } | ||
| 850 | |||
| 851 | static int ab3550_registers_open(struct inode *inode, struct file *file) | ||
| 852 | { | ||
| 853 | return single_open(file, ab3550_registers_print, inode->i_private); | ||
| 854 | } | ||
| 855 | |||
| 856 | static const struct file_operations ab3550_registers_fops = { | ||
| 857 | .open = ab3550_registers_open, | ||
| 858 | .read = seq_read, | ||
| 859 | .llseek = seq_lseek, | ||
| 860 | .release = single_release, | ||
| 861 | .owner = THIS_MODULE, | ||
| 862 | }; | ||
| 863 | |||
| 864 | static int ab3550_bank_print(struct seq_file *s, void *p) | ||
| 865 | { | ||
| 866 | struct ab3550 *ab = s->private; | ||
| 867 | |||
| 868 | seq_printf(s, "%d\n", ab->debug_bank); | ||
| 869 | return 0; | ||
| 870 | } | ||
| 871 | |||
| 872 | static int ab3550_bank_open(struct inode *inode, struct file *file) | ||
| 873 | { | ||
| 874 | return single_open(file, ab3550_bank_print, inode->i_private); | ||
| 875 | } | ||
| 876 | |||
| 877 | static ssize_t ab3550_bank_write(struct file *file, | ||
| 878 | const char __user *user_buf, | ||
| 879 | size_t count, loff_t *ppos) | ||
| 880 | { | ||
| 881 | struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private; | ||
| 882 | unsigned long user_bank; | ||
| 883 | int err; | ||
| 884 | |||
| 885 | /* Get userspace string and assure termination */ | ||
| 886 | err = kstrtoul_from_user(user_buf, count, 0, &user_bank); | ||
| 887 | if (err) | ||
| 888 | return err; | ||
| 889 | |||
| 890 | if (user_bank >= AB3550_NUM_BANKS) { | ||
| 891 | dev_err(&ab->i2c_client[0]->dev, | ||
| 892 | "debugfs error input > number of banks\n"); | ||
| 893 | return -EINVAL; | ||
| 894 | } | ||
| 895 | |||
| 896 | ab->debug_bank = user_bank; | ||
| 897 | |||
| 898 | return count; | ||
| 899 | } | ||
| 900 | |||
| 901 | static int ab3550_address_print(struct seq_file *s, void *p) | ||
| 902 | { | ||
| 903 | struct ab3550 *ab = s->private; | ||
| 904 | |||
| 905 | seq_printf(s, "0x%02X\n", ab->debug_address); | ||
| 906 | return 0; | ||
| 907 | } | ||
| 908 | |||
| 909 | static int ab3550_address_open(struct inode *inode, struct file *file) | ||
| 910 | { | ||
| 911 | return single_open(file, ab3550_address_print, inode->i_private); | ||
| 912 | } | ||
| 913 | |||
| 914 | static ssize_t ab3550_address_write(struct file *file, | ||
| 915 | const char __user *user_buf, | ||
| 916 | size_t count, loff_t *ppos) | ||
| 917 | { | ||
| 918 | struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private; | ||
| 919 | unsigned long user_address; | ||
| 920 | int err; | ||
| 921 | |||
| 922 | /* Get userspace string and assure termination */ | ||
| 923 | err = kstrtoul_from_user(user_buf, count, 0, &user_address); | ||
| 924 | if (err) | ||
| 925 | return err; | ||
| 926 | |||
| 927 | if (user_address > 0xff) { | ||
| 928 | dev_err(&ab->i2c_client[0]->dev, | ||
| 929 | "debugfs error input > 0xff\n"); | ||
| 930 | return -EINVAL; | ||
| 931 | } | ||
| 932 | ab->debug_address = user_address; | ||
| 933 | return count; | ||
| 934 | } | ||
| 935 | |||
| 936 | static int ab3550_val_print(struct seq_file *s, void *p) | ||
| 937 | { | ||
| 938 | struct ab3550 *ab = s->private; | ||
| 939 | int err; | ||
| 940 | u8 regvalue; | ||
| 941 | |||
| 942 | err = get_register_interruptible(ab, (u8)ab->debug_bank, | ||
| 943 | (u8)ab->debug_address, ®value); | ||
| 944 | if (err) | ||
| 945 | return -EINVAL; | ||
| 946 | seq_printf(s, "0x%02X\n", regvalue); | ||
| 947 | |||
| 948 | return 0; | ||
| 949 | } | ||
| 950 | |||
| 951 | static int ab3550_val_open(struct inode *inode, struct file *file) | ||
| 952 | { | ||
| 953 | return single_open(file, ab3550_val_print, inode->i_private); | ||
| 954 | } | ||
| 955 | |||
| 956 | static ssize_t ab3550_val_write(struct file *file, | ||
| 957 | const char __user *user_buf, | ||
| 958 | size_t count, loff_t *ppos) | ||
| 959 | { | ||
| 960 | struct ab3550 *ab = ((struct seq_file *)(file->private_data))->private; | ||
| 961 | unsigned long user_val; | ||
| 962 | int err; | ||
| 963 | u8 regvalue; | ||
| 964 | |||
| 965 | /* Get userspace string and assure termination */ | ||
| 966 | err = kstrtoul_from_user(user_buf, count, 0, &user_val); | ||
| 967 | if (err) | ||
| 968 | return err; | ||
| 969 | |||
| 970 | if (user_val > 0xff) { | ||
| 971 | dev_err(&ab->i2c_client[0]->dev, | ||
| 972 | "debugfs error input > 0xff\n"); | ||
| 973 | return -EINVAL; | ||
| 974 | } | ||
| 975 | err = mask_and_set_register_interruptible( | ||
| 976 | ab, (u8)ab->debug_bank, | ||
| 977 | (u8)ab->debug_address, 0xFF, (u8)user_val); | ||
| 978 | if (err) | ||
| 979 | return -EINVAL; | ||
| 980 | |||
| 981 | get_register_interruptible(ab, (u8)ab->debug_bank, | ||
| 982 | (u8)ab->debug_address, ®value); | ||
| 983 | if (err) | ||
| 984 | return -EINVAL; | ||
| 985 | |||
| 986 | return count; | ||
| 987 | } | ||
| 988 | |||
| 989 | static const struct file_operations ab3550_bank_fops = { | ||
| 990 | .open = ab3550_bank_open, | ||
| 991 | .write = ab3550_bank_write, | ||
| 992 | .read = seq_read, | ||
| 993 | .llseek = seq_lseek, | ||
| 994 | .release = single_release, | ||
| 995 | .owner = THIS_MODULE, | ||
| 996 | }; | ||
| 997 | |||
| 998 | static const struct file_operations ab3550_address_fops = { | ||
| 999 | .open = ab3550_address_open, | ||
| 1000 | .write = ab3550_address_write, | ||
| 1001 | .read = seq_read, | ||
| 1002 | .llseek = seq_lseek, | ||
| 1003 | .release = single_release, | ||
| 1004 | .owner = THIS_MODULE, | ||
| 1005 | }; | ||
| 1006 | |||
| 1007 | static const struct file_operations ab3550_val_fops = { | ||
| 1008 | .open = ab3550_val_open, | ||
| 1009 | .write = ab3550_val_write, | ||
| 1010 | .read = seq_read, | ||
| 1011 | .llseek = seq_lseek, | ||
| 1012 | .release = single_release, | ||
| 1013 | .owner = THIS_MODULE, | ||
| 1014 | }; | ||
| 1015 | |||
| 1016 | static struct dentry *ab3550_dir; | ||
| 1017 | static struct dentry *ab3550_reg_file; | ||
| 1018 | static struct dentry *ab3550_bank_file; | ||
| 1019 | static struct dentry *ab3550_address_file; | ||
| 1020 | static struct dentry *ab3550_val_file; | ||
| 1021 | |||
| 1022 | static inline void ab3550_setup_debugfs(struct ab3550 *ab) | ||
| 1023 | { | ||
| 1024 | ab->debug_bank = 0; | ||
| 1025 | ab->debug_address = 0x00; | ||
| 1026 | |||
| 1027 | ab3550_dir = debugfs_create_dir(AB3550_NAME_STRING, NULL); | ||
| 1028 | if (!ab3550_dir) | ||
| 1029 | goto exit_no_debugfs; | ||
| 1030 | |||
| 1031 | ab3550_reg_file = debugfs_create_file("all-registers", | ||
| 1032 | S_IRUGO, ab3550_dir, ab, &ab3550_registers_fops); | ||
| 1033 | if (!ab3550_reg_file) | ||
| 1034 | goto exit_destroy_dir; | ||
| 1035 | |||
| 1036 | ab3550_bank_file = debugfs_create_file("register-bank", | ||
| 1037 | (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_bank_fops); | ||
| 1038 | if (!ab3550_bank_file) | ||
| 1039 | goto exit_destroy_reg; | ||
| 1040 | |||
| 1041 | ab3550_address_file = debugfs_create_file("register-address", | ||
| 1042 | (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_address_fops); | ||
| 1043 | if (!ab3550_address_file) | ||
| 1044 | goto exit_destroy_bank; | ||
| 1045 | |||
| 1046 | ab3550_val_file = debugfs_create_file("register-value", | ||
| 1047 | (S_IRUGO | S_IWUSR), ab3550_dir, ab, &ab3550_val_fops); | ||
| 1048 | if (!ab3550_val_file) | ||
| 1049 | goto exit_destroy_address; | ||
| 1050 | |||
| 1051 | return; | ||
| 1052 | |||
| 1053 | exit_destroy_address: | ||
| 1054 | debugfs_remove(ab3550_address_file); | ||
| 1055 | exit_destroy_bank: | ||
| 1056 | debugfs_remove(ab3550_bank_file); | ||
| 1057 | exit_destroy_reg: | ||
| 1058 | debugfs_remove(ab3550_reg_file); | ||
| 1059 | exit_destroy_dir: | ||
| 1060 | debugfs_remove(ab3550_dir); | ||
| 1061 | exit_no_debugfs: | ||
| 1062 | dev_err(&ab->i2c_client[0]->dev, "failed to create debugfs entries.\n"); | ||
| 1063 | return; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | static inline void ab3550_remove_debugfs(void) | ||
| 1067 | { | ||
| 1068 | debugfs_remove(ab3550_val_file); | ||
| 1069 | debugfs_remove(ab3550_address_file); | ||
| 1070 | debugfs_remove(ab3550_bank_file); | ||
| 1071 | debugfs_remove(ab3550_reg_file); | ||
| 1072 | debugfs_remove(ab3550_dir); | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | #else /* !CONFIG_DEBUG_FS */ | ||
| 1076 | static inline void ab3550_setup_debugfs(struct ab3550 *ab) | ||
| 1077 | { | ||
| 1078 | } | ||
| 1079 | static inline void ab3550_remove_debugfs(void) | ||
| 1080 | { | ||
| 1081 | } | ||
| 1082 | #endif | ||
| 1083 | |||
| 1084 | /* | ||
| 1085 | * Basic set-up, datastructure creation/destruction and I2C interface. | ||
| 1086 | * This sets up a default config in the AB3550 chip so that it | ||
| 1087 | * will work as expected. | ||
| 1088 | */ | ||
| 1089 | static int __init ab3550_setup(struct ab3550 *ab) | ||
| 1090 | { | ||
| 1091 | int err = 0; | ||
| 1092 | int i; | ||
| 1093 | struct ab3550_platform_data *plf_data; | ||
| 1094 | struct abx500_init_settings *settings; | ||
| 1095 | |||
| 1096 | plf_data = ab->i2c_client[0]->dev.platform_data; | ||
| 1097 | settings = plf_data->init_settings; | ||
| 1098 | |||
| 1099 | for (i = 0; i < plf_data->init_settings_sz; i++) { | ||
| 1100 | err = mask_and_set_register_interruptible(ab, | ||
| 1101 | settings[i].bank, | ||
| 1102 | settings[i].reg, | ||
| 1103 | 0xFF, settings[i].setting); | ||
| 1104 | if (err) | ||
| 1105 | goto exit_no_setup; | ||
| 1106 | |||
| 1107 | /* If event mask register update the event mask in ab3550 */ | ||
| 1108 | if ((settings[i].bank == 0) && | ||
| 1109 | (AB3550_IMR1 <= settings[i].reg) && | ||
| 1110 | (settings[i].reg <= AB3550_IMR5)) { | ||
| 1111 | ab->event_mask[settings[i].reg - AB3550_IMR1] = | ||
| 1112 | settings[i].setting; | ||
| 1113 | } | ||
| 1114 | } | ||
| 1115 | exit_no_setup: | ||
| 1116 | return err; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | static void ab3550_mask_work(struct work_struct *work) | ||
| 1120 | { | ||
| 1121 | struct ab3550 *ab = container_of(work, struct ab3550, mask_work); | ||
| 1122 | int i; | ||
| 1123 | unsigned long flags; | ||
| 1124 | u8 mask[AB3550_NUM_EVENT_REG]; | ||
| 1125 | |||
| 1126 | spin_lock_irqsave(&ab->event_lock, flags); | ||
| 1127 | for (i = 0; i < AB3550_NUM_EVENT_REG; i++) | ||
| 1128 | mask[i] = ab->event_mask[i]; | ||
| 1129 | spin_unlock_irqrestore(&ab->event_lock, flags); | ||
| 1130 | |||
| 1131 | for (i = 0; i < AB3550_NUM_EVENT_REG; i++) { | ||
| 1132 | int err; | ||
| 1133 | |||
| 1134 | err = mask_and_set_register_interruptible(ab, 0, | ||
| 1135 | (AB3550_IMR1 + i), ~0, mask[i]); | ||
| 1136 | if (err) | ||
| 1137 | dev_err(&ab->i2c_client[0]->dev, | ||
| 1138 | "ab3550_mask_work failed 0x%x,0x%x\n", | ||
| 1139 | (AB3550_IMR1 + i), mask[i]); | ||
| 1140 | } | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | static void ab3550_mask(struct irq_data *data) | ||
| 1144 | { | ||
| 1145 | unsigned long flags; | ||
| 1146 | struct ab3550 *ab; | ||
| 1147 | struct ab3550_platform_data *plf_data; | ||
| 1148 | int irq; | ||
| 1149 | |||
| 1150 | ab = irq_data_get_irq_chip_data(data); | ||
| 1151 | plf_data = ab->i2c_client[0]->dev.platform_data; | ||
| 1152 | irq = data->irq - plf_data->irq.base; | ||
| 1153 | |||
| 1154 | spin_lock_irqsave(&ab->event_lock, flags); | ||
| 1155 | ab->event_mask[irq / 8] |= BIT(irq % 8); | ||
| 1156 | spin_unlock_irqrestore(&ab->event_lock, flags); | ||
| 1157 | |||
| 1158 | schedule_work(&ab->mask_work); | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | static void ab3550_unmask(struct irq_data *data) | ||
| 1162 | { | ||
| 1163 | unsigned long flags; | ||
| 1164 | struct ab3550 *ab; | ||
| 1165 | struct ab3550_platform_data *plf_data; | ||
| 1166 | int irq; | ||
| 1167 | |||
| 1168 | ab = irq_data_get_irq_chip_data(data); | ||
| 1169 | plf_data = ab->i2c_client[0]->dev.platform_data; | ||
| 1170 | irq = data->irq - plf_data->irq.base; | ||
| 1171 | |||
| 1172 | spin_lock_irqsave(&ab->event_lock, flags); | ||
| 1173 | ab->event_mask[irq / 8] &= ~BIT(irq % 8); | ||
| 1174 | spin_unlock_irqrestore(&ab->event_lock, flags); | ||
| 1175 | |||
| 1176 | schedule_work(&ab->mask_work); | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | static void noop(struct irq_data *data) | ||
| 1180 | { | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | static struct irq_chip ab3550_irq_chip = { | ||
| 1184 | .name = "ab3550-core", /* Keep the same name as the request */ | ||
| 1185 | .irq_disable = ab3550_mask, /* No default to mask in chip.c */ | ||
| 1186 | .irq_ack = noop, | ||
| 1187 | .irq_mask = ab3550_mask, | ||
| 1188 | .irq_unmask = ab3550_unmask, | ||
| 1189 | }; | ||
| 1190 | |||
| 1191 | struct ab_family_id { | ||
| 1192 | u8 id; | ||
| 1193 | char *name; | ||
| 1194 | }; | ||
| 1195 | |||
| 1196 | static const struct ab_family_id ids[] __initdata = { | ||
| 1197 | /* AB3550 */ | ||
| 1198 | { | ||
| 1199 | .id = AB3550_P1A, | ||
| 1200 | .name = "P1A" | ||
| 1201 | }, | ||
| 1202 | /* Terminator */ | ||
| 1203 | { | ||
| 1204 | .id = 0x00, | ||
| 1205 | } | ||
| 1206 | }; | ||
| 1207 | |||
| 1208 | static int __init ab3550_probe(struct i2c_client *client, | ||
| 1209 | const struct i2c_device_id *id) | ||
| 1210 | { | ||
| 1211 | struct ab3550 *ab; | ||
| 1212 | struct ab3550_platform_data *ab3550_plf_data = | ||
| 1213 | client->dev.platform_data; | ||
| 1214 | int err; | ||
| 1215 | int i; | ||
| 1216 | int num_i2c_clients = 0; | ||
| 1217 | |||
| 1218 | ab = kzalloc(sizeof(struct ab3550), GFP_KERNEL); | ||
| 1219 | if (!ab) { | ||
| 1220 | dev_err(&client->dev, | ||
| 1221 | "could not allocate " AB3550_NAME_STRING " device\n"); | ||
| 1222 | return -ENOMEM; | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | /* Initialize data structure */ | ||
| 1226 | mutex_init(&ab->access_mutex); | ||
| 1227 | spin_lock_init(&ab->event_lock); | ||
| 1228 | ab->i2c_client[0] = client; | ||
| 1229 | |||
| 1230 | i2c_set_clientdata(client, ab); | ||
| 1231 | |||
| 1232 | /* Read chip ID register */ | ||
| 1233 | err = get_register_interruptible(ab, 0, AB3550_CID_REG, &ab->chip_id); | ||
| 1234 | if (err) { | ||
| 1235 | dev_err(&client->dev, "could not communicate with the analog " | ||
| 1236 | "baseband chip\n"); | ||
| 1237 | goto exit_no_detect; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | for (i = 0; ids[i].id != 0x0; i++) { | ||
| 1241 | if (ids[i].id == ab->chip_id) { | ||
| 1242 | snprintf(&ab->chip_name[0], sizeof(ab->chip_name) - 1, | ||
| 1243 | AB3550_ID_FORMAT_STRING, ids[i].name); | ||
| 1244 | break; | ||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | if (ids[i].id == 0x0) { | ||
| 1249 | dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n", | ||
| 1250 | ab->chip_id); | ||
| 1251 | dev_err(&client->dev, "driver not started!\n"); | ||
| 1252 | goto exit_no_detect; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | dev_info(&client->dev, "detected AB chip: %s\n", &ab->chip_name[0]); | ||
| 1256 | |||
| 1257 | /* Attach other dummy I2C clients. */ | ||
| 1258 | while (++num_i2c_clients < AB3550_NUM_BANKS) { | ||
| 1259 | ab->i2c_client[num_i2c_clients] = | ||
| 1260 | i2c_new_dummy(client->adapter, | ||
| 1261 | (client->addr + num_i2c_clients)); | ||
| 1262 | if (!ab->i2c_client[num_i2c_clients]) { | ||
| 1263 | err = -ENOMEM; | ||
| 1264 | goto exit_no_dummy_client; | ||
| 1265 | } | ||
| 1266 | strlcpy(ab->i2c_client[num_i2c_clients]->name, id->name, | ||
| 1267 | sizeof(ab->i2c_client[num_i2c_clients]->name)); | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | err = ab3550_setup(ab); | ||
| 1271 | if (err) | ||
| 1272 | goto exit_no_setup; | ||
| 1273 | |||
| 1274 | INIT_WORK(&ab->mask_work, ab3550_mask_work); | ||
| 1275 | |||
| 1276 | for (i = 0; i < ab3550_plf_data->irq.count; i++) { | ||
| 1277 | unsigned int irq; | ||
| 1278 | |||
| 1279 | irq = ab3550_plf_data->irq.base + i; | ||
| 1280 | irq_set_chip_data(irq, ab); | ||
| 1281 | irq_set_chip_and_handler(irq, &ab3550_irq_chip, | ||
| 1282 | handle_simple_irq); | ||
| 1283 | irq_set_nested_thread(irq, 1); | ||
| 1284 | #ifdef CONFIG_ARM | ||
| 1285 | set_irq_flags(irq, IRQF_VALID); | ||
| 1286 | #else | ||
| 1287 | irq_set_noprobe(irq); | ||
| 1288 | #endif | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | err = request_threaded_irq(client->irq, NULL, ab3550_irq_handler, | ||
| 1292 | IRQF_ONESHOT, "ab3550-core", ab); | ||
| 1293 | /* This real unpredictable IRQ is of course sampled for entropy */ | ||
| 1294 | rand_initialize_irq(client->irq); | ||
| 1295 | |||
| 1296 | if (err) | ||
| 1297 | goto exit_no_irq; | ||
| 1298 | |||
| 1299 | err = abx500_register_ops(&client->dev, &ab3550_ops); | ||
| 1300 | if (err) | ||
| 1301 | goto exit_no_ops; | ||
| 1302 | |||
| 1303 | /* Set up and register the platform devices. */ | ||
| 1304 | for (i = 0; i < AB3550_NUM_DEVICES; i++) { | ||
| 1305 | ab3550_devs[i].platform_data = ab3550_plf_data->dev_data[i]; | ||
| 1306 | ab3550_devs[i].pdata_size = ab3550_plf_data->dev_data_sz[i]; | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | err = mfd_add_devices(&client->dev, 0, ab3550_devs, | ||
| 1310 | ARRAY_SIZE(ab3550_devs), NULL, | ||
| 1311 | ab3550_plf_data->irq.base); | ||
| 1312 | |||
| 1313 | ab3550_setup_debugfs(ab); | ||
| 1314 | |||
| 1315 | return 0; | ||
| 1316 | |||
| 1317 | exit_no_ops: | ||
| 1318 | exit_no_irq: | ||
| 1319 | exit_no_setup: | ||
| 1320 | exit_no_dummy_client: | ||
| 1321 | /* Unregister the dummy i2c clients. */ | ||
| 1322 | while (--num_i2c_clients) | ||
| 1323 | i2c_unregister_device(ab->i2c_client[num_i2c_clients]); | ||
| 1324 | exit_no_detect: | ||
| 1325 | kfree(ab); | ||
| 1326 | return err; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | static int __exit ab3550_remove(struct i2c_client *client) | ||
| 1330 | { | ||
| 1331 | struct ab3550 *ab = i2c_get_clientdata(client); | ||
| 1332 | int num_i2c_clients = AB3550_NUM_BANKS; | ||
| 1333 | |||
| 1334 | mfd_remove_devices(&client->dev); | ||
| 1335 | ab3550_remove_debugfs(); | ||
| 1336 | |||
| 1337 | while (--num_i2c_clients) | ||
| 1338 | i2c_unregister_device(ab->i2c_client[num_i2c_clients]); | ||
| 1339 | |||
| 1340 | /* | ||
| 1341 | * At this point, all subscribers should have unregistered | ||
| 1342 | * their notifiers so deactivate IRQ | ||
| 1343 | */ | ||
| 1344 | free_irq(client->irq, ab); | ||
| 1345 | kfree(ab); | ||
| 1346 | return 0; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | static const struct i2c_device_id ab3550_id[] = { | ||
| 1350 | {AB3550_NAME_STRING, 0}, | ||
| 1351 | {} | ||
| 1352 | }; | ||
| 1353 | MODULE_DEVICE_TABLE(i2c, ab3550_id); | ||
| 1354 | |||
| 1355 | static struct i2c_driver ab3550_driver = { | ||
| 1356 | .driver = { | ||
| 1357 | .name = AB3550_NAME_STRING, | ||
| 1358 | .owner = THIS_MODULE, | ||
| 1359 | }, | ||
| 1360 | .id_table = ab3550_id, | ||
| 1361 | .probe = ab3550_probe, | ||
| 1362 | .remove = __exit_p(ab3550_remove), | ||
| 1363 | }; | ||
| 1364 | |||
| 1365 | static int __init ab3550_i2c_init(void) | ||
| 1366 | { | ||
| 1367 | return i2c_add_driver(&ab3550_driver); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | static void __exit ab3550_i2c_exit(void) | ||
| 1371 | { | ||
| 1372 | i2c_del_driver(&ab3550_driver); | ||
| 1373 | } | ||
| 1374 | |||
| 1375 | subsys_initcall(ab3550_i2c_init); | ||
| 1376 | module_exit(ab3550_i2c_exit); | ||
| 1377 | |||
| 1378 | MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>"); | ||
| 1379 | MODULE_DESCRIPTION("AB3550 core driver"); | ||
| 1380 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mfd/ab8500-i2c.c b/drivers/mfd/ab8500-i2c.c new file mode 100644 index 00000000000..9be541c6b00 --- /dev/null +++ b/drivers/mfd/ab8500-i2c.c | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) ST-Ericsson SA 2010 | ||
| 3 | * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson. | ||
| 4 | * License Terms: GNU General Public License v2 | ||
| 5 | * This file was based on drivers/mfd/ab8500-spi.c | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <linux/kernel.h> | ||
| 9 | #include <linux/slab.h> | ||
| 10 | #include <linux/init.h> | ||
| 11 | #include <linux/module.h> | ||
| 12 | #include <linux/platform_device.h> | ||
| 13 | #include <linux/mfd/ab8500.h> | ||
| 14 | #include <linux/mfd/db8500-prcmu.h> | ||
| 15 | |||
| 16 | static int ab8500_i2c_write(struct ab8500 *ab8500, u16 addr, u8 data) | ||
| 17 | { | ||
| 18 | int ret; | ||
| 19 | |||
| 20 | ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1); | ||
| 21 | if (ret < 0) | ||
| 22 | dev_err(ab8500->dev, "prcmu i2c error %d\n", ret); | ||
| 23 | return ret; | ||
| 24 | } | ||
| 25 | |||
| 26 | static int ab8500_i2c_read(struct ab8500 *ab8500, u16 addr) | ||
| 27 | { | ||
| 28 | int ret; | ||
| 29 | u8 data; | ||
| 30 | |||
| 31 | ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1); | ||
| 32 | if (ret < 0) { | ||
| 33 | dev_err(ab8500->dev, "prcmu i2c error %d\n", ret); | ||
| 34 | return ret; | ||
| 35 | } | ||
| 36 | return (int)data; | ||
| 37 | } | ||
| 38 | |||
| 39 | static int __devinit ab8500_i2c_probe(struct platform_device *plf) | ||
| 40 | { | ||
| 41 | struct ab8500 *ab8500; | ||
| 42 | struct resource *resource; | ||
| 43 | int ret; | ||
| 44 | |||
| 45 | ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL); | ||
| 46 | if (!ab8500) | ||
| 47 | return -ENOMEM; | ||
| 48 | |||
| 49 | ab8500->dev = &plf->dev; | ||
| 50 | |||
| 51 | resource = platform_get_resource(plf, IORESOURCE_IRQ, 0); | ||
| 52 | if (!resource) { | ||
| 53 | kfree(ab8500); | ||
| 54 | return -ENODEV; | ||
| 55 | } | ||
| 56 | |||
| 57 | ab8500->irq = resource->start; | ||
| 58 | |||
| 59 | ab8500->read = ab8500_i2c_read; | ||
| 60 | ab8500->write = ab8500_i2c_write; | ||
| 61 | |||
| 62 | platform_set_drvdata(plf, ab8500); | ||
| 63 | |||
| 64 | ret = ab8500_init(ab8500); | ||
| 65 | if (ret) | ||
| 66 | kfree(ab8500); | ||
| 67 | |||
| 68 | return ret; | ||
| 69 | } | ||
| 70 | |||
| 71 | static int __devexit ab8500_i2c_remove(struct platform_device *plf) | ||
| 72 | { | ||
| 73 | struct ab8500 *ab8500 = platform_get_drvdata(plf); | ||
| 74 | |||
| 75 | ab8500_exit(ab8500); | ||
| 76 | kfree(ab8500); | ||
| 77 | |||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | static struct platform_driver ab8500_i2c_driver = { | ||
| 82 | .driver = { | ||
| 83 | .name = "ab8500-i2c", | ||
| 84 | .owner = THIS_MODULE, | ||
| 85 | }, | ||
| 86 | .probe = ab8500_i2c_probe, | ||
| 87 | .remove = __devexit_p(ab8500_i2c_remove) | ||
| 88 | }; | ||
| 89 | |||
| 90 | static int __init ab8500_i2c_init(void) | ||
| 91 | { | ||
| 92 | return platform_driver_register(&ab8500_i2c_driver); | ||
| 93 | } | ||
| 94 | |||
| 95 | static void __exit ab8500_i2c_exit(void) | ||
| 96 | { | ||
| 97 | platform_driver_unregister(&ab8500_i2c_driver); | ||
| 98 | } | ||
| 99 | arch_initcall(ab8500_i2c_init); | ||
| 100 | module_exit(ab8500_i2c_exit); | ||
| 101 | |||
| 102 | MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com"); | ||
| 103 | MODULE_DESCRIPTION("AB8500 Core access via PRCMU I2C"); | ||
| 104 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/mfd/db5500-prcmu-regs.h b/drivers/mfd/db5500-prcmu-regs.h new file mode 100644 index 00000000000..9a8e9e4ddd3 --- /dev/null +++ b/drivers/mfd/db5500-prcmu-regs.h | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) STMicroelectronics 2009 | ||
| 3 | * Copyright (C) ST-Ericsson SA 2010 | ||
| 4 | * | ||
| 5 | * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com> | ||
| 6 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> | ||
| 7 | * | ||
| 8 | * License Terms: GNU General Public License v2 | ||
| 9 | * | ||
| 10 | * PRCM Unit registers | ||
| 11 | */ | ||
| 12 | |||
| 13 | #ifndef __MACH_PRCMU_REGS_H | ||
| 14 | #define __MACH_PRCMU_REGS_H | ||
| 15 | |||
| 16 | #include <mach/hardware.h> | ||
| 17 | |||
| 18 | #define PRCM_ARM_PLLDIVPS (_PRCMU_BASE + 0x118) | ||
| 19 | #define PRCM_ARM_PLLDIVPS_ARM_BRM_RATE 0x3f | ||
| 20 | #define PRCM_ARM_PLLDIVPS_MAX_MASK 0xf | ||
| 21 | |||
| 22 | #define PRCM_PLLARM_LOCKP (_PRCMU_BASE + 0x0a8) | ||
| 23 | #define PRCM_PLLARM_LOCKP_PRCM_PLLARM_LOCKP3 0x2 | ||
| 24 | |||
| 25 | #define PRCM_ARM_CHGCLKREQ (_PRCMU_BASE + 0x114) | ||
| 26 | #define PRCM_ARM_CHGCLKREQ_PRCM_ARM_CHGCLKREQ 0x1 | ||
| 27 | |||
| 28 | #define PRCM_PLLARM_ENABLE (_PRCMU_BASE + 0x98) | ||
| 29 | #define PRCM_PLLARM_ENABLE_PRCM_PLLARM_ENABLE 0x1 | ||
| 30 | #define PRCM_PLLARM_ENABLE_PRCM_PLLARM_COUNTON 0x100 | ||
| 31 | |||
| 32 | #define PRCM_ARMCLKFIX_MGT (_PRCMU_BASE + 0x0) | ||
| 33 | #define PRCM_A9_RESETN_CLR (_PRCMU_BASE + 0x1f4) | ||
| 34 | #define PRCM_A9_RESETN_SET (_PRCMU_BASE + 0x1f0) | ||
| 35 | #define PRCM_ARM_LS_CLAMP (_PRCMU_BASE + 0x30c) | ||
| 36 | #define PRCM_SRAM_A9 (_PRCMU_BASE + 0x308) | ||
| 37 | |||
| 38 | /* ARM WFI Standby signal register */ | ||
| 39 | #define PRCM_ARM_WFI_STANDBY (_PRCMU_BASE + 0x130) | ||
| 40 | #define PRCM_IOCR (_PRCMU_BASE + 0x310) | ||
| 41 | #define PRCM_IOCR_IOFORCE 0x1 | ||
| 42 | |||
| 43 | /* CPU mailbox registers */ | ||
| 44 | #define PRCM_MBOX_CPU_VAL (_PRCMU_BASE + 0x0fc) | ||
| 45 | #define PRCM_MBOX_CPU_SET (_PRCMU_BASE + 0x100) | ||
| 46 | #define PRCM_MBOX_CPU_CLR (_PRCMU_BASE + 0x104) | ||
| 47 | |||
| 48 | /* Dual A9 core interrupt management unit registers */ | ||
| 49 | #define PRCM_A9_MASK_REQ (_PRCMU_BASE + 0x328) | ||
| 50 | #define PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ 0x1 | ||
| 51 | |||
| 52 | #define PRCM_A9_MASK_ACK (_PRCMU_BASE + 0x32c) | ||
| 53 | #define PRCM_ARMITMSK31TO0 (_PRCMU_BASE + 0x11c) | ||
| 54 | #define PRCM_ARMITMSK63TO32 (_PRCMU_BASE + 0x120) | ||
| 55 | #define PRCM_ARMITMSK95TO64 (_PRCMU_BASE + 0x124) | ||
| 56 | #define PRCM_ARMITMSK127TO96 (_PRCMU_BASE + 0x128) | ||
| 57 | #define PRCM_POWER_STATE_VAL (_PRCMU_BASE + 0x25C) | ||
| 58 | #define PRCM_ARMITVAL31TO0 (_PRCMU_BASE + 0x260) | ||
| 59 | #define PRCM_ARMITVAL63TO32 (_PRCMU_BASE + 0x264) | ||
| 60 | #define PRCM_ARMITVAL95TO64 (_PRCMU_BASE + 0x268) | ||
| 61 | #define PRCM_ARMITVAL127TO96 (_PRCMU_BASE + 0x26C) | ||
| 62 | |||
| 63 | #define PRCM_HOSTACCESS_REQ (_PRCMU_BASE + 0x334) | ||
| 64 | #define ARM_WAKEUP_MODEM 0x1 | ||
| 65 | |||
| 66 | #define PRCM_ARM_IT1_CLEAR (_PRCMU_BASE + 0x48C) | ||
| 67 | #define PRCM_ARM_IT1_VAL (_PRCMU_BASE + 0x494) | ||
| 68 | #define PRCM_HOLD_EVT (_PRCMU_BASE + 0x174) | ||
| 69 | |||
| 70 | #define PRCM_ITSTATUS0 (_PRCMU_BASE + 0x148) | ||
| 71 | #define PRCM_ITSTATUS1 (_PRCMU_BASE + 0x150) | ||
| 72 | #define PRCM_ITSTATUS2 (_PRCMU_BASE + 0x158) | ||
| 73 | #define PRCM_ITSTATUS3 (_PRCMU_BASE + 0x160) | ||
| 74 | #define PRCM_ITSTATUS4 (_PRCMU_BASE + 0x168) | ||
| 75 | #define PRCM_ITSTATUS5 (_PRCMU_BASE + 0x484) | ||
| 76 | #define PRCM_ITCLEAR5 (_PRCMU_BASE + 0x488) | ||
| 77 | #define PRCM_ARMIT_MASKXP70_IT (_PRCMU_BASE + 0x1018) | ||
| 78 | |||
| 79 | /* System reset register */ | ||
| 80 | #define PRCM_APE_SOFTRST (_PRCMU_BASE + 0x228) | ||
| 81 | |||
| 82 | /* Level shifter and clamp control registers */ | ||
| 83 | #define PRCM_MMIP_LS_CLAMP_SET (_PRCMU_BASE + 0x420) | ||
| 84 | #define PRCM_MMIP_LS_CLAMP_CLR (_PRCMU_BASE + 0x424) | ||
| 85 | |||
| 86 | /* PRCMU clock/PLL/reset registers */ | ||
| 87 | #define PRCM_PLLDSI_FREQ (_PRCMU_BASE + 0x500) | ||
| 88 | #define PRCM_PLLDSI_ENABLE (_PRCMU_BASE + 0x504) | ||
| 89 | #define PRCM_PLLDSI_LOCKP (_PRCMU_BASE + 0x508) | ||
| 90 | #define PRCM_LCDCLK_MGT (_PRCMU_BASE + 0x044) | ||
| 91 | #define PRCM_MCDECLK_MGT (_PRCMU_BASE + 0x064) | ||
| 92 | #define PRCM_HDMICLK_MGT (_PRCMU_BASE + 0x058) | ||
| 93 | #define PRCM_TVCLK_MGT (_PRCMU_BASE + 0x07c) | ||
| 94 | #define PRCM_DSI_PLLOUT_SEL (_PRCMU_BASE + 0x530) | ||
| 95 | #define PRCM_DSITVCLK_DIV (_PRCMU_BASE + 0x52C) | ||
| 96 | #define PRCM_PLLDSI_LOCKP (_PRCMU_BASE + 0x508) | ||
| 97 | #define PRCM_APE_RESETN_SET (_PRCMU_BASE + 0x1E4) | ||
| 98 | #define PRCM_APE_RESETN_CLR (_PRCMU_BASE + 0x1E8) | ||
| 99 | #define PRCM_CLKOCR (_PRCMU_BASE + 0x1CC) | ||
| 100 | |||
| 101 | /* ePOD and memory power signal control registers */ | ||
| 102 | #define PRCM_EPOD_C_SET (_PRCMU_BASE + 0x410) | ||
| 103 | #define PRCM_SRAM_LS_SLEEP (_PRCMU_BASE + 0x304) | ||
| 104 | |||
| 105 | /* Debug power control unit registers */ | ||
| 106 | #define PRCM_POWER_STATE_SET (_PRCMU_BASE + 0x254) | ||
| 107 | |||
| 108 | /* Miscellaneous unit registers */ | ||
| 109 | #define PRCM_DSI_SW_RESET (_PRCMU_BASE + 0x324) | ||
| 110 | #define PRCM_GPIOCR (_PRCMU_BASE + 0x138) | ||
| 111 | #define PRCM_GPIOCR_DBG_STM_MOD_CMD1 0x800 | ||
| 112 | #define PRCM_GPIOCR_DBG_UARTMOD_CMD0 0x1 | ||
| 113 | |||
| 114 | |||
| 115 | #endif /* __MACH_PRCMU__REGS_H */ | ||
diff --git a/drivers/mfd/db5500-prcmu.c b/drivers/mfd/db5500-prcmu.c new file mode 100644 index 00000000000..9dbb3cab4a6 --- /dev/null +++ b/drivers/mfd/db5500-prcmu.c | |||
| @@ -0,0 +1,448 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) ST-Ericsson SA 2010 | ||
| 3 | * | ||
| 4 | * License Terms: GNU General Public License v2 | ||
| 5 | * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> | ||
| 6 | * | ||
| 7 | * U5500 PRCM Unit interface driver | ||
| 8 | */ | ||
| 9 | #include <linux/module.h> | ||
| 10 | #include <linux/kernel.h> | ||
| 11 | #include <linux/delay.h> | ||
| 12 | #include <linux/errno.h> | ||
| 13 | #include <linux/err.h> | ||
| 14 | #include <linux/spinlock.h> | ||
| 15 | #include <linux/io.h> | ||
| 16 | #include <linux/slab.h> | ||
| 17 | #include <linux/mutex.h> | ||
| 18 | #include <linux/completion.h> | ||
| 19 | #include <linux/irq.h> | ||
| 20 | #include <linux/jiffies.h> | ||
| 21 | #include <linux/bitops.h> | ||
| 22 | #include <linux/interrupt.h> | ||
| 23 | #include <linux/mfd/db5500-prcmu.h> | ||
| 24 | #include <mach/hardware.h> | ||
| 25 | #include <mach/irqs.h> | ||
| 26 | #include <mach/db5500-regs.h> | ||
| 27 | #include "db5500-prcmu-regs.h" | ||
| 28 | |||
| 29 | #define _PRCM_MB_HEADER (tcdm_base + 0xFE8) | ||
| 30 | #define PRCM_REQ_MB0_HEADER (_PRCM_MB_HEADER + 0x0) | ||
| 31 | #define PRCM_REQ_MB1_HEADER (_PRCM_MB_HEADER + 0x1) | ||
| 32 | #define PRCM_REQ_MB2_HEADER (_PRCM_MB_HEADER + 0x2) | ||
| 33 | #define PRCM_REQ_MB3_HEADER (_PRCM_MB_HEADER + 0x3) | ||
| 34 | #define PRCM_REQ_MB4_HEADER (_PRCM_MB_HEADER + 0x4) | ||
| 35 | #define PRCM_REQ_MB5_HEADER (_PRCM_MB_HEADER + 0x5) | ||
| 36 | #define PRCM_REQ_MB6_HEADER (_PRCM_MB_HEADER + 0x6) | ||
| 37 | #define PRCM_REQ_MB7_HEADER (_PRCM_MB_HEADER + 0x7) | ||
| 38 | #define PRCM_ACK_MB0_HEADER (_PRCM_MB_HEADER + 0x8) | ||
| 39 | #define PRCM_ACK_MB1_HEADER (_PRCM_MB_HEADER + 0x9) | ||
| 40 | #define PRCM_ACK_MB2_HEADER (_PRCM_MB_HEADER + 0xa) | ||
| 41 | #define PRCM_ACK_MB3_HEADER (_PRCM_MB_HEADER + 0xb) | ||
| 42 | #define PRCM_ACK_MB4_HEADER (_PRCM_MB_HEADER + 0xc) | ||
| 43 | #define PRCM_ACK_MB5_HEADER (_PRCM_MB_HEADER + 0xd) | ||
| 44 | #define PRCM_ACK_MB6_HEADER (_PRCM_MB_HEADER + 0xe) | ||
| 45 | #define PRCM_ACK_MB7_HEADER (_PRCM_MB_HEADER + 0xf) | ||
| 46 | |||
| 47 | /* Req Mailboxes */ | ||
| 48 | #define PRCM_REQ_MB0 (tcdm_base + 0xFD8) | ||
| 49 | #define PRCM_REQ_MB1 (tcdm_base + 0xFCC) | ||
| 50 | #define PRCM_REQ_MB2 (tcdm_base + 0xFC4) | ||
| 51 | #define PRCM_REQ_MB3 (tcdm_base + 0xFC0) | ||
| 52 | #define PRCM_REQ_MB4 (tcdm_base + 0xF98) | ||
| 53 | #define PRCM_REQ_MB5 (tcdm_base + 0xF90) | ||
| 54 | #define PRCM_REQ_MB6 (tcdm_base + 0xF8C) | ||
| 55 | #define PRCM_REQ_MB7 (tcdm_base + 0xF84) | ||
| 56 | |||
| 57 | /* Ack Mailboxes */ | ||
| 58 | #define PRCM_ACK_MB0 (tcdm_base + 0xF38) | ||
| 59 | #define PRCM_ACK_MB1 (tcdm_base + 0xF30) | ||
| 60 | #define PRCM_ACK_MB2 (tcdm_base + 0xF24) | ||
| 61 | #define PRCM_ACK_MB3 (tcdm_base + 0xF20) | ||
| 62 | #define PRCM_ACK_MB4 (tcdm_base + 0xF1C) | ||
| 63 | #define PRCM_ACK_MB5 (tcdm_base + 0xF14) | ||
| 64 | #define PRCM_ACK_MB6 (tcdm_base + 0xF0C) | ||
| 65 | #define PRCM_ACK_MB7 (tcdm_base + 0xF08) | ||
| 66 | |||
| 67 | enum mb_return_code { | ||
| 68 | RC_SUCCESS, | ||
| 69 | RC_FAIL, | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* Mailbox 0 headers. */ | ||
| 73 | enum mb0_header { | ||
| 74 | /* request */ | ||
| 75 | RMB0H_PWR_STATE_TRANS = 1, | ||
| 76 | RMB0H_WAKE_UP_CFG, | ||
| 77 | RMB0H_RD_WAKE_UP_ACK, | ||
| 78 | /* acknowledge */ | ||
| 79 | AMB0H_WAKE_UP = 1, | ||
| 80 | }; | ||
| 81 | |||
| 82 | /* Mailbox 5 headers. */ | ||
| 83 | enum mb5_header { | ||
| 84 | MB5H_I2C_WRITE = 1, | ||
| 85 | MB5H_I2C_READ, | ||
| 86 | }; | ||
| 87 | |||
| 88 | /* Request mailbox 5 fields. */ | ||
| 89 | #define PRCM_REQ_MB5_I2C_SLAVE (PRCM_REQ_MB5 + 0) | ||
| 90 | #define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 1) | ||
| 91 | #define PRCM_REQ_MB5_I2C_SIZE (PRCM_REQ_MB5 + 2) | ||
| 92 | #define PRCM_REQ_MB5_I2C_DATA (PRCM_REQ_MB5 + 4) | ||
| 93 | |||
| 94 | /* Acknowledge mailbox 5 fields. */ | ||
| 95 | #define PRCM_ACK_MB5_RETURN_CODE (PRCM_ACK_MB5 + 0) | ||
| 96 | #define PRCM_ACK_MB5_I2C_DATA (PRCM_ACK_MB5 + 4) | ||
| 97 | |||
| 98 | #define NUM_MB 8 | ||
| 99 | #define MBOX_BIT BIT | ||
| 100 | #define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1) | ||
| 101 | |||
| 102 | /* | ||
| 103 | * Used by MCDE to setup all necessary PRCMU registers | ||
| 104 | */ | ||
| 105 | #define PRCMU_RESET_DSIPLL 0x00004000 | ||
| 106 | #define PRCMU_UNCLAMP_DSIPLL 0x00400800 | ||
| 107 | |||
| 108 | /* HDMI CLK MGT PLLSW=001 (PLLSOC0), PLLDIV=0x8, = 50 Mhz*/ | ||
| 109 | #define PRCMU_DSI_CLOCK_SETTING 0x00000128 | ||
| 110 | /* TVCLK_MGT PLLSW=001 (PLLSOC0) PLLDIV=0x13, = 19.05 MHZ */ | ||
| 111 | #define PRCMU_DSI_LP_CLOCK_SETTING 0x00000135 | ||
| 112 | #define PRCMU_PLLDSI_FREQ_SETTING 0x0004013C | ||
| 113 | #define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000002 | ||
| 114 | #define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x03000101 | ||
| 115 | #define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00000101 | ||
| 116 | |||
| 117 | #define PRCMU_ENABLE_PLLDSI 0x00000001 | ||
| 118 | #define PRCMU_DISABLE_PLLDSI 0x00000000 | ||
| 119 | |||
| 120 | #define PRCMU_DSI_RESET_SW 0x00000003 | ||
| 121 | |||
| 122 | #define PRCMU_PLLDSI_LOCKP_LOCKED 0x3 | ||
| 123 | |||
| 124 | /* | ||
| 125 | * mb0_transfer - state needed for mailbox 0 communication. | ||
| 126 | * @lock: The transaction lock. | ||
| 127 | */ | ||
| 128 | static struct { | ||
| 129 | spinlock_t lock; | ||
| 130 | } mb0_transfer; | ||
| 131 | |||
| 132 | /* | ||
| 133 | * mb5_transfer - state needed for mailbox 5 communication. | ||
| 134 | * @lock: The transaction lock. | ||
| 135 | * @work: The transaction completion structure. | ||
| 136 | * @ack: Reply ("acknowledge") data. | ||
| 137 | */ | ||
| 138 | static struct { | ||
| 139 | struct mutex lock; | ||
| 140 | struct completion work; | ||
| 141 | struct { | ||
| 142 | u8 header; | ||
| 143 | u8 status; | ||
| 144 | u8 value[4]; | ||
| 145 | } ack; | ||
| 146 | } mb5_transfer; | ||
| 147 | |||
| 148 | /* PRCMU TCDM base IO address. */ | ||
| 149 | static __iomem void *tcdm_base; | ||
| 150 | |||
| 151 | /** | ||
| 152 | * db5500_prcmu_abb_read() - Read register value(s) from the ABB. | ||
| 153 | * @slave: The I2C slave address. | ||
| 154 | * @reg: The (start) register address. | ||
| 155 | * @value: The read out value(s). | ||
| 156 | * @size: The number of registers to read. | ||
| 157 | * | ||
| 158 | * Reads register value(s) from the ABB. | ||
| 159 | * @size has to be <= 4. | ||
| 160 | */ | ||
| 161 | int db5500_prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size) | ||
| 162 | { | ||
| 163 | int r; | ||
| 164 | |||
| 165 | if ((size < 1) || (4 < size)) | ||
| 166 | return -EINVAL; | ||
| 167 | |||
| 168 | mutex_lock(&mb5_transfer.lock); | ||
| 169 | |||
| 170 | while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5)) | ||
| 171 | cpu_relax(); | ||
| 172 | writeb(slave, PRCM_REQ_MB5_I2C_SLAVE); | ||
| 173 | writeb(reg, PRCM_REQ_MB5_I2C_REG); | ||
| 174 | writeb(size, PRCM_REQ_MB5_I2C_SIZE); | ||
| 175 | writeb(MB5H_I2C_READ, PRCM_REQ_MB5_HEADER); | ||
| 176 | |||
| 177 | writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET); | ||
| 178 | wait_for_completion(&mb5_transfer.work); | ||
| 179 | |||
| 180 | r = 0; | ||
| 181 | if ((mb5_transfer.ack.header == MB5H_I2C_READ) && | ||
| 182 | (mb5_transfer.ack.status == RC_SUCCESS)) | ||
| 183 | memcpy(value, mb5_transfer.ack.value, (size_t)size); | ||
| 184 | else | ||
| 185 | r = -EIO; | ||
| 186 | |||
| 187 | mutex_unlock(&mb5_transfer.lock); | ||
| 188 | |||
| 189 | return r; | ||
| 190 | } | ||
| 191 | |||
| 192 | /** | ||
| 193 | * db5500_prcmu_abb_write() - Write register value(s) to the ABB. | ||
| 194 | * @slave: The I2C slave address. | ||
| 195 | * @reg: The (start) register address. | ||
| 196 | * @value: The value(s) to write. | ||
| 197 | * @size: The number of registers to write. | ||
| 198 | * | ||
| 199 | * Writes register value(s) to the ABB. | ||
| 200 | * @size has to be <= 4. | ||
| 201 | */ | ||
| 202 | int db5500_prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size) | ||
| 203 | { | ||
| 204 | int r; | ||
| 205 | |||
| 206 | if ((size < 1) || (4 < size)) | ||
| 207 | return -EINVAL; | ||
| 208 | |||
| 209 | mutex_lock(&mb5_transfer.lock); | ||
| 210 | |||
| 211 | while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5)) | ||
| 212 | cpu_relax(); | ||
| 213 | writeb(slave, PRCM_REQ_MB5_I2C_SLAVE); | ||
| 214 | writeb(reg, PRCM_REQ_MB5_I2C_REG); | ||
| 215 | writeb(size, PRCM_REQ_MB5_I2C_SIZE); | ||
| 216 | memcpy_toio(PRCM_REQ_MB5_I2C_DATA, value, size); | ||
| 217 | writeb(MB5H_I2C_WRITE, PRCM_REQ_MB5_HEADER); | ||
| 218 | |||
| 219 | writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET); | ||
| 220 | wait_for_completion(&mb5_transfer.work); | ||
| 221 | |||
| 222 | if ((mb5_transfer.ack.header == MB5H_I2C_WRITE) && | ||
| 223 | (mb5_transfer.ack.status == RC_SUCCESS)) | ||
| 224 | r = 0; | ||
| 225 | else | ||
| 226 | r = -EIO; | ||
| 227 | |||
| 228 | mutex_unlock(&mb5_transfer.lock); | ||
| 229 | |||
| 230 | return r; | ||
| 231 | } | ||
| 232 | |||
| 233 | int db5500_prcmu_enable_dsipll(void) | ||
| 234 | { | ||
| 235 | int i; | ||
| 236 | |||
| 237 | /* Enable DSIPLL_RESETN resets */ | ||
| 238 | writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR); | ||
| 239 | /* Unclamp DSIPLL in/out */ | ||
| 240 | writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR); | ||
| 241 | /* Set DSI PLL FREQ */ | ||
| 242 | writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ); | ||
| 243 | writel(PRCMU_DSI_PLLOUT_SEL_SETTING, | ||
| 244 | PRCM_DSI_PLLOUT_SEL); | ||
| 245 | /* Enable Escape clocks */ | ||
| 246 | writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV); | ||
| 247 | |||
| 248 | /* Start DSI PLL */ | ||
| 249 | writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE); | ||
| 250 | /* Reset DSI PLL */ | ||
| 251 | writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET); | ||
| 252 | for (i = 0; i < 10; i++) { | ||
| 253 | if ((readl(PRCM_PLLDSI_LOCKP) & | ||
| 254 | PRCMU_PLLDSI_LOCKP_LOCKED) == PRCMU_PLLDSI_LOCKP_LOCKED) | ||
| 255 | break; | ||
| 256 | udelay(100); | ||
| 257 | } | ||
| 258 | /* Release DSIPLL_RESETN */ | ||
| 259 | writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET); | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | int db5500_prcmu_disable_dsipll(void) | ||
| 264 | { | ||
| 265 | /* Disable dsi pll */ | ||
| 266 | writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE); | ||
| 267 | /* Disable escapeclock */ | ||
| 268 | writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV); | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | int db5500_prcmu_set_display_clocks(void) | ||
| 273 | { | ||
| 274 | /* HDMI and TVCLK Should be handled somewhere else */ | ||
| 275 | /* PLLDIV=8, PLLSW=2, CLKEN=1 */ | ||
| 276 | writel(PRCMU_DSI_CLOCK_SETTING, PRCM_HDMICLK_MGT); | ||
| 277 | /* PLLDIV=14, PLLSW=2, CLKEN=1 */ | ||
| 278 | writel(PRCMU_DSI_LP_CLOCK_SETTING, PRCM_TVCLK_MGT); | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | |||
| 282 | static void ack_dbb_wakeup(void) | ||
| 283 | { | ||
| 284 | unsigned long flags; | ||
| 285 | |||
| 286 | spin_lock_irqsave(&mb0_transfer.lock, flags); | ||
| 287 | |||
| 288 | while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0)) | ||
| 289 | cpu_relax(); | ||
| 290 | |||
| 291 | writeb(RMB0H_RD_WAKE_UP_ACK, PRCM_REQ_MB0_HEADER); | ||
| 292 | writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET); | ||
| 293 | |||
| 294 | spin_unlock_irqrestore(&mb0_transfer.lock, flags); | ||
| 295 | } | ||
| 296 | |||
| 297 | static inline void print_unknown_header_warning(u8 n, u8 header) | ||
| 298 | { | ||
| 299 | pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n", | ||
| 300 | header, n); | ||
| 301 | } | ||
| 302 | |||
| 303 | static bool read_mailbox_0(void) | ||
| 304 | { | ||
| 305 | bool r; | ||
| 306 | u8 header; | ||
| 307 | |||
| 308 | header = readb(PRCM_ACK_MB0_HEADER); | ||
| 309 | switch (header) { | ||
| 310 | case AMB0H_WAKE_UP: | ||
| 311 | r = true; | ||
| 312 | break; | ||
| 313 | default: | ||
| 314 | print_unknown_header_warning(0, header); | ||
| 315 | r = false; | ||
| 316 | break; | ||
| 317 | } | ||
| 318 | writel(MBOX_BIT(0), PRCM_ARM_IT1_CLEAR); | ||
| 319 | return r; | ||
| 320 | } | ||
| 321 | |||
| 322 | static bool read_mailbox_1(void) | ||
| 323 | { | ||
| 324 | writel(MBOX_BIT(1), PRCM_ARM_IT1_CLEAR); | ||
| 325 | return false; | ||
| 326 | } | ||
| 327 | |||
| 328 | static bool read_mailbox_2(void) | ||
| 329 | { | ||
| 330 | writel(MBOX_BIT(2), PRCM_ARM_IT1_CLEAR); | ||
| 331 | return false; | ||
| 332 | } | ||
| 333 | |||
| 334 | static bool read_mailbox_3(void) | ||
| 335 | { | ||
| 336 | writel(MBOX_BIT(3), PRCM_ARM_IT1_CLEAR); | ||
| 337 | return false; | ||
| 338 | } | ||
| 339 | |||
| 340 | static bool read_mailbox_4(void) | ||
| 341 | { | ||
| 342 | writel(MBOX_BIT(4), PRCM_ARM_IT1_CLEAR); | ||
| 343 | return false; | ||
| 344 | } | ||
| 345 | |||
| 346 | static bool read_mailbox_5(void) | ||
| 347 | { | ||
| 348 | u8 header; | ||
| 349 | |||
| 350 | header = readb(PRCM_ACK_MB5_HEADER); | ||
| 351 | switch (header) { | ||
| 352 | case MB5H_I2C_READ: | ||
| 353 | memcpy_fromio(mb5_transfer.ack.value, PRCM_ACK_MB5_I2C_DATA, 4); | ||
| 354 | case MB5H_I2C_WRITE: | ||
| 355 | mb5_transfer.ack.header = header; | ||
| 356 | mb5_transfer.ack.status = readb(PRCM_ACK_MB5_RETURN_CODE); | ||
| 357 | complete(&mb5_transfer.work); | ||
| 358 | break; | ||
| 359 | default: | ||
| 360 | print_unknown_header_warning(5, header); | ||
| 361 | break; | ||
| 362 | } | ||
| 363 | writel(MBOX_BIT(5), PRCM_ARM_IT1_CLEAR); | ||
| 364 | return false; | ||
| 365 | } | ||
| 366 | |||
| 367 | static bool read_mailbox_6(void) | ||
| 368 | { | ||
| 369 | writel(MBOX_BIT(6), PRCM_ARM_IT1_CLEAR); | ||
| 370 | return false; | ||
| 371 | } | ||
| 372 | |||
| 373 | static bool read_mailbox_7(void) | ||
| 374 | { | ||
| 375 | writel(MBOX_BIT(7), PRCM_ARM_IT1_CLEAR); | ||
| 376 | return false; | ||
| 377 | } | ||
| 378 | |||
| 379 | static bool (* const read_mailbox[NUM_MB])(void) = { | ||
| 380 | read_mailbox_0, | ||
| 381 | read_mailbox_1, | ||
| 382 | read_mailbox_2, | ||
| 383 | read_mailbox_3, | ||
| 384 | read_mailbox_4, | ||
| 385 | read_mailbox_5, | ||
| 386 | read_mailbox_6, | ||
| 387 | read_mailbox_7 | ||
| 388 | }; | ||
| 389 | |||
| 390 | static irqreturn_t prcmu_irq_handler(int irq, void *data) | ||
| 391 | { | ||
| 392 | u32 bits; | ||
| 393 | u8 n; | ||
| 394 | irqreturn_t r; | ||
| 395 | |||
| 396 | bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS); | ||
| 397 | if (unlikely(!bits)) | ||
| 398 | return IRQ_NONE; | ||
| 399 | |||
| 400 | r = IRQ_HANDLED; | ||
| 401 | for (n = 0; bits; n++) { | ||
| 402 | if (bits & MBOX_BIT(n)) { | ||
| 403 | bits -= MBOX_BIT(n); | ||
| 404 | if (read_mailbox[n]()) | ||
| 405 | r = IRQ_WAKE_THREAD; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | return r; | ||
| 409 | } | ||
| 410 | |||
| 411 | static irqreturn_t prcmu_irq_thread_fn(int irq, void *data) | ||
| 412 | { | ||
| 413 | ack_dbb_wakeup(); | ||
| 414 | return IRQ_HANDLED; | ||
| 415 | } | ||
| 416 | |||
| 417 | void __init db5500_prcmu_early_init(void) | ||
| 418 | { | ||
| 419 | tcdm_base = __io_address(U5500_PRCMU_TCDM_BASE); | ||
| 420 | spin_lock_init(&mb0_transfer.lock); | ||
| 421 | mutex_init(&mb5_transfer.lock); | ||
| 422 | init_completion(&mb5_transfer.work); | ||
| 423 | } | ||
| 424 | |||
| 425 | /** | ||
| 426 | * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic | ||
| 427 | * | ||
| 428 | */ | ||
| 429 | int __init db5500_prcmu_init(void) | ||
| 430 | { | ||
| 431 | int r = 0; | ||
| 432 | |||
| 433 | if (ux500_is_svp() || !cpu_is_u5500()) | ||
| 434 | return -ENODEV; | ||
| 435 | |||
| 436 | /* Clean up the mailbox interrupts after pre-kernel code. */ | ||
| 437 | writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLEAR); | ||
| 438 | |||
| 439 | r = request_threaded_irq(IRQ_DB5500_PRCMU1, prcmu_irq_handler, | ||
| 440 | prcmu_irq_thread_fn, 0, "prcmu", NULL); | ||
| 441 | if (r < 0) { | ||
| 442 | pr_err("prcmu: Failed to allocate IRQ_DB5500_PRCMU1.\n"); | ||
| 443 | return -EBUSY; | ||
| 444 | } | ||
| 445 | return 0; | ||
| 446 | } | ||
| 447 | |||
| 448 | arch_initcall(db5500_prcmu_init); | ||
diff --git a/drivers/mfd/db8500-prcmu-regs.h b/drivers/mfd/db8500-prcmu-regs.h new file mode 100644 index 00000000000..3bbf04d5804 --- /dev/null +++ b/drivers/mfd/db8500-prcmu-regs.h | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) STMicroelectronics 2009 | ||
| 3 | * Copyright (C) ST-Ericsson SA 2010 | ||
| 4 | * | ||
| 5 | * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com> | ||
| 6 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> | ||
| 7 | * | ||
| 8 | * License Terms: GNU General Public License v2 | ||
| 9 | * | ||
| 10 | * PRCM Unit registers | ||
| 11 | */ | ||
| 12 | #ifndef __DB8500_PRCMU_REGS_H | ||
| 13 | #define __DB8500_PRCMU_REGS_H | ||
| 14 | |||
| 15 | #include <linux/bitops.h> | ||
| 16 | #include <mach/hardware.h> | ||
| 17 | |||
| 18 | #define BITS(_start, _end) ((BIT(_end) - BIT(_start)) + BIT(_end)) | ||
| 19 | |||
| 20 | #define PRCM_ARM_PLLDIVPS 0x118 | ||
| 21 | #define PRCM_ARM_PLLDIVPS_ARM_BRM_RATE BITS(0, 5) | ||
| 22 | #define PRCM_ARM_PLLDIVPS_MAX_MASK 0xF | ||
| 23 | |||
| 24 | #define PRCM_PLLARM_LOCKP 0x0A8 | ||
| 25 | #define PRCM_PLLARM_LOCKP_PRCM_PLLARM_LOCKP3 BIT(1) | ||
| 26 | |||
| 27 | #define PRCM_ARM_CHGCLKREQ 0x114 | ||
| 28 | #define PRCM_ARM_CHGCLKREQ_PRCM_ARM_CHGCLKREQ BIT(0) | ||
| 29 | |||
| 30 | #define PRCM_PLLARM_ENABLE 0x98 | ||
| 31 | #define PRCM_PLLARM_ENABLE_PRCM_PLLARM_ENABLE BIT(0) | ||
| 32 | #define PRCM_PLLARM_ENABLE_PRCM_PLLARM_COUNTON BIT(8) | ||
| 33 | |||
| 34 | #define PRCM_ARMCLKFIX_MGT 0x0 | ||
| 35 | #define PRCM_A9_RESETN_CLR 0x1f4 | ||
| 36 | #define PRCM_A9_RESETN_SET 0x1f0 | ||
| 37 | #define PRCM_ARM_LS_CLAMP 0x30C | ||
| 38 | #define PRCM_SRAM_A9 0x308 | ||
| 39 | |||
| 40 | /* ARM WFI Standby signal register */ | ||
| 41 | #define PRCM_ARM_WFI_STANDBY 0x130 | ||
| 42 | #define PRCM_IOCR 0x310 | ||
| 43 | #define PRCM_IOCR_IOFORCE BIT(0) | ||
| 44 | |||
| 45 | /* CPU mailbox registers */ | ||
| 46 | #define PRCM_MBOX_CPU_VAL 0x0FC | ||
| 47 | #define PRCM_MBOX_CPU_SET 0x100 | ||
| 48 | |||
| 49 | /* Dual A9 core interrupt management unit registers */ | ||
| 50 | #define PRCM_A9_MASK_REQ 0x328 | ||
| 51 | #define PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ BIT(0) | ||
| 52 | |||
| 53 | #define PRCM_A9_MASK_ACK 0x32C | ||
| 54 | #define PRCM_ARMITMSK31TO0 0x11C | ||
| 55 | #define PRCM_ARMITMSK63TO32 0x120 | ||
| 56 | #define PRCM_ARMITMSK95TO64 0x124 | ||
| 57 | #define PRCM_ARMITMSK127TO96 0x128 | ||
| 58 | #define PRCM_POWER_STATE_VAL 0x25C | ||
| 59 | #define PRCM_ARMITVAL31TO0 0x260 | ||
| 60 | #define PRCM_ARMITVAL63TO32 0x264 | ||
| 61 | #define PRCM_ARMITVAL95TO64 0x268 | ||
| 62 | #define PRCM_ARMITVAL127TO96 0x26C | ||
| 63 | |||
| 64 | #define PRCM_HOSTACCESS_REQ 0x334 | ||
| 65 | #define PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ BIT(0) | ||
| 66 | |||
| 67 | #define PRCM_ARM_IT1_CLR 0x48C | ||
| 68 | #define PRCM_ARM_IT1_VAL 0x494 | ||
| 69 | |||
| 70 | #define PRCM_ITSTATUS0 0x148 | ||
| 71 | #define PRCM_ITSTATUS1 0x150 | ||
| 72 | #define PRCM_ITSTATUS2 0x158 | ||
| 73 | #define PRCM_ITSTATUS3 0x160 | ||
| 74 | #define PRCM_ITSTATUS4 0x168 | ||
| 75 | #define PRCM_ITSTATUS5 0x484 | ||
| 76 | #define PRCM_ITCLEAR5 0x488 | ||
| 77 | #define PRCM_ARMIT_MASKXP70_IT 0x1018 | ||
| 78 | |||
| 79 | /* System reset register */ | ||
| 80 | #define PRCM_APE_SOFTRST 0x228 | ||
| 81 | |||
| 82 | /* Level shifter and clamp control registers */ | ||
| 83 | #define PRCM_MMIP_LS_CLAMP_SET 0x420 | ||
| 84 | #define PRCM_MMIP_LS_CLAMP_CLR 0x424 | ||
| 85 | |||
| 86 | /* PRCMU HW semaphore */ | ||
| 87 | #define PRCM_SEM 0x400 | ||
| 88 | #define PRCM_SEM_PRCM_SEM BIT(0) | ||
| 89 | |||
| 90 | /* PRCMU clock/PLL/reset registers */ | ||
| 91 | #define PRCM_PLLDSI_FREQ 0x500 | ||
| 92 | #define PRCM_PLLDSI_ENABLE 0x504 | ||
| 93 | #define PRCM_PLLDSI_LOCKP 0x508 | ||
| 94 | #define PRCM_DSI_PLLOUT_SEL 0x530 | ||
| 95 | #define PRCM_DSITVCLK_DIV 0x52C | ||
| 96 | #define PRCM_APE_RESETN_SET 0x1E4 | ||
| 97 | #define PRCM_APE_RESETN_CLR 0x1E8 | ||
| 98 | |||
| 99 | #define PRCM_TCR 0x1C8 | ||
| 100 | #define PRCM_TCR_TENSEL_MASK BITS(0, 7) | ||
| 101 | #define PRCM_TCR_STOP_TIMERS BIT(16) | ||
| 102 | #define PRCM_TCR_DOZE_MODE BIT(17) | ||
| 103 | |||
| 104 | #define PRCM_CLKOCR 0x1CC | ||
| 105 | #define PRCM_CLKOCR_CLKODIV0_SHIFT 0 | ||
| 106 | #define PRCM_CLKOCR_CLKODIV0_MASK BITS(0, 5) | ||
| 107 | #define PRCM_CLKOCR_CLKOSEL0_SHIFT 6 | ||
| 108 | #define PRCM_CLKOCR_CLKOSEL0_MASK BITS(6, 8) | ||
| 109 | #define PRCM_CLKOCR_CLKODIV1_SHIFT 16 | ||
| 110 | #define PRCM_CLKOCR_CLKODIV1_MASK BITS(16, 21) | ||
| 111 | #define PRCM_CLKOCR_CLKOSEL1_SHIFT 22 | ||
| 112 | #define PRCM_CLKOCR_CLKOSEL1_MASK BITS(22, 24) | ||
| 113 | #define PRCM_CLKOCR_CLK1TYPE BIT(28) | ||
| 114 | |||
| 115 | #define PRCM_SGACLK_MGT 0x014 | ||
| 116 | #define PRCM_UARTCLK_MGT 0x018 | ||
| 117 | #define PRCM_MSP02CLK_MGT 0x01C | ||
| 118 | #define PRCM_MSP1CLK_MGT 0x288 | ||
| 119 | #define PRCM_I2CCLK_MGT 0x020 | ||
| 120 | #define PRCM_SDMMCCLK_MGT 0x024 | ||
| 121 | #define PRCM_SLIMCLK_MGT 0x028 | ||
| 122 | #define PRCM_PER1CLK_MGT 0x02C | ||
| 123 | #define PRCM_PER2CLK_MGT 0x030 | ||
| 124 | #define PRCM_PER3CLK_MGT 0x034 | ||
| 125 | #define PRCM_PER5CLK_MGT 0x038 | ||
| 126 | #define PRCM_PER6CLK_MGT 0x03C | ||
| 127 | #define PRCM_PER7CLK_MGT 0x040 | ||
| 128 | #define PRCM_LCDCLK_MGT 0x044 | ||
| 129 | #define PRCM_BMLCLK_MGT 0x04C | ||
| 130 | #define PRCM_HSITXCLK_MGT 0x050 | ||
| 131 | #define PRCM_HSIRXCLK_MGT 0x054 | ||
| 132 | #define PRCM_HDMICLK_MGT 0x058 | ||
| 133 | #define PRCM_APEATCLK_MGT 0x05C | ||
| 134 | #define PRCM_APETRACECLK_MGT 0x060 | ||
| 135 | #define PRCM_MCDECLK_MGT 0x064 | ||
| 136 | #define PRCM_IPI2CCLK_MGT 0x068 | ||
| 137 | #define PRCM_DSIALTCLK_MGT 0x06C | ||
| 138 | #define PRCM_DMACLK_MGT 0x074 | ||
| 139 | #define PRCM_B2R2CLK_MGT 0x078 | ||
| 140 | #define PRCM_TVCLK_MGT 0x07C | ||
| 141 | #define PRCM_UNIPROCLK_MGT 0x278 | ||
| 142 | #define PRCM_SSPCLK_MGT 0x280 | ||
| 143 | #define PRCM_RNGCLK_MGT 0x284 | ||
| 144 | #define PRCM_UICCCLK_MGT 0x27C | ||
| 145 | |||
| 146 | #define PRCM_CLK_MGT_CLKPLLDIV_MASK BITS(0, 4) | ||
| 147 | #define PRCM_CLK_MGT_CLKPLLSW_MASK BITS(5, 7) | ||
| 148 | #define PRCM_CLK_MGT_CLKEN BIT(8) | ||
| 149 | |||
| 150 | /* ePOD and memory power signal control registers */ | ||
| 151 | #define PRCM_EPOD_C_SET 0x410 | ||
| 152 | #define PRCM_SRAM_LS_SLEEP 0x304 | ||
| 153 | |||
| 154 | /* Debug power control unit registers */ | ||
| 155 | #define PRCM_POWER_STATE_SET 0x254 | ||
| 156 | |||
| 157 | /* Miscellaneous unit registers */ | ||
| 158 | #define PRCM_DSI_SW_RESET 0x324 | ||
| 159 | #define PRCM_GPIOCR 0x138 | ||
| 160 | |||
| 161 | /* GPIOCR register */ | ||
| 162 | #define PRCM_GPIOCR_SPI2_SELECT BIT(23) | ||
| 163 | |||
| 164 | #define PRCM_DDR_SUBSYS_APE_MINBW 0x438 | ||
| 165 | |||
| 166 | #endif /* __DB8500_PRCMU_REGS_H */ | ||
diff --git a/drivers/mfd/max77663-core.c b/drivers/mfd/max77663-core.c new file mode 100644 index 00000000000..ff47cb123d0 --- /dev/null +++ b/drivers/mfd/max77663-core.c | |||
| @@ -0,0 +1,1445 @@ | |||
| 1 | /* | ||
| 2 | * drivers/mfd/max77663-core.c | ||
| 3 | * Max77663 mfd driver (I2C bus access) | ||
| 4 | * | ||
| 5 | * Copyright 2011 Maxim Integrated Products, Inc. | ||
| 6 | * Copyright (C) 2011-2012 NVIDIA Corporation | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU General Public License as | ||
| 10 | * published by the Free Software Foundation; either version 2 of the | ||
| 11 | * License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <linux/interrupt.h> | ||
| 16 | #include <linux/i2c.h> | ||
| 17 | #include <linux/gpio.h> | ||
| 18 | #include <linux/slab.h> | ||
| 19 | #include <linux/ratelimit.h> | ||
| 20 | #include <linux/kthread.h> | ||
| 21 | #include <linux/mfd/core.h> | ||
| 22 | #include <linux/platform_device.h> | ||
| 23 | #include <linux/seq_file.h> | ||
| 24 | #include <linux/debugfs.h> | ||
| 25 | #include <linux/delay.h> | ||
| 26 | #include <linux/uaccess.h> | ||
| 27 | |||
| 28 | #include <linux/mfd/max77663-core.h> | ||
| 29 | |||
| 30 | /* RTC i2c slave address */ | ||
| 31 | #define MAX77663_RTC_I2C_ADDR 0x48 | ||
| 32 | |||
| 33 | /* Registers */ | ||
| 34 | #define MAX77663_REG_IRQ_TOP 0x05 | ||
| 35 | #define MAX77663_REG_LBT_IRQ 0x06 | ||
| 36 | #define MAX77663_REG_SD_IRQ 0x07 | ||
| 37 | #define MAX77663_REG_LDOX_IRQ 0x08 | ||
| 38 | #define MAX77663_REG_LDO8_IRQ 0x09 | ||
| 39 | #define MAX77663_REG_GPIO_IRQ 0x0A | ||
| 40 | #define MAX77663_REG_ONOFF_IRQ 0x0B | ||
| 41 | #define MAX77663_REG_NVER 0x0C | ||
| 42 | #define MAX77663_REG_IRQ_TOP_MASK 0x0D | ||
| 43 | #define MAX77663_REG_LBT_IRQ_MASK 0x0E | ||
| 44 | #define MAX77663_REG_SD_IRQ_MASK 0x0F | ||
| 45 | #define MAX77663_REG_LDOX_IRQ_MASK 0x10 | ||
| 46 | #define MAX77663_REG_LDO8_IRQ_MASK 0x11 | ||
| 47 | #define MAX77663_REG_ONOFF_IRQ_MASK 0x12 | ||
| 48 | #define MAX77663_REG_GPIO_CTRL0 0x36 | ||
| 49 | #define MAX77663_REG_GPIO_CTRL1 0x37 | ||
| 50 | #define MAX77663_REG_GPIO_CTRL2 0x38 | ||
| 51 | #define MAX77663_REG_GPIO_CTRL3 0x39 | ||
| 52 | #define MAX77663_REG_GPIO_CTRL4 0x3A | ||
| 53 | #define MAX77663_REG_GPIO_CTRL5 0x3B | ||
| 54 | #define MAX77663_REG_GPIO_CTRL6 0x3C | ||
| 55 | #define MAX77663_REG_GPIO_CTRL7 0x3D | ||
| 56 | #define MAX77663_REG_GPIO_PU 0x3E | ||
| 57 | #define MAX77663_REG_GPIO_PD 0x3F | ||
| 58 | #define MAX77663_REG_GPIO_ALT 0x40 | ||
| 59 | #define MAX77663_REG_ONOFF_CFG1 0x41 | ||
| 60 | #define MAX77663_REG_ONOFF_CFG2 0x42 | ||
| 61 | |||
| 62 | #define IRQ_TOP_GLBL_MASK (1 << 7) | ||
| 63 | #define IRQ_TOP_GLBL_SHIFT 7 | ||
| 64 | #define IRQ_TOP_SD_MASK (1 << 6) | ||
| 65 | #define IRQ_TOP_SD_SHIFT 6 | ||
| 66 | #define IRQ_TOP_LDO_MASK (1 << 5) | ||
| 67 | #define IRQ_TOP_LDO_SHIFT 5 | ||
| 68 | #define IRQ_TOP_GPIO_MASK (1 << 4) | ||
| 69 | #define IRQ_TOP_GPIO_SHIFT 4 | ||
| 70 | #define IRQ_TOP_RTC_MASK (1 << 3) | ||
| 71 | #define IRQ_TOP_RTC_SHIFT 3 | ||
| 72 | #define IRQ_TOP_32K_MASK (1 << 2) | ||
| 73 | #define IRQ_TOP_32K_SHIFT 2 | ||
| 74 | #define IRQ_TOP_ONOFF_MASK (1 << 1) | ||
| 75 | #define IRQ_TOP_ONOFF_SHIFT 1 | ||
| 76 | #define IRQ_TOP_NVER_MASK (1 << 0) | ||
| 77 | #define IRQ_TOP_NVER_SHIFT 0 | ||
| 78 | |||
| 79 | #define IRQ_GLBL_MASK (1 << 0) | ||
| 80 | |||
| 81 | #define IRQ_LBT_BASE MAX77663_IRQ_LBT_LB | ||
| 82 | #define IRQ_LBT_END MAX77663_IRQ_LBT_THERM_ALRM2 | ||
| 83 | |||
| 84 | #define IRQ_GPIO_BASE MAX77663_IRQ_GPIO0 | ||
| 85 | #define IRQ_GPIO_END MAX77663_IRQ_GPIO7 | ||
| 86 | |||
| 87 | #define IRQ_ONOFF_BASE MAX77663_IRQ_ONOFF_HRDPOWRN | ||
| 88 | #define IRQ_ONOFF_END MAX77663_IRQ_ONOFF_ACOK_RISING | ||
| 89 | |||
| 90 | #define GPIO_REG_ADDR(offset) (MAX77663_REG_GPIO_CTRL0 + offset) | ||
| 91 | |||
| 92 | #define GPIO_CTRL_DBNC_MASK (3 << 6) | ||
| 93 | #define GPIO_CTRL_DBNC_SHIFT 6 | ||
| 94 | #define GPIO_CTRL_REFE_IRQ_MASK (3 << 4) | ||
| 95 | #define GPIO_CTRL_REFE_IRQ_SHIFT 4 | ||
| 96 | #define GPIO_CTRL_DOUT_MASK (1 << 3) | ||
| 97 | #define GPIO_CTRL_DOUT_SHIFT 3 | ||
| 98 | #define GPIO_CTRL_DIN_MASK (1 << 2) | ||
| 99 | #define GPIO_CTRL_DIN_SHIFT 2 | ||
| 100 | #define GPIO_CTRL_DIR_MASK (1 << 1) | ||
| 101 | #define GPIO_CTRL_DIR_SHIFT 1 | ||
| 102 | #define GPIO_CTRL_OUT_DRV_MASK (1 << 0) | ||
| 103 | #define GPIO_CTRL_OUT_DRV_SHIFT 0 | ||
| 104 | |||
| 105 | #define GPIO_REFE_IRQ_NONE 0 | ||
| 106 | #define GPIO_REFE_IRQ_EDGE_FALLING 1 | ||
| 107 | #define GPIO_REFE_IRQ_EDGE_RISING 2 | ||
| 108 | #define GPIO_REFE_IRQ_EDGE_BOTH 3 | ||
| 109 | |||
| 110 | #define GPIO_DBNC_NONE 0 | ||
| 111 | #define GPIO_DBNC_8MS 1 | ||
| 112 | #define GPIO_DBNC_16MS 2 | ||
| 113 | #define GPIO_DBNC_32MS 3 | ||
| 114 | |||
| 115 | #define ONOFF_SFT_RST_MASK (1 << 7) | ||
| 116 | #define ONOFF_SLPEN_MASK (1 << 2) | ||
| 117 | #define ONOFF_PWR_OFF_MASK (1 << 1) | ||
| 118 | |||
| 119 | #define ONOFF_SLP_LPM_MASK (1 << 5) | ||
| 120 | |||
| 121 | enum { | ||
| 122 | CACHE_IRQ_LBT, | ||
| 123 | CACHE_IRQ_SD, | ||
| 124 | CACHE_IRQ_LDO, | ||
| 125 | CACHE_IRQ_ONOFF, | ||
| 126 | CACHE_IRQ_NR, | ||
| 127 | }; | ||
| 128 | |||
| 129 | struct max77663_irq_data { | ||
| 130 | int mask_reg; | ||
| 131 | u16 mask; | ||
| 132 | u8 top_mask; | ||
| 133 | u8 top_shift; | ||
| 134 | int cache_idx; | ||
| 135 | bool is_rtc; | ||
| 136 | bool is_unmask; | ||
| 137 | u8 trigger_type; | ||
| 138 | }; | ||
| 139 | |||
| 140 | struct max77663_chip { | ||
| 141 | struct device *dev; | ||
| 142 | struct i2c_client *i2c_power; | ||
| 143 | struct i2c_client *i2c_rtc; | ||
| 144 | |||
| 145 | struct max77663_platform_data *pdata; | ||
| 146 | struct mutex io_lock; | ||
| 147 | |||
| 148 | struct irq_chip irq; | ||
| 149 | struct mutex irq_lock; | ||
| 150 | int irq_base; | ||
| 151 | int irq_top_count[8]; | ||
| 152 | u8 cache_irq_top_mask; | ||
| 153 | u16 cache_irq_mask[CACHE_IRQ_NR]; | ||
| 154 | |||
| 155 | struct gpio_chip gpio; | ||
| 156 | int gpio_base; | ||
| 157 | u8 cache_gpio_ctrl[MAX77663_GPIO_NR]; | ||
| 158 | u8 cache_gpio_pu; | ||
| 159 | u8 cache_gpio_pd; | ||
| 160 | u8 cache_gpio_alt; | ||
| 161 | |||
| 162 | u8 rtc_i2c_addr; | ||
| 163 | }; | ||
| 164 | |||
| 165 | struct max77663_chip *max77663_chip; | ||
| 166 | |||
| 167 | #define IRQ_DATA_LBT(_name, _shift) \ | ||
| 168 | [MAX77663_IRQ_LBT_##_name] = { \ | ||
| 169 | .mask_reg = MAX77663_REG_LBT_IRQ_MASK, \ | ||
| 170 | .mask = (1 << _shift), \ | ||
| 171 | .top_mask = IRQ_TOP_GLBL_MASK, \ | ||
| 172 | .top_shift = IRQ_TOP_GLBL_SHIFT, \ | ||
| 173 | .cache_idx = CACHE_IRQ_LBT, \ | ||
| 174 | } | ||
| 175 | |||
| 176 | #define IRQ_DATA_GPIO(_name) \ | ||
| 177 | [MAX77663_IRQ_GPIO##_name] = { \ | ||
| 178 | .mask = (1 << _name), \ | ||
| 179 | .top_mask = IRQ_TOP_GPIO_MASK, \ | ||
| 180 | .top_shift = IRQ_TOP_GPIO_SHIFT, \ | ||
| 181 | .cache_idx = -1, \ | ||
| 182 | } | ||
| 183 | |||
| 184 | #define IRQ_DATA_ONOFF(_name, _shift) \ | ||
| 185 | [MAX77663_IRQ_ONOFF_##_name] = { \ | ||
| 186 | .mask_reg = MAX77663_REG_ONOFF_IRQ_MASK,\ | ||
| 187 | .mask = (1 << _shift), \ | ||
| 188 | .top_mask = IRQ_TOP_ONOFF_MASK, \ | ||
| 189 | .top_shift = IRQ_TOP_ONOFF_SHIFT, \ | ||
| 190 | .cache_idx = CACHE_IRQ_ONOFF, \ | ||
| 191 | } | ||
| 192 | |||
| 193 | static struct max77663_irq_data max77663_irqs[MAX77663_IRQ_NR] = { | ||
| 194 | IRQ_DATA_LBT(LB, 3), | ||
| 195 | IRQ_DATA_LBT(THERM_ALRM1, 2), | ||
| 196 | IRQ_DATA_LBT(THERM_ALRM2, 1), | ||
| 197 | IRQ_DATA_GPIO(0), | ||
| 198 | IRQ_DATA_GPIO(1), | ||
| 199 | IRQ_DATA_GPIO(2), | ||
| 200 | IRQ_DATA_GPIO(3), | ||
| 201 | IRQ_DATA_GPIO(4), | ||
| 202 | IRQ_DATA_GPIO(5), | ||
| 203 | IRQ_DATA_GPIO(6), | ||
| 204 | IRQ_DATA_GPIO(7), | ||
| 205 | IRQ_DATA_ONOFF(HRDPOWRN, 0), | ||
| 206 | IRQ_DATA_ONOFF(EN0_1SEC, 1), | ||
| 207 | IRQ_DATA_ONOFF(EN0_FALLING, 2), | ||
| 208 | IRQ_DATA_ONOFF(EN0_RISING, 3), | ||
| 209 | IRQ_DATA_ONOFF(LID_FALLING, 4), | ||
| 210 | IRQ_DATA_ONOFF(LID_RISING, 5), | ||
| 211 | IRQ_DATA_ONOFF(ACOK_FALLING, 6), | ||
| 212 | IRQ_DATA_ONOFF(ACOK_RISING, 7), | ||
| 213 | [MAX77663_IRQ_RTC] = { | ||
| 214 | .top_mask = IRQ_TOP_RTC_MASK, | ||
| 215 | .top_shift = IRQ_TOP_RTC_SHIFT, | ||
| 216 | .cache_idx = -1, | ||
| 217 | .is_rtc = 1, | ||
| 218 | }, | ||
| 219 | [MAX77663_IRQ_SD_PF] = { | ||
| 220 | .mask_reg = MAX77663_REG_SD_IRQ_MASK, | ||
| 221 | .mask = 0xF8, | ||
| 222 | .top_mask = IRQ_TOP_SD_MASK, | ||
| 223 | .top_shift = IRQ_TOP_SD_SHIFT, | ||
| 224 | .cache_idx = CACHE_IRQ_SD, | ||
| 225 | }, | ||
| 226 | [MAX77663_IRQ_LDO_PF] = { | ||
| 227 | .mask_reg = MAX77663_REG_LDOX_IRQ_MASK, | ||
| 228 | .mask = 0x1FF, | ||
| 229 | .top_mask = IRQ_TOP_LDO_MASK, | ||
| 230 | .top_shift = IRQ_TOP_LDO_SHIFT, | ||
| 231 | .cache_idx = CACHE_IRQ_LDO, | ||
| 232 | }, | ||
| 233 | [MAX77663_IRQ_32K] = { | ||
| 234 | .top_mask = IRQ_TOP_32K_MASK, | ||
| 235 | .top_shift = IRQ_TOP_32K_SHIFT, | ||
| 236 | .cache_idx = -1, | ||
| 237 | }, | ||
| 238 | [MAX77663_IRQ_NVER] = { | ||
| 239 | .top_mask = IRQ_TOP_NVER_MASK, | ||
| 240 | .top_shift = IRQ_TOP_NVER_SHIFT, | ||
| 241 | .cache_idx = -1, | ||
| 242 | }, | ||
| 243 | }; | ||
| 244 | |||
| 245 | /* MAX77663 PMU doesn't allow PWR_OFF and SFT_RST setting in ONOFF_CFG1 | ||
| 246 | * at the same time. So if it try to set PWR_OFF and SFT_RST to ONOFF_CFG1 | ||
| 247 | * simultaneously, handle only SFT_RST and ignore PWR_OFF. | ||
| 248 | */ | ||
| 249 | #define CHECK_ONOFF_CFG1_MASK (ONOFF_SFT_RST_MASK | ONOFF_PWR_OFF_MASK) | ||
| 250 | #define CHECK_ONOFF_CFG1(_addr, _val) \ | ||
| 251 | unlikely((_addr == MAX77663_REG_ONOFF_CFG1) && \ | ||
| 252 | ((_val & CHECK_ONOFF_CFG1_MASK) == CHECK_ONOFF_CFG1_MASK)) | ||
| 253 | |||
| 254 | static inline int max77663_i2c_write(struct i2c_client *client, u8 addr, | ||
| 255 | void *src, u32 bytes) | ||
| 256 | { | ||
| 257 | u8 buf[bytes + 1]; | ||
| 258 | int ret; | ||
| 259 | |||
| 260 | dev_dbg(&client->dev, "i2c_write: addr=0x%02x, src=0x%02x, bytes=%u\n", | ||
| 261 | addr, *((u8 *)src), bytes); | ||
| 262 | |||
| 263 | if (client->addr == max77663_chip->rtc_i2c_addr) { | ||
| 264 | /* RTC registers support sequential writing */ | ||
| 265 | buf[0] = addr; | ||
| 266 | memcpy(&buf[1], src, bytes); | ||
| 267 | } else { | ||
| 268 | /* Power registers support register-data pair writing */ | ||
| 269 | u8 *src8 = (u8 *)src; | ||
| 270 | int i; | ||
| 271 | |||
| 272 | for (i = 0; i < (bytes * 2); i++) { | ||
| 273 | if (i % 2) { | ||
| 274 | if (CHECK_ONOFF_CFG1(buf[i - 1], *src8)) | ||
| 275 | buf[i] = *src8++ & ~ONOFF_PWR_OFF_MASK; | ||
| 276 | else | ||
| 277 | buf[i] = *src8++; | ||
| 278 | } else { | ||
| 279 | buf[i] = addr++; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | bytes = (bytes * 2) - 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | ret = i2c_master_send(client, buf, bytes + 1); | ||
| 286 | if (ret < 0) | ||
| 287 | return ret; | ||
| 288 | return 0; | ||
| 289 | } | ||
| 290 | |||
| 291 | static inline int max77663_i2c_read(struct i2c_client *client, u8 addr, | ||
| 292 | void *dest, u32 bytes) | ||
| 293 | { | ||
| 294 | int ret; | ||
| 295 | |||
| 296 | if (bytes > 1) { | ||
| 297 | ret = i2c_smbus_read_i2c_block_data(client, addr, bytes, dest); | ||
| 298 | if (ret < 0) | ||
| 299 | return ret; | ||
| 300 | } else { | ||
| 301 | ret = i2c_smbus_read_byte_data(client, addr); | ||
| 302 | if (ret < 0) | ||
| 303 | return ret; | ||
| 304 | |||
| 305 | *((u8 *)dest) = (u8)ret; | ||
| 306 | } | ||
| 307 | |||
| 308 | dev_dbg(&client->dev, "i2c_read: addr=0x%02x, dest=0x%02x, bytes=%u\n", | ||
| 309 | addr, *((u8 *)dest), bytes); | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | int max77663_read(struct device *dev, u8 addr, void *values, u32 len, | ||
| 314 | bool is_rtc) | ||
| 315 | { | ||
| 316 | struct max77663_chip *chip = dev_get_drvdata(dev); | ||
| 317 | struct i2c_client *client = NULL; | ||
| 318 | int ret; | ||
| 319 | |||
| 320 | mutex_lock(&chip->io_lock); | ||
| 321 | if (!is_rtc) | ||
| 322 | client = chip->i2c_power; | ||
| 323 | else | ||
| 324 | client = chip->i2c_rtc; | ||
| 325 | |||
| 326 | ret = max77663_i2c_read(client, addr, values, len); | ||
| 327 | mutex_unlock(&chip->io_lock); | ||
| 328 | return ret; | ||
| 329 | } | ||
| 330 | EXPORT_SYMBOL(max77663_read); | ||
| 331 | |||
| 332 | int max77663_write(struct device *dev, u8 addr, void *values, u32 len, | ||
| 333 | bool is_rtc) | ||
| 334 | { | ||
| 335 | struct max77663_chip *chip = dev_get_drvdata(dev); | ||
| 336 | struct i2c_client *client = NULL; | ||
| 337 | int ret; | ||
| 338 | |||
| 339 | mutex_lock(&chip->io_lock); | ||
| 340 | if (!is_rtc) | ||
| 341 | client = chip->i2c_power; | ||
| 342 | else | ||
| 343 | client = chip->i2c_rtc; | ||
| 344 | |||
| 345 | ret = max77663_i2c_write(client, addr, values, len); | ||
| 346 | mutex_unlock(&chip->io_lock); | ||
| 347 | return ret; | ||
| 348 | } | ||
| 349 | EXPORT_SYMBOL(max77663_write); | ||
| 350 | |||
| 351 | int max77663_set_bits(struct device *dev, u8 addr, u8 mask, u8 value, | ||
| 352 | bool is_rtc) | ||
| 353 | { | ||
| 354 | struct max77663_chip *chip = dev_get_drvdata(dev); | ||
| 355 | struct i2c_client *client = NULL; | ||
| 356 | u8 tmp; | ||
| 357 | int ret; | ||
| 358 | |||
| 359 | mutex_lock(&chip->io_lock); | ||
| 360 | if (!is_rtc) | ||
| 361 | client = chip->i2c_power; | ||
| 362 | else | ||
| 363 | client = chip->i2c_rtc; | ||
| 364 | |||
| 365 | ret = max77663_i2c_read(client, addr, &tmp, 1); | ||
| 366 | if (ret == 0) { | ||
| 367 | value = (tmp & ~mask) | (value & mask); | ||
| 368 | ret = max77663_i2c_write(client, addr, &value, 1); | ||
| 369 | } | ||
| 370 | mutex_unlock(&chip->io_lock); | ||
| 371 | return ret; | ||
| 372 | } | ||
| 373 | EXPORT_SYMBOL(max77663_set_bits); | ||
| 374 | |||
| 375 | static void max77663_power_off(void) | ||
| 376 | { | ||
| 377 | struct max77663_chip *chip = max77663_chip; | ||
| 378 | |||
| 379 | if (!chip) | ||
| 380 | return; | ||
| 381 | |||
| 382 | dev_info(chip->dev, "%s: Global shutdown\n", __func__); | ||
| 383 | max77663_set_bits(chip->dev, MAX77663_REG_ONOFF_CFG1, | ||
| 384 | ONOFF_SFT_RST_MASK, ONOFF_SFT_RST_MASK, 0); | ||
| 385 | } | ||
| 386 | |||
| 387 | static int max77663_sleep(struct max77663_chip *chip, bool on) | ||
| 388 | { | ||
| 389 | int ret = 0; | ||
| 390 | |||
| 391 | if (chip->pdata->flags & SLP_LPM_ENABLE) { | ||
| 392 | /* Put the power rails into Low-Power mode during sleep mode, | ||
| 393 | * if the power rail's power mode is GLPM. */ | ||
| 394 | ret = max77663_set_bits(chip->dev, MAX77663_REG_ONOFF_CFG2, | ||
| 395 | ONOFF_SLP_LPM_MASK, | ||
| 396 | on ? ONOFF_SLP_LPM_MASK : 0, 0); | ||
| 397 | if (ret < 0) | ||
| 398 | return ret; | ||
| 399 | } | ||
| 400 | |||
| 401 | /* Enable sleep that AP can be placed into sleep mode | ||
| 402 | * by pulling EN1 low */ | ||
| 403 | return max77663_set_bits(chip->dev, MAX77663_REG_ONOFF_CFG1, | ||
| 404 | ONOFF_SLPEN_MASK, | ||
| 405 | on ? ONOFF_SLPEN_MASK : 0, 0); | ||
| 406 | } | ||
| 407 | |||
| 408 | static inline int max77663_cache_write(struct device *dev, u8 addr, u8 mask, | ||
| 409 | u8 val, u8 *cache) | ||
| 410 | { | ||
| 411 | u8 new_val; | ||
| 412 | int ret; | ||
| 413 | |||
| 414 | new_val = (*cache & ~mask) | (val & mask); | ||
| 415 | if (*cache != new_val) { | ||
| 416 | ret = max77663_write(dev, addr, &new_val, 1, 0); | ||
| 417 | if (ret < 0) | ||
| 418 | return ret; | ||
| 419 | *cache = new_val; | ||
| 420 | } | ||
| 421 | return 0; | ||
| 422 | } | ||
| 423 | |||
| 424 | static inline | ||
| 425 | struct max77663_chip *max77663_chip_from_gpio(struct gpio_chip *gpio) | ||
| 426 | { | ||
| 427 | return container_of(gpio, struct max77663_chip, gpio); | ||
| 428 | } | ||
| 429 | |||
| 430 | static int max77663_gpio_set_pull_up(struct max77663_chip *chip, int offset, | ||
| 431 | int pull_up) | ||
| 432 | { | ||
| 433 | u8 val = 0; | ||
| 434 | |||
| 435 | if ((offset < MAX77663_GPIO0) || (MAX77663_GPIO7 < offset)) | ||
| 436 | return -EINVAL; | ||
| 437 | |||
| 438 | if (pull_up == GPIO_PU_ENABLE) | ||
| 439 | val = (1 << offset); | ||
| 440 | |||
| 441 | return max77663_cache_write(chip->dev, MAX77663_REG_GPIO_PU, | ||
| 442 | (1 << offset), val, &chip->cache_gpio_pu); | ||
| 443 | } | ||
| 444 | |||
| 445 | static int max77663_gpio_set_pull_down(struct max77663_chip *chip, int offset, | ||
| 446 | int pull_down) | ||
| 447 | { | ||
| 448 | u8 val = 0; | ||
| 449 | |||
| 450 | if ((offset < MAX77663_GPIO0) || (MAX77663_GPIO7 < offset)) | ||
| 451 | return -EINVAL; | ||
| 452 | |||
| 453 | if (pull_down == GPIO_PD_ENABLE) | ||
| 454 | val = (1 << offset); | ||
| 455 | |||
| 456 | return max77663_cache_write(chip->dev, MAX77663_REG_GPIO_PD, | ||
| 457 | (1 << offset), val, &chip->cache_gpio_pd); | ||
| 458 | } | ||
| 459 | |||
| 460 | static inline | ||
| 461 | int max77663_gpio_is_alternate(struct max77663_chip *chip, int offset) | ||
| 462 | { | ||
| 463 | return (chip->cache_gpio_alt & (1 << offset)) ? 1 : 0; | ||
| 464 | } | ||
| 465 | |||
| 466 | int max77663_gpio_set_alternate(int gpio, int alternate) | ||
| 467 | { | ||
| 468 | struct max77663_chip *chip = max77663_chip; | ||
| 469 | u8 val = 0; | ||
| 470 | int ret = 0; | ||
| 471 | |||
| 472 | if (!chip) | ||
| 473 | return -ENXIO; | ||
| 474 | |||
| 475 | gpio -= chip->gpio_base; | ||
| 476 | if ((gpio < MAX77663_GPIO0) || (MAX77663_GPIO7 < gpio)) | ||
| 477 | return -EINVAL; | ||
| 478 | |||
| 479 | if (alternate == GPIO_ALT_ENABLE) { | ||
| 480 | val = (1 << gpio); | ||
| 481 | if (gpio == MAX77663_GPIO7) { | ||
| 482 | ret = max77663_gpio_set_pull_up(chip, gpio, 0); | ||
| 483 | if (ret < 0) | ||
| 484 | return ret; | ||
| 485 | |||
| 486 | ret = max77663_gpio_set_pull_down(chip, gpio, 0); | ||
| 487 | if (ret < 0) | ||
| 488 | return ret; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | return max77663_cache_write(chip->dev, MAX77663_REG_GPIO_ALT, | ||
| 493 | (1 << gpio), val, &chip->cache_gpio_alt); | ||
| 494 | } | ||
| 495 | EXPORT_SYMBOL(max77663_gpio_set_alternate); | ||
| 496 | |||
| 497 | static int max77663_gpio_dir_input(struct gpio_chip *gpio, unsigned offset) | ||
| 498 | { | ||
| 499 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 500 | |||
| 501 | if (max77663_gpio_is_alternate(chip, offset)) { | ||
| 502 | dev_warn(chip->dev, "gpio_dir_input: " | ||
| 503 | "gpio%u is used as alternate mode\n", offset); | ||
| 504 | return 0; | ||
| 505 | } | ||
| 506 | |||
| 507 | return max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), | ||
| 508 | GPIO_CTRL_DIR_MASK, GPIO_CTRL_DIR_MASK, | ||
| 509 | &chip->cache_gpio_ctrl[offset]); | ||
| 510 | } | ||
| 511 | |||
| 512 | static int max77663_gpio_get(struct gpio_chip *gpio, unsigned offset) | ||
| 513 | { | ||
| 514 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 515 | u8 val; | ||
| 516 | int ret; | ||
| 517 | |||
| 518 | if (max77663_gpio_is_alternate(chip, offset)) { | ||
| 519 | dev_warn(chip->dev, "gpio_get: " | ||
| 520 | "gpio%u is used as alternate mode\n", offset); | ||
| 521 | return 0; | ||
| 522 | } | ||
| 523 | |||
| 524 | ret = max77663_read(chip->dev, GPIO_REG_ADDR(offset), &val, 1, 0); | ||
| 525 | if (ret < 0) | ||
| 526 | return ret; | ||
| 527 | |||
| 528 | chip->cache_gpio_ctrl[offset] = val; | ||
| 529 | return (val & GPIO_CTRL_DIN_MASK) >> GPIO_CTRL_DIN_SHIFT; | ||
| 530 | } | ||
| 531 | |||
| 532 | static int max77663_gpio_dir_output(struct gpio_chip *gpio, unsigned offset, | ||
| 533 | int value) | ||
| 534 | { | ||
| 535 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 536 | u8 mask = GPIO_CTRL_DIR_MASK | GPIO_CTRL_DOUT_MASK; | ||
| 537 | u8 val = (value ? 1 : 0) << GPIO_CTRL_DOUT_SHIFT; | ||
| 538 | |||
| 539 | if (max77663_gpio_is_alternate(chip, offset)) { | ||
| 540 | dev_warn(chip->dev, "gpio_dir_output: " | ||
| 541 | "gpio%u is used as alternate mode\n", offset); | ||
| 542 | return 0; | ||
| 543 | } | ||
| 544 | |||
| 545 | return max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), mask, val, | ||
| 546 | &chip->cache_gpio_ctrl[offset]); | ||
| 547 | } | ||
| 548 | |||
| 549 | static int max77663_gpio_set_debounce(struct gpio_chip *gpio, unsigned offset, | ||
| 550 | unsigned debounce) | ||
| 551 | { | ||
| 552 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 553 | u8 shift = GPIO_CTRL_DBNC_SHIFT; | ||
| 554 | u8 val = 0; | ||
| 555 | |||
| 556 | if (max77663_gpio_is_alternate(chip, offset)) { | ||
| 557 | dev_warn(chip->dev, "gpio_set_debounce: " | ||
| 558 | "gpio%u is used as alternate mode\n", offset); | ||
| 559 | return 0; | ||
| 560 | } | ||
| 561 | |||
| 562 | if (debounce == 0) | ||
| 563 | val = 0; | ||
| 564 | else if ((0 < debounce) && (debounce <= 8)) | ||
| 565 | val = (GPIO_DBNC_8MS << shift); | ||
| 566 | else if ((8 < debounce) && (debounce <= 16)) | ||
| 567 | val = (GPIO_DBNC_16MS << shift); | ||
| 568 | else if ((16 < debounce) && (debounce <= 32)) | ||
| 569 | val = (GPIO_DBNC_32MS << shift); | ||
| 570 | else | ||
| 571 | return -EINVAL; | ||
| 572 | |||
| 573 | return max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), | ||
| 574 | GPIO_CTRL_DBNC_MASK, val, | ||
| 575 | &chip->cache_gpio_ctrl[offset]); | ||
| 576 | } | ||
| 577 | |||
| 578 | static void max77663_gpio_set(struct gpio_chip *gpio, unsigned offset, | ||
| 579 | int value) | ||
| 580 | { | ||
| 581 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 582 | u8 val = (value ? 1 : 0) << GPIO_CTRL_DOUT_SHIFT; | ||
| 583 | |||
| 584 | if (max77663_gpio_is_alternate(chip, offset)) { | ||
| 585 | dev_warn(chip->dev, "gpio_set: " | ||
| 586 | "gpio%u is used as alternate mode\n", offset); | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | |||
| 590 | max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), | ||
| 591 | GPIO_CTRL_DOUT_MASK, val, | ||
| 592 | &chip->cache_gpio_ctrl[offset]); | ||
| 593 | } | ||
| 594 | |||
| 595 | static int max77663_gpio_to_irq(struct gpio_chip *gpio, unsigned offset) | ||
| 596 | { | ||
| 597 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 598 | |||
| 599 | return chip->irq_base + IRQ_GPIO_BASE + offset; | ||
| 600 | } | ||
| 601 | |||
| 602 | #ifdef CONFIG_DEBUG_FS | ||
| 603 | static void max77663_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gpio) | ||
| 604 | { | ||
| 605 | struct max77663_chip *chip = max77663_chip_from_gpio(gpio); | ||
| 606 | int i; | ||
| 607 | |||
| 608 | for (i = 0; i < gpio->ngpio; i++) { | ||
| 609 | u8 ctrl_val; | ||
| 610 | const char *label; | ||
| 611 | int is_out; | ||
| 612 | int ret; | ||
| 613 | |||
| 614 | label = gpiochip_is_requested(gpio, i); | ||
| 615 | if (!label) | ||
| 616 | label = "Unrequested"; | ||
| 617 | |||
| 618 | seq_printf(s, " gpio-%-3d (%-20.20s) ", i + chip->gpio_base, | ||
| 619 | label); | ||
| 620 | |||
| 621 | if (chip->cache_gpio_alt & (1 << i)) { | ||
| 622 | seq_printf(s, "alt\n"); | ||
| 623 | continue; | ||
| 624 | } | ||
| 625 | |||
| 626 | ret = max77663_read(chip->dev, GPIO_REG_ADDR(i), &ctrl_val, 1, | ||
| 627 | 0); | ||
| 628 | if (ret < 0) { | ||
| 629 | seq_printf(s, "\n"); | ||
| 630 | continue; | ||
| 631 | } | ||
| 632 | |||
| 633 | is_out = ctrl_val & GPIO_CTRL_DIR_MASK ? 0 : 1; | ||
| 634 | seq_printf(s, "%s %s", (is_out ? "out" : "in"), (is_out ? | ||
| 635 | (ctrl_val & GPIO_CTRL_DOUT_MASK ? "hi" : "lo") | ||
| 636 | : (ctrl_val & GPIO_CTRL_DIN_MASK ? "hi" : "lo"))); | ||
| 637 | |||
| 638 | if (!is_out) { | ||
| 639 | int irq = gpio_to_irq(i + chip->gpio_base); | ||
| 640 | struct irq_desc *desc = irq_to_desc(irq); | ||
| 641 | u8 dbnc; | ||
| 642 | |||
| 643 | if (irq >= 0 && desc->action) { | ||
| 644 | u8 mask = GPIO_CTRL_REFE_IRQ_MASK; | ||
| 645 | u8 shift = GPIO_CTRL_REFE_IRQ_SHIFT; | ||
| 646 | char *trigger; | ||
| 647 | |||
| 648 | switch ((ctrl_val & mask) >> shift) { | ||
| 649 | case GPIO_REFE_IRQ_EDGE_FALLING: | ||
| 650 | trigger = "edge-falling"; | ||
| 651 | break; | ||
| 652 | case GPIO_REFE_IRQ_EDGE_RISING: | ||
| 653 | trigger = "edge-rising"; | ||
| 654 | break; | ||
| 655 | case GPIO_REFE_IRQ_EDGE_BOTH: | ||
| 656 | trigger = "edge-both"; | ||
| 657 | break; | ||
| 658 | default: | ||
| 659 | trigger = "masked"; | ||
| 660 | break; | ||
| 661 | } | ||
| 662 | |||
| 663 | seq_printf(s, " irq-%d %s", irq, trigger); | ||
| 664 | } | ||
| 665 | |||
| 666 | dbnc = (ctrl_val & GPIO_CTRL_DBNC_MASK) | ||
| 667 | >> GPIO_CTRL_DBNC_SHIFT; | ||
| 668 | seq_printf(s, " debounce-%s", | ||
| 669 | dbnc == GPIO_DBNC_8MS ? "8ms" : | ||
| 670 | dbnc == GPIO_DBNC_16MS ? "16ms" : | ||
| 671 | dbnc == GPIO_DBNC_32MS ? "32ms" : "none"); | ||
| 672 | } else { | ||
| 673 | seq_printf(s, " %s", | ||
| 674 | (ctrl_val & GPIO_CTRL_OUT_DRV_MASK ? | ||
| 675 | "output-drive" : "open-drain")); | ||
| 676 | } | ||
| 677 | |||
| 678 | seq_printf(s, "\n"); | ||
| 679 | } | ||
| 680 | } | ||
| 681 | #else | ||
| 682 | #define max77663_gpio_dbg_show NULL | ||
| 683 | #endif /* CONFIG_DEBUG_FS */ | ||
| 684 | |||
| 685 | static int max77663_gpio_set_config(struct max77663_chip *chip, | ||
| 686 | struct max77663_gpio_config *gpio_cfg) | ||
| 687 | { | ||
| 688 | int gpio = gpio_cfg->gpio; | ||
| 689 | u8 val = 0, mask = 0; | ||
| 690 | int ret = 0; | ||
| 691 | |||
| 692 | if ((gpio < MAX77663_GPIO0) || (MAX77663_GPIO7 < gpio)) | ||
| 693 | return -EINVAL; | ||
| 694 | |||
| 695 | if (gpio_cfg->pull_up != GPIO_PU_DEF) { | ||
| 696 | ret = max77663_gpio_set_pull_up(chip, gpio, gpio_cfg->pull_up); | ||
| 697 | if (ret < 0) { | ||
| 698 | dev_err(chip->dev, "gpio_set_config: " | ||
| 699 | "Failed to set gpio%d pull-up\n", gpio); | ||
| 700 | return ret; | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | if (gpio_cfg->pull_down != GPIO_PD_DEF) { | ||
| 705 | ret = max77663_gpio_set_pull_down(chip, gpio, | ||
| 706 | gpio_cfg->pull_down); | ||
| 707 | if (ret < 0) { | ||
| 708 | dev_err(chip->dev, "gpio_set_config: " | ||
| 709 | "Failed to set gpio%d pull-down\n", gpio); | ||
| 710 | return ret; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | if (gpio_cfg->dir != GPIO_DIR_DEF) { | ||
| 715 | mask = GPIO_CTRL_DIR_MASK; | ||
| 716 | if (gpio_cfg->dir == GPIO_DIR_IN) { | ||
| 717 | val |= GPIO_CTRL_DIR_MASK; | ||
| 718 | } else { | ||
| 719 | if (gpio_cfg->dout != GPIO_DOUT_DEF) { | ||
| 720 | mask |= GPIO_CTRL_DOUT_MASK; | ||
| 721 | if (gpio_cfg->dout == GPIO_DOUT_HIGH) | ||
| 722 | val |= GPIO_CTRL_DOUT_MASK; | ||
| 723 | } | ||
| 724 | |||
| 725 | if (gpio_cfg->out_drv != GPIO_OUT_DRV_DEF) { | ||
| 726 | mask |= GPIO_CTRL_OUT_DRV_MASK; | ||
| 727 | if (gpio_cfg->out_drv == GPIO_OUT_DRV_PUSH_PULL) | ||
| 728 | val |= GPIO_CTRL_OUT_DRV_MASK; | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | ret = max77663_cache_write(chip->dev, GPIO_REG_ADDR(gpio), mask, | ||
| 733 | val, &chip->cache_gpio_ctrl[gpio]); | ||
| 734 | if (ret < 0) { | ||
| 735 | dev_err(chip->dev, "gpio_set_config: " | ||
| 736 | "Failed to set gpio%d control\n", gpio); | ||
| 737 | return ret; | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | if (gpio_cfg->alternate != GPIO_ALT_DEF) { | ||
| 742 | ret = max77663_gpio_set_alternate(gpio + chip->gpio_base, | ||
| 743 | gpio_cfg->alternate); | ||
| 744 | if (ret < 0) { | ||
| 745 | dev_err(chip->dev, "gpio_set_config: " | ||
| 746 | "Failed to set gpio%d alternate\n", gpio); | ||
| 747 | return ret; | ||
| 748 | } | ||
| 749 | } | ||
| 750 | |||
| 751 | return 0; | ||
| 752 | } | ||
| 753 | |||
| 754 | static int max77663_gpio_init(struct max77663_chip *chip) | ||
| 755 | { | ||
| 756 | int i; | ||
| 757 | int ret; | ||
| 758 | |||
| 759 | chip->gpio.label = chip->i2c_power->name; | ||
| 760 | chip->gpio.dev = chip->dev; | ||
| 761 | chip->gpio.owner = THIS_MODULE; | ||
| 762 | chip->gpio.direction_input = max77663_gpio_dir_input; | ||
| 763 | chip->gpio.get = max77663_gpio_get; | ||
| 764 | chip->gpio.direction_output = max77663_gpio_dir_output; | ||
| 765 | chip->gpio.set_debounce = max77663_gpio_set_debounce; | ||
| 766 | chip->gpio.set = max77663_gpio_set; | ||
| 767 | chip->gpio.to_irq = max77663_gpio_to_irq; | ||
| 768 | chip->gpio.dbg_show = max77663_gpio_dbg_show; | ||
| 769 | chip->gpio.ngpio = MAX77663_GPIO_NR; | ||
| 770 | chip->gpio.can_sleep = 1; | ||
| 771 | if (chip->gpio_base) | ||
| 772 | chip->gpio.base = chip->gpio_base; | ||
| 773 | else | ||
| 774 | chip->gpio.base = -1; | ||
| 775 | |||
| 776 | ret = max77663_read(chip->dev, MAX77663_REG_GPIO_CTRL0, | ||
| 777 | chip->cache_gpio_ctrl, MAX77663_GPIO_NR, 0); | ||
| 778 | if (ret < 0) { | ||
| 779 | dev_err(chip->dev, "gpio_init: Failed to get gpio control\n"); | ||
| 780 | return ret; | ||
| 781 | } | ||
| 782 | |||
| 783 | ret = max77663_read(chip->dev, MAX77663_REG_GPIO_PU, | ||
| 784 | &chip->cache_gpio_pu, 1, 0); | ||
| 785 | if (ret < 0) { | ||
| 786 | dev_err(chip->dev, "gpio_init: Failed to get gpio pull-up\n"); | ||
| 787 | return ret; | ||
| 788 | } | ||
| 789 | |||
| 790 | ret = max77663_read(chip->dev, MAX77663_REG_GPIO_PD, | ||
| 791 | &chip->cache_gpio_pd, 1, 0); | ||
| 792 | if (ret < 0) { | ||
| 793 | dev_err(chip->dev, "gpio_init: Failed to get gpio pull-down\n"); | ||
| 794 | return ret; | ||
| 795 | } | ||
| 796 | |||
| 797 | ret = max77663_read(chip->dev, MAX77663_REG_GPIO_ALT, | ||
| 798 | &chip->cache_gpio_alt, 1, 0); | ||
| 799 | if (ret < 0) { | ||
| 800 | dev_err(chip->dev, "gpio_init: Failed to get gpio alternate\n"); | ||
| 801 | return ret; | ||
| 802 | } | ||
| 803 | |||
| 804 | ret = gpiochip_add(&chip->gpio); | ||
| 805 | if (ret < 0) { | ||
| 806 | dev_err(chip->dev, "gpio_init: Failed to add gpiochip\n"); | ||
| 807 | return ret; | ||
| 808 | } | ||
| 809 | chip->gpio_base = chip->gpio.base; | ||
| 810 | |||
| 811 | for (i = 0; i < chip->pdata->num_gpio_cfgs; i++) { | ||
| 812 | ret = max77663_gpio_set_config(chip, | ||
| 813 | &chip->pdata->gpio_cfgs[i]); | ||
| 814 | if (ret < 0) { | ||
| 815 | dev_err(chip->dev, | ||
| 816 | "gpio_init: Failed to set gpio config\n"); | ||
| 817 | return ret; | ||
| 818 | } | ||
| 819 | } | ||
| 820 | |||
| 821 | return 0; | ||
| 822 | } | ||
| 823 | |||
| 824 | static void max77663_gpio_exit(struct max77663_chip *chip) | ||
| 825 | { | ||
| 826 | if (gpiochip_remove(&chip->gpio) < 0) | ||
| 827 | dev_err(chip->dev, "gpio_exit: Failed to remove gpiochip\n"); | ||
| 828 | } | ||
| 829 | |||
| 830 | static void max77663_irq_mask(struct irq_data *data) | ||
| 831 | { | ||
| 832 | struct max77663_chip *chip = irq_data_get_irq_chip_data(data); | ||
| 833 | |||
| 834 | max77663_irqs[data->irq - chip->irq_base].is_unmask = 0; | ||
| 835 | } | ||
| 836 | |||
| 837 | static void max77663_irq_unmask(struct irq_data *data) | ||
| 838 | { | ||
| 839 | struct max77663_chip *chip = irq_data_get_irq_chip_data(data); | ||
| 840 | |||
| 841 | max77663_irqs[data->irq - chip->irq_base].is_unmask = 1; | ||
| 842 | } | ||
| 843 | |||
| 844 | static void max77663_irq_lock(struct irq_data *data) | ||
| 845 | { | ||
| 846 | struct max77663_chip *chip = irq_data_get_irq_chip_data(data); | ||
| 847 | |||
| 848 | mutex_lock(&chip->irq_lock); | ||
| 849 | } | ||
| 850 | |||
| 851 | static void max77663_irq_sync_unlock(struct irq_data *data) | ||
| 852 | { | ||
| 853 | struct max77663_chip *chip = irq_data_get_irq_chip_data(data); | ||
| 854 | struct max77663_irq_data *irq_data = | ||
| 855 | &max77663_irqs[data->irq - chip->irq_base]; | ||
| 856 | int idx = irq_data->cache_idx; | ||
| 857 | u8 irq_top_mask = chip->cache_irq_top_mask; | ||
| 858 | u16 irq_mask = chip->cache_irq_mask[idx]; | ||
| 859 | int update_irq_top = 0; | ||
| 860 | u32 len = 1; | ||
| 861 | int ret; | ||
| 862 | |||
| 863 | if (irq_data->is_unmask) { | ||
| 864 | if (chip->irq_top_count[irq_data->top_shift] == 0) | ||
| 865 | update_irq_top = 1; | ||
| 866 | chip->irq_top_count[irq_data->top_shift]++; | ||
| 867 | |||
| 868 | if (irq_data->top_mask != IRQ_TOP_GLBL_MASK) | ||
| 869 | irq_top_mask &= ~irq_data->top_mask; | ||
| 870 | |||
| 871 | if (idx != -1) | ||
| 872 | irq_mask &= ~irq_data->mask; | ||
| 873 | } else { | ||
| 874 | if (chip->irq_top_count[irq_data->top_shift] == 1) | ||
| 875 | update_irq_top = 1; | ||
| 876 | |||
| 877 | if (--chip->irq_top_count[irq_data->top_shift] < 0) | ||
| 878 | chip->irq_top_count[irq_data->top_shift] = 0; | ||
| 879 | |||
| 880 | if (irq_data->top_mask != IRQ_TOP_GLBL_MASK) | ||
| 881 | irq_top_mask |= irq_data->top_mask; | ||
| 882 | |||
| 883 | if (idx != -1) | ||
| 884 | irq_mask |= irq_data->mask; | ||
| 885 | } | ||
| 886 | |||
| 887 | if ((idx != -1) && (irq_mask != chip->cache_irq_mask[idx])) { | ||
| 888 | if (irq_data->top_mask == IRQ_TOP_LDO_MASK) | ||
| 889 | len = 2; | ||
| 890 | |||
| 891 | ret = max77663_write(chip->dev, irq_data->mask_reg, | ||
| 892 | &irq_mask, len, irq_data->is_rtc); | ||
| 893 | if (ret < 0) | ||
| 894 | goto out; | ||
| 895 | |||
| 896 | chip->cache_irq_mask[idx] = irq_mask; | ||
| 897 | } else if ((idx == -1) && (irq_data->top_mask == IRQ_TOP_GPIO_MASK)) { | ||
| 898 | unsigned offset = data->irq - chip->irq_base - IRQ_GPIO_BASE; | ||
| 899 | u8 shift = GPIO_CTRL_REFE_IRQ_SHIFT; | ||
| 900 | |||
| 901 | if (irq_data->is_unmask) { | ||
| 902 | if (irq_data->trigger_type) | ||
| 903 | irq_mask = irq_data->trigger_type; | ||
| 904 | else | ||
| 905 | irq_mask = GPIO_REFE_IRQ_EDGE_FALLING << shift; | ||
| 906 | } | ||
| 907 | |||
| 908 | ret = max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), | ||
| 909 | GPIO_CTRL_REFE_IRQ_MASK, irq_mask, | ||
| 910 | &chip->cache_gpio_ctrl[offset]); | ||
| 911 | if (ret < 0) | ||
| 912 | goto out; | ||
| 913 | |||
| 914 | if (irq_data->is_unmask) | ||
| 915 | irq_data->trigger_type = irq_mask; | ||
| 916 | } | ||
| 917 | |||
| 918 | if (update_irq_top && (irq_top_mask != chip->cache_irq_top_mask)) { | ||
| 919 | ret = max77663_cache_write(chip->dev, MAX77663_REG_IRQ_TOP_MASK, | ||
| 920 | irq_data->top_mask, irq_top_mask, | ||
| 921 | &chip->cache_irq_top_mask); | ||
| 922 | if (ret < 0) | ||
| 923 | goto out; | ||
| 924 | } | ||
| 925 | |||
| 926 | out: | ||
| 927 | mutex_unlock(&chip->irq_lock); | ||
| 928 | } | ||
| 929 | |||
| 930 | static int max77663_irq_gpio_set_type(struct irq_data *data, unsigned int type) | ||
| 931 | { | ||
| 932 | struct max77663_chip *chip = irq_data_get_irq_chip_data(data); | ||
| 933 | struct max77663_irq_data *irq_data = | ||
| 934 | &max77663_irqs[data->irq - chip->irq_base]; | ||
| 935 | unsigned offset = data->irq - chip->irq_base - IRQ_GPIO_BASE; | ||
| 936 | u8 shift = GPIO_CTRL_REFE_IRQ_SHIFT; | ||
| 937 | u8 val; | ||
| 938 | |||
| 939 | switch (type) { | ||
| 940 | case IRQ_TYPE_NONE: | ||
| 941 | case IRQ_TYPE_EDGE_FALLING: | ||
| 942 | val = (GPIO_REFE_IRQ_EDGE_FALLING << shift); | ||
| 943 | break; | ||
| 944 | case IRQ_TYPE_EDGE_RISING: | ||
| 945 | val = (GPIO_REFE_IRQ_EDGE_RISING << shift); | ||
| 946 | break; | ||
| 947 | case IRQ_TYPE_EDGE_BOTH: | ||
| 948 | val = (GPIO_REFE_IRQ_EDGE_BOTH << shift); | ||
| 949 | break; | ||
| 950 | default: | ||
| 951 | return -EINVAL; | ||
| 952 | } | ||
| 953 | |||
| 954 | irq_data->trigger_type = val; | ||
| 955 | if (!(chip->cache_gpio_ctrl[offset] & GPIO_CTRL_REFE_IRQ_MASK)) | ||
| 956 | return 0; | ||
| 957 | |||
| 958 | return max77663_cache_write(chip->dev, GPIO_REG_ADDR(offset), | ||
| 959 | GPIO_CTRL_REFE_IRQ_MASK, val, | ||
| 960 | &chip->cache_gpio_ctrl[offset]); | ||
| 961 | } | ||
| 962 | |||
| 963 | static inline int max77663_do_irq(struct max77663_chip *chip, u8 addr, | ||
| 964 | int irq_base, int irq_end) | ||
| 965 | { | ||
| 966 | struct max77663_irq_data *irq_data = NULL; | ||
| 967 | int irqs_to_handle[irq_end - irq_base + 1]; | ||
| 968 | int handled = 0; | ||
| 969 | u16 val; | ||
| 970 | u32 len = 1; | ||
| 971 | int i; | ||
| 972 | int ret; | ||
| 973 | |||
| 974 | ret = max77663_read(chip->dev, addr, &val, len, 0); | ||
| 975 | if (ret < 0) | ||
| 976 | return ret; | ||
| 977 | |||
| 978 | for (i = irq_base; i <= irq_end; i++) { | ||
| 979 | irq_data = &max77663_irqs[i]; | ||
| 980 | if (val & irq_data->mask) { | ||
| 981 | irqs_to_handle[handled] = i + chip->irq_base; | ||
| 982 | handled++; | ||
| 983 | } | ||
| 984 | } | ||
| 985 | |||
| 986 | for (i = 0; i < handled; i++) | ||
| 987 | handle_nested_irq(irqs_to_handle[i]); | ||
| 988 | |||
| 989 | return 0; | ||
| 990 | } | ||
| 991 | |||
| 992 | static irqreturn_t max77663_irq(int irq, void *data) | ||
| 993 | { | ||
| 994 | struct max77663_chip *chip = data; | ||
| 995 | u8 irq_top; | ||
| 996 | int ret; | ||
| 997 | |||
| 998 | ret = max77663_read(chip->dev, MAX77663_REG_IRQ_TOP, &irq_top, 1, 0); | ||
| 999 | if (ret < 0) { | ||
| 1000 | dev_err(chip->dev, "irq: Failed to get irq top status\n"); | ||
| 1001 | return IRQ_NONE; | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | if (irq_top & IRQ_TOP_GLBL_MASK) { | ||
| 1005 | ret = max77663_do_irq(chip, MAX77663_REG_LBT_IRQ, IRQ_LBT_BASE, | ||
| 1006 | IRQ_LBT_END); | ||
| 1007 | if (ret < 0) | ||
| 1008 | return IRQ_NONE; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | if (irq_top & IRQ_TOP_GPIO_MASK) { | ||
| 1012 | ret = max77663_do_irq(chip, MAX77663_REG_GPIO_IRQ, | ||
| 1013 | IRQ_GPIO_BASE, IRQ_GPIO_END); | ||
| 1014 | if (ret < 0) | ||
| 1015 | return IRQ_NONE; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | if (irq_top & IRQ_TOP_ONOFF_MASK) { | ||
| 1019 | ret = max77663_do_irq(chip, MAX77663_REG_ONOFF_IRQ, | ||
| 1020 | IRQ_ONOFF_BASE, IRQ_ONOFF_END); | ||
| 1021 | if (ret < 0) | ||
| 1022 | return IRQ_NONE; | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | if (irq_top & IRQ_TOP_RTC_MASK) | ||
| 1026 | handle_nested_irq(MAX77663_IRQ_RTC + chip->irq_base); | ||
| 1027 | |||
| 1028 | if (irq_top & IRQ_TOP_SD_MASK) | ||
| 1029 | handle_nested_irq(MAX77663_IRQ_SD_PF + chip->irq_base); | ||
| 1030 | |||
| 1031 | if (irq_top & IRQ_TOP_LDO_MASK) | ||
| 1032 | handle_nested_irq(MAX77663_IRQ_LDO_PF + chip->irq_base); | ||
| 1033 | |||
| 1034 | if (irq_top & IRQ_TOP_32K_MASK) | ||
| 1035 | handle_nested_irq(MAX77663_IRQ_32K + chip->irq_base); | ||
| 1036 | |||
| 1037 | if (irq_top & IRQ_TOP_NVER_MASK) | ||
| 1038 | handle_nested_irq(MAX77663_IRQ_NVER + chip->irq_base); | ||
| 1039 | |||
| 1040 | return IRQ_HANDLED; | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | static struct irq_chip max77663_irq_gpio_chip = { | ||
| 1044 | .name = "max77663-irq", | ||
| 1045 | .irq_mask = max77663_irq_mask, | ||
| 1046 | .irq_unmask = max77663_irq_unmask, | ||
| 1047 | .irq_set_type = max77663_irq_gpio_set_type, | ||
| 1048 | .irq_bus_lock = max77663_irq_lock, | ||
| 1049 | .irq_bus_sync_unlock = max77663_irq_sync_unlock, | ||
| 1050 | }; | ||
| 1051 | |||
| 1052 | static struct irq_chip max77663_irq_chip = { | ||
| 1053 | .name = "max77663-irq", | ||
| 1054 | .irq_mask = max77663_irq_mask, | ||
| 1055 | .irq_unmask = max77663_irq_unmask, | ||
| 1056 | .irq_bus_lock = max77663_irq_lock, | ||
| 1057 | .irq_bus_sync_unlock = max77663_irq_sync_unlock, | ||
| 1058 | }; | ||
| 1059 | |||
| 1060 | static int max77663_irq_init(struct max77663_chip *chip) | ||
| 1061 | { | ||
| 1062 | u32 temp; | ||
| 1063 | int i, ret = 0; | ||
| 1064 | |||
| 1065 | mutex_init(&chip->irq_lock); | ||
| 1066 | |||
| 1067 | /* Mask all interrupts */ | ||
| 1068 | chip->cache_irq_top_mask = 0xFF; | ||
| 1069 | chip->cache_irq_mask[CACHE_IRQ_LBT] = 0x0F; | ||
| 1070 | chip->cache_irq_mask[CACHE_IRQ_SD] = 0xFF; | ||
| 1071 | chip->cache_irq_mask[CACHE_IRQ_LDO] = 0xFFFF; | ||
| 1072 | chip->cache_irq_mask[CACHE_IRQ_ONOFF] = 0xFF; | ||
| 1073 | |||
| 1074 | max77663_write(chip->dev, MAX77663_REG_IRQ_TOP_MASK, | ||
| 1075 | &chip->cache_irq_top_mask, 1, 0); | ||
| 1076 | max77663_write(chip->dev, MAX77663_REG_LBT_IRQ_MASK, | ||
| 1077 | &chip->cache_irq_mask[CACHE_IRQ_LBT], 1, 0); | ||
| 1078 | max77663_write(chip->dev, MAX77663_REG_SD_IRQ_MASK, | ||
| 1079 | &chip->cache_irq_mask[CACHE_IRQ_SD], 1, 0); | ||
| 1080 | max77663_write(chip->dev, MAX77663_REG_LDOX_IRQ_MASK, | ||
| 1081 | &chip->cache_irq_mask[CACHE_IRQ_LDO], 2, 0); | ||
| 1082 | max77663_write(chip->dev, MAX77663_REG_ONOFF_IRQ_MASK, | ||
| 1083 | &chip->cache_irq_mask[CACHE_IRQ_ONOFF], 1, 0); | ||
| 1084 | |||
| 1085 | /* Clear all interrups */ | ||
| 1086 | max77663_read(chip->dev, MAX77663_REG_LBT_IRQ, &temp, 1, 0); | ||
| 1087 | max77663_read(chip->dev, MAX77663_REG_SD_IRQ, &temp, 1, 0); | ||
| 1088 | max77663_read(chip->dev, MAX77663_REG_LDOX_IRQ, &temp, 2, 0); | ||
| 1089 | max77663_read(chip->dev, MAX77663_REG_GPIO_IRQ, &temp, 1, 0); | ||
| 1090 | max77663_read(chip->dev, MAX77663_REG_ONOFF_IRQ, &temp, 1, 0); | ||
| 1091 | |||
| 1092 | for (i = chip->irq_base; i < (MAX77663_IRQ_NR + chip->irq_base); i++) { | ||
| 1093 | if (i >= NR_IRQS) { | ||
| 1094 | dev_err(chip->dev, | ||
| 1095 | "irq_init: Can't set irq chip for irq %d\n", i); | ||
| 1096 | continue; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | irq_set_chip_data(i, chip); | ||
| 1100 | |||
| 1101 | if ((IRQ_GPIO_BASE <= i - chip->irq_base) && | ||
| 1102 | (i - chip->irq_base <= IRQ_GPIO_END)) | ||
| 1103 | irq_set_chip_and_handler(i, &max77663_irq_gpio_chip, | ||
| 1104 | handle_edge_irq); | ||
| 1105 | else | ||
| 1106 | irq_set_chip_and_handler(i, &max77663_irq_chip, | ||
| 1107 | handle_edge_irq); | ||
| 1108 | #ifdef CONFIG_ARM | ||
| 1109 | set_irq_flags(i, IRQF_VALID); | ||
| 1110 | #else | ||
| 1111 | irq_set_noprobe(i); | ||
| 1112 | #endif | ||
| 1113 | irq_set_nested_thread(i, 1); | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | ret = request_threaded_irq(chip->i2c_power->irq, NULL, max77663_irq, | ||
| 1117 | IRQF_ONESHOT, "max77663", chip); | ||
| 1118 | if (ret) { | ||
| 1119 | dev_err(chip->dev, "irq_init: Failed to request irq %d\n", | ||
| 1120 | chip->i2c_power->irq); | ||
| 1121 | return ret; | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | device_init_wakeup(chip->dev, 1); | ||
| 1125 | enable_irq_wake(chip->i2c_power->irq); | ||
| 1126 | |||
| 1127 | chip->cache_irq_top_mask &= ~IRQ_TOP_GLBL_MASK; | ||
| 1128 | max77663_write(chip->dev, MAX77663_REG_IRQ_TOP_MASK, | ||
| 1129 | &chip->cache_irq_top_mask, 1, 0); | ||
| 1130 | |||
| 1131 | chip->cache_irq_mask[CACHE_IRQ_LBT] &= ~IRQ_GLBL_MASK; | ||
| 1132 | max77663_write(chip->dev, MAX77663_REG_LBT_IRQ_MASK, | ||
| 1133 | &chip->cache_irq_mask[CACHE_IRQ_LBT], 1, 0); | ||
| 1134 | |||
| 1135 | return 0; | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | static void max77663_irq_exit(struct max77663_chip *chip) | ||
| 1139 | { | ||
| 1140 | if (chip->i2c_power->irq) | ||
| 1141 | free_irq(chip->i2c_power->irq, chip); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | #ifdef CONFIG_DEBUG_FS | ||
| 1145 | static struct dentry *max77663_dentry_regs; | ||
| 1146 | |||
| 1147 | static int max77663_debugfs_dump_regs(struct max77663_chip *chip, char *label, | ||
| 1148 | u8 *addrs, int num_addrs, char *buf, | ||
| 1149 | ssize_t *len, int is_rtc) | ||
| 1150 | { | ||
| 1151 | ssize_t count = *len; | ||
| 1152 | u8 val; | ||
| 1153 | int ret = 0; | ||
| 1154 | int i; | ||
| 1155 | |||
| 1156 | count += sprintf(buf + count, "%s\n", label); | ||
| 1157 | if (count >= PAGE_SIZE - 1) | ||
| 1158 | return -ERANGE; | ||
| 1159 | |||
| 1160 | for (i = 0; i < num_addrs; i++) { | ||
| 1161 | count += sprintf(buf + count, "0x%02x: ", addrs[i]); | ||
| 1162 | if (count >= PAGE_SIZE - 1) | ||
| 1163 | return -ERANGE; | ||
| 1164 | |||
| 1165 | ret = max77663_read(chip->dev, addrs[i], &val, 1, is_rtc); | ||
| 1166 | if (ret == 0) | ||
| 1167 | count += sprintf(buf + count, "0x%02x\n", val); | ||
| 1168 | else | ||
| 1169 | count += sprintf(buf + count, "<read fail: %d>\n", ret); | ||
| 1170 | |||
| 1171 | if (count >= PAGE_SIZE - 1) | ||
| 1172 | return -ERANGE; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | *len = count; | ||
| 1176 | return 0; | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | static int max77663_debugfs_regs_open(struct inode *inode, struct file *file) | ||
| 1180 | { | ||
| 1181 | file->private_data = inode->i_private; | ||
| 1182 | return 0; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | static ssize_t max77663_debugfs_regs_read(struct file *file, | ||
| 1186 | char __user *user_buf, | ||
| 1187 | size_t count, loff_t *ppos) | ||
| 1188 | { | ||
| 1189 | struct max77663_chip *chip = file->private_data; | ||
| 1190 | char *buf; | ||
| 1191 | size_t len = 0; | ||
| 1192 | ssize_t ret; | ||
| 1193 | |||
| 1194 | /* Excluded interrupt status register to prevent register clear */ | ||
| 1195 | u8 global_regs[] = { 0x00, 0x01, 0x02, 0x05, 0x0D, 0x0E, 0x13 }; | ||
| 1196 | u8 sd_regs[] = { | ||
| 1197 | 0x07, 0x0F, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, | ||
| 1198 | 0x1E, 0x1F, 0x20, 0x21, 0x22 | ||
| 1199 | }; | ||
| 1200 | u8 ldo_regs[] = { | ||
| 1201 | 0x10, 0x11, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, | ||
| 1202 | 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, | ||
| 1203 | 0x35 | ||
| 1204 | }; | ||
| 1205 | u8 gpio_regs[] = { | ||
| 1206 | 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, | ||
| 1207 | 0x40 | ||
| 1208 | }; | ||
| 1209 | u8 rtc_regs[] = { | ||
| 1210 | 0x01, 0x02, 0x03, 0x04, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, | ||
| 1211 | 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, | ||
| 1212 | 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B | ||
| 1213 | }; | ||
| 1214 | u8 osc_32k_regs[] = { 0x03 }; | ||
| 1215 | u8 bbc_regs[] = { 0x04 }; | ||
| 1216 | u8 onoff_regs[] = { 0x12, 0x15, 0x41, 0x42 }; | ||
| 1217 | u8 fps_regs[] = { | ||
| 1218 | 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, | ||
| 1219 | 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, | ||
| 1220 | 0x57 | ||
| 1221 | }; | ||
| 1222 | u8 cid_regs[] = { 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D }; | ||
| 1223 | |||
| 1224 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | ||
| 1225 | if (!buf) | ||
| 1226 | return -ENOMEM; | ||
| 1227 | |||
| 1228 | len += sprintf(buf + len, "MAX77663 Registers\n"); | ||
| 1229 | max77663_debugfs_dump_regs(chip, "[Global]", global_regs, | ||
| 1230 | ARRAY_SIZE(global_regs), buf, &len, 0); | ||
| 1231 | max77663_debugfs_dump_regs(chip, "[Step-Down]", sd_regs, | ||
| 1232 | ARRAY_SIZE(sd_regs), buf, &len, 0); | ||
| 1233 | max77663_debugfs_dump_regs(chip, "[LDO]", ldo_regs, | ||
| 1234 | ARRAY_SIZE(ldo_regs), buf, &len, 0); | ||
| 1235 | max77663_debugfs_dump_regs(chip, "[GPIO]", gpio_regs, | ||
| 1236 | ARRAY_SIZE(gpio_regs), buf, &len, 0); | ||
| 1237 | max77663_debugfs_dump_regs(chip, "[RTC]", rtc_regs, | ||
| 1238 | ARRAY_SIZE(rtc_regs), buf, &len, 1); | ||
| 1239 | max77663_debugfs_dump_regs(chip, "[32kHz Oscillator]", osc_32k_regs, | ||
| 1240 | ARRAY_SIZE(osc_32k_regs), buf, &len, 0); | ||
| 1241 | max77663_debugfs_dump_regs(chip, "[Backup Battery Charger]", bbc_regs, | ||
| 1242 | ARRAY_SIZE(bbc_regs), buf, &len, 0); | ||
| 1243 | max77663_debugfs_dump_regs(chip, "[On/OFF Controller]", onoff_regs, | ||
| 1244 | ARRAY_SIZE(onoff_regs), buf, &len, 0); | ||
| 1245 | max77663_debugfs_dump_regs(chip, "[Flexible Power Sequencer]", fps_regs, | ||
| 1246 | ARRAY_SIZE(fps_regs), buf, &len, 0); | ||
| 1247 | max77663_debugfs_dump_regs(chip, "[Chip Identification]", cid_regs, | ||
| 1248 | ARRAY_SIZE(cid_regs), buf, &len, 0); | ||
| 1249 | |||
| 1250 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); | ||
| 1251 | kfree(buf); | ||
| 1252 | |||
| 1253 | return ret; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | static const struct file_operations max77663_debugfs_regs_fops = { | ||
| 1257 | .open = max77663_debugfs_regs_open, | ||
| 1258 | .read = max77663_debugfs_regs_read, | ||
| 1259 | }; | ||
| 1260 | |||
| 1261 | static void max77663_debugfs_init(struct max77663_chip *chip) | ||
| 1262 | { | ||
| 1263 | max77663_dentry_regs = debugfs_create_file(chip->i2c_power->name, | ||
| 1264 | 0444, 0, chip, | ||
| 1265 | &max77663_debugfs_regs_fops); | ||
| 1266 | if (!max77663_dentry_regs) | ||
| 1267 | dev_warn(chip->dev, | ||
| 1268 | "debugfs_init: Failed to create debugfs file\n"); | ||
| 1269 | } | ||
| 1270 | |||
| 1271 | static void max77663_debugfs_exit(struct max77663_chip *chip) | ||
| 1272 | { | ||
| 1273 | debugfs_remove(max77663_dentry_regs); | ||
| 1274 | } | ||
| 1275 | #else | ||
| 1276 | static inline void max77663_debugfs_init(struct max77663_chip *chip) | ||
| 1277 | { | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | static inline void max77663_debugfs_exit(struct max77663_chip *chip) | ||
| 1281 | { | ||
| 1282 | } | ||
| 1283 | #endif /* CONFIG_DEBUG_FS */ | ||
| 1284 | |||
| 1285 | static int max77663_probe(struct i2c_client *client, | ||
| 1286 | const struct i2c_device_id *id) | ||
| 1287 | { | ||
| 1288 | struct max77663_platform_data *pdata = client->dev.platform_data; | ||
| 1289 | struct max77663_chip *chip; | ||
| 1290 | int ret = 0; | ||
| 1291 | |||
| 1292 | if (pdata == NULL) { | ||
| 1293 | dev_err(&client->dev, "probe: Invalid platform_data\n"); | ||
| 1294 | ret = -ENODEV; | ||
| 1295 | goto out; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | chip = kzalloc(sizeof(struct max77663_chip), GFP_KERNEL); | ||
| 1299 | if (chip == NULL) { | ||
| 1300 | dev_err(&client->dev, "probe: kzalloc() failed\n"); | ||
| 1301 | ret = -ENOMEM; | ||
| 1302 | goto out; | ||
| 1303 | } | ||
| 1304 | max77663_chip = chip; | ||
| 1305 | |||
| 1306 | chip->i2c_power = client; | ||
| 1307 | i2c_set_clientdata(client, chip); | ||
| 1308 | |||
| 1309 | if (pdata->rtc_i2c_addr) | ||
| 1310 | chip->rtc_i2c_addr = pdata->rtc_i2c_addr; | ||
| 1311 | else | ||
| 1312 | chip->rtc_i2c_addr = MAX77663_RTC_I2C_ADDR; | ||
| 1313 | |||
| 1314 | chip->i2c_rtc = i2c_new_dummy(client->adapter, chip->rtc_i2c_addr); | ||
| 1315 | i2c_set_clientdata(chip->i2c_rtc, chip); | ||
| 1316 | |||
| 1317 | chip->dev = &client->dev; | ||
| 1318 | chip->pdata = pdata; | ||
| 1319 | chip->irq_base = pdata->irq_base; | ||
| 1320 | chip->gpio_base = pdata->gpio_base; | ||
| 1321 | mutex_init(&chip->io_lock); | ||
| 1322 | |||
| 1323 | max77663_gpio_init(chip); | ||
| 1324 | max77663_irq_init(chip); | ||
| 1325 | max77663_debugfs_init(chip); | ||
| 1326 | ret = max77663_sleep(chip, false); | ||
| 1327 | if (ret < 0) { | ||
| 1328 | dev_err(&client->dev, "probe: Failed to disable sleep\n"); | ||
| 1329 | goto out_exit; | ||
| 1330 | } | ||
| 1331 | |||
| 1332 | if (pdata->use_power_off && !pm_power_off) | ||
| 1333 | pm_power_off = max77663_power_off; | ||
| 1334 | |||
| 1335 | ret = mfd_add_devices(&client->dev, 0, pdata->sub_devices, | ||
| 1336 | pdata->num_subdevs, NULL, 0); | ||
| 1337 | if (ret != 0) { | ||
| 1338 | dev_err(&client->dev, "probe: Failed to add subdev: %d\n", ret); | ||
| 1339 | goto out_exit; | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | return 0; | ||
| 1343 | |||
| 1344 | out_exit: | ||
| 1345 | max77663_debugfs_exit(chip); | ||
| 1346 | max77663_gpio_exit(chip); | ||
| 1347 | max77663_irq_exit(chip); | ||
| 1348 | mutex_destroy(&chip->io_lock); | ||
| 1349 | max77663_chip = NULL; | ||
| 1350 | kfree(chip); | ||
| 1351 | out: | ||
| 1352 | return ret; | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | static int __devexit max77663_remove(struct i2c_client *client) | ||
| 1356 | { | ||
| 1357 | struct max77663_chip *chip = i2c_get_clientdata(client); | ||
| 1358 | |||
| 1359 | mfd_remove_devices(chip->dev); | ||
| 1360 | max77663_debugfs_exit(chip); | ||
| 1361 | max77663_irq_exit(chip); | ||
| 1362 | max77663_gpio_exit(chip); | ||
| 1363 | mutex_destroy(&chip->io_lock); | ||
| 1364 | max77663_chip = NULL; | ||
| 1365 | kfree(chip); | ||
| 1366 | |||
| 1367 | return 0; | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | #ifdef CONFIG_PM | ||
| 1371 | static int max77663_suspend(struct device *dev) | ||
| 1372 | { | ||
| 1373 | struct i2c_client *client = to_i2c_client(dev); | ||
| 1374 | struct max77663_chip *chip = i2c_get_clientdata(client); | ||
| 1375 | int ret; | ||
| 1376 | |||
| 1377 | if (client->irq) | ||
| 1378 | disable_irq(client->irq); | ||
| 1379 | |||
| 1380 | ret = max77663_sleep(chip, true); | ||
| 1381 | if (ret < 0) | ||
| 1382 | dev_err(dev, "suspend: Failed to enable sleep\n"); | ||
| 1383 | |||
| 1384 | return ret; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | static int max77663_resume(struct device *dev) | ||
| 1388 | { | ||
| 1389 | struct i2c_client *client = to_i2c_client(dev); | ||
| 1390 | struct max77663_chip *chip = i2c_get_clientdata(client); | ||
| 1391 | int ret; | ||
| 1392 | |||
| 1393 | ret = max77663_sleep(chip, false); | ||
| 1394 | if (ret < 0) { | ||
| 1395 | dev_err(dev, "resume: Failed to disable sleep\n"); | ||
| 1396 | return ret; | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | if (client->irq) | ||
| 1400 | enable_irq(client->irq); | ||
| 1401 | |||
| 1402 | return 0; | ||
| 1403 | } | ||
| 1404 | #else | ||
| 1405 | #define max77663_suspend NULL | ||
| 1406 | #define max77663_resume NULL | ||
| 1407 | #endif /* CONFIG_PM */ | ||
| 1408 | |||
| 1409 | static const struct i2c_device_id max77663_id[] = { | ||
| 1410 | {"max77663", 0}, | ||
| 1411 | {}, | ||
| 1412 | }; | ||
| 1413 | MODULE_DEVICE_TABLE(i2c, max77663_id); | ||
| 1414 | |||
| 1415 | static const struct dev_pm_ops max77663_pm = { | ||
| 1416 | .suspend = max77663_suspend, | ||
| 1417 | .resume = max77663_resume, | ||
| 1418 | }; | ||
| 1419 | |||
| 1420 | static struct i2c_driver max77663_driver = { | ||
| 1421 | .driver = { | ||
| 1422 | .name = "max77663", | ||
| 1423 | .owner = THIS_MODULE, | ||
| 1424 | .pm = &max77663_pm, | ||
| 1425 | }, | ||
| 1426 | .probe = max77663_probe, | ||
| 1427 | .remove = __devexit_p(max77663_remove), | ||
| 1428 | .id_table = max77663_id, | ||
| 1429 | }; | ||
| 1430 | |||
| 1431 | static int __init max77663_init(void) | ||
| 1432 | { | ||
| 1433 | return i2c_add_driver(&max77663_driver); | ||
| 1434 | } | ||
| 1435 | subsys_initcall(max77663_init); | ||
| 1436 | |||
| 1437 | static void __exit max77663_exit(void) | ||
| 1438 | { | ||
| 1439 | i2c_del_driver(&max77663_driver); | ||
| 1440 | } | ||
| 1441 | module_exit(max77663_exit); | ||
| 1442 | |||
| 1443 | MODULE_LICENSE("GPL v2"); | ||
| 1444 | MODULE_DESCRIPTION("MAX77663 Multi Function Device Core Driver"); | ||
| 1445 | MODULE_VERSION("1.0"); | ||
diff --git a/drivers/mfd/max8907c-irq.c b/drivers/mfd/max8907c-irq.c new file mode 100644 index 00000000000..8d6e9600e7f --- /dev/null +++ b/drivers/mfd/max8907c-irq.c | |||
| @@ -0,0 +1,425 @@ | |||
| 1 | /* | ||
| 2 | * Battery driver for Maxim MAX8907C | ||
| 3 | * | ||
| 4 | * Copyright (c) 2011, NVIDIA Corporation. | ||
| 5 | * Based on driver/mfd/max8925-core.c, Copyright (C) 2009-2010 Marvell International Ltd. | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/init.h> | ||
| 14 | #include <linux/slab.h> | ||
| 15 | #include <linux/i2c.h> | ||
| 16 | #include <linux/irq.h> | ||
| 17 | #include <linux/interrupt.h> | ||
| 18 | #include <linux/mfd/core.h> | ||
| 19 | #include <linux/mfd/max8907c.h> | ||
| 20 | |||
| 21 | struct max8907c_irq_data { | ||
| 22 | int reg; | ||
| 23 | int mask_reg; | ||
| 24 | int enable; /* enable or not */ | ||
| 25 | int offs; /* bit offset in mask register */ | ||
| 26 | bool is_rtc; | ||
| 27 | int wake; | ||
| 28 | }; | ||
| 29 | |||
| 30 | static struct max8907c_irq_data max8907c_irqs[] = { | ||
| 31 | [MAX8907C_IRQ_VCHG_DC_OVP] = { | ||
| 32 | .reg = MAX8907C_REG_CHG_IRQ1, | ||
| 33 | .mask_reg = MAX8907C_REG_CHG_IRQ1_MASK, | ||
| 34 | .offs = 1 << 0, | ||
| 35 | }, | ||
| 36 | [MAX8907C_IRQ_VCHG_DC_F] = { | ||
| 37 | .reg = MAX8907C_REG_CHG_IRQ1, | ||
| 38 | .mask_reg = MAX8907C_REG_CHG_IRQ1_MASK, | ||
| 39 | .offs = 1 << 1, | ||
| 40 | }, | ||
| 41 | [MAX8907C_IRQ_VCHG_DC_R] = { | ||
| 42 | .reg = MAX8907C_REG_CHG_IRQ1, | ||
| 43 | .mask_reg = MAX8907C_REG_CHG_IRQ1_MASK, | ||
| 44 | .offs = 1 << 2, | ||
| 45 | }, | ||
| 46 | [MAX8907C_IRQ_VCHG_THM_OK_R] = { | ||
| 47 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 48 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 49 | .offs = 1 << 0, | ||
| 50 | }, | ||
| 51 | [MAX8907C_IRQ_VCHG_THM_OK_F] = { | ||
| 52 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 53 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 54 | .offs = 1 << 1, | ||
| 55 | }, | ||
| 56 | [MAX8907C_IRQ_VCHG_MBATTLOW_F] = { | ||
| 57 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 58 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 59 | .offs = 1 << 2, | ||
| 60 | }, | ||
| 61 | [MAX8907C_IRQ_VCHG_MBATTLOW_R] = { | ||
| 62 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 63 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 64 | .offs = 1 << 3, | ||
| 65 | }, | ||
| 66 | [MAX8907C_IRQ_VCHG_RST] = { | ||
| 67 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 68 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 69 | .offs = 1 << 4, | ||
| 70 | }, | ||
| 71 | [MAX8907C_IRQ_VCHG_DONE] = { | ||
| 72 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 73 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 74 | .offs = 1 << 5, | ||
| 75 | }, | ||
| 76 | [MAX8907C_IRQ_VCHG_TOPOFF] = { | ||
| 77 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 78 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 79 | .offs = 1 << 6, | ||
| 80 | }, | ||
| 81 | [MAX8907C_IRQ_VCHG_TMR_FAULT] = { | ||
| 82 | .reg = MAX8907C_REG_CHG_IRQ2, | ||
| 83 | .mask_reg = MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 84 | .offs = 1 << 7, | ||
| 85 | }, | ||
| 86 | [MAX8907C_IRQ_GPM_RSTIN] = { | ||
| 87 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 88 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 89 | .offs = 1 << 0, | ||
| 90 | }, | ||
| 91 | [MAX8907C_IRQ_GPM_MPL] = { | ||
| 92 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 93 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 94 | .offs = 1 << 1, | ||
| 95 | }, | ||
| 96 | [MAX8907C_IRQ_GPM_SW_3SEC] = { | ||
| 97 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 98 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 99 | .offs = 1 << 2, | ||
| 100 | }, | ||
| 101 | [MAX8907C_IRQ_GPM_EXTON_F] = { | ||
| 102 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 103 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 104 | .offs = 1 << 3, | ||
| 105 | }, | ||
| 106 | [MAX8907C_IRQ_GPM_EXTON_R] = { | ||
| 107 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 108 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 109 | .offs = 1 << 4, | ||
| 110 | }, | ||
| 111 | [MAX8907C_IRQ_GPM_SW_1SEC] = { | ||
| 112 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 113 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 114 | .offs = 1 << 5, | ||
| 115 | }, | ||
| 116 | [MAX8907C_IRQ_GPM_SW_F] = { | ||
| 117 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 118 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 119 | .offs = 1 << 6, | ||
| 120 | }, | ||
| 121 | [MAX8907C_IRQ_GPM_SW_R] = { | ||
| 122 | .reg = MAX8907C_REG_ON_OFF_IRQ1, | ||
| 123 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 124 | .offs = 1 << 7, | ||
| 125 | }, | ||
| 126 | [MAX8907C_IRQ_GPM_SYSCKEN_F] = { | ||
| 127 | .reg = MAX8907C_REG_ON_OFF_IRQ2, | ||
| 128 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ2_MASK, | ||
| 129 | .offs = 1 << 0, | ||
| 130 | }, | ||
| 131 | [MAX8907C_IRQ_GPM_SYSCKEN_R] = { | ||
| 132 | .reg = MAX8907C_REG_ON_OFF_IRQ2, | ||
| 133 | .mask_reg = MAX8907C_REG_ON_OFF_IRQ2_MASK, | ||
| 134 | .offs = 1 << 1, | ||
| 135 | }, | ||
| 136 | [MAX8907C_IRQ_RTC_ALARM1] = { | ||
| 137 | .reg = MAX8907C_REG_RTC_IRQ, | ||
| 138 | .mask_reg = MAX8907C_REG_RTC_IRQ_MASK, | ||
| 139 | .offs = 1 << 2, | ||
| 140 | .is_rtc = true, | ||
| 141 | }, | ||
| 142 | [MAX8907C_IRQ_RTC_ALARM0] = { | ||
| 143 | .reg = MAX8907C_REG_RTC_IRQ, | ||
| 144 | .mask_reg = MAX8907C_REG_RTC_IRQ_MASK, | ||
| 145 | .offs = 1 << 3, | ||
| 146 | .is_rtc = true, | ||
| 147 | }, | ||
| 148 | }; | ||
| 149 | |||
| 150 | static inline struct max8907c_irq_data *irq_to_max8907c(struct max8907c *chip, | ||
| 151 | int irq) | ||
| 152 | { | ||
| 153 | return &max8907c_irqs[irq - chip->irq_base]; | ||
| 154 | } | ||
| 155 | |||
| 156 | static irqreturn_t max8907c_irq(int irq, void *data) | ||
| 157 | { | ||
| 158 | struct max8907c *chip = data; | ||
| 159 | struct max8907c_irq_data *irq_data; | ||
| 160 | struct i2c_client *i2c; | ||
| 161 | int read_reg = -1, value = 0; | ||
| 162 | int i; | ||
| 163 | |||
| 164 | for (i = 0; i < ARRAY_SIZE(max8907c_irqs); i++) { | ||
| 165 | irq_data = &max8907c_irqs[i]; | ||
| 166 | |||
| 167 | if (irq_data->is_rtc) | ||
| 168 | i2c = chip->i2c_rtc; | ||
| 169 | else | ||
| 170 | i2c = chip->i2c_power; | ||
| 171 | |||
| 172 | if (read_reg != irq_data->reg) { | ||
| 173 | read_reg = irq_data->reg; | ||
| 174 | value = max8907c_reg_read(i2c, irq_data->reg); | ||
| 175 | } | ||
| 176 | |||
| 177 | if (value & irq_data->enable) | ||
| 178 | handle_nested_irq(chip->irq_base + i); | ||
| 179 | } | ||
| 180 | return IRQ_HANDLED; | ||
| 181 | } | ||
| 182 | |||
| 183 | static void max8907c_irq_lock(struct irq_data *data) | ||
| 184 | { | ||
| 185 | struct max8907c *chip = irq_data_get_irq_chip_data(data); | ||
| 186 | |||
| 187 | mutex_lock(&chip->irq_lock); | ||
| 188 | } | ||
| 189 | |||
| 190 | static void max8907c_irq_sync_unlock(struct irq_data *data) | ||
| 191 | { | ||
| 192 | struct max8907c *chip = irq_data_get_irq_chip_data(data); | ||
| 193 | struct max8907c_irq_data *irq_data; | ||
| 194 | unsigned char irq_chg[2], irq_on[2]; | ||
| 195 | unsigned char irq_rtc; | ||
| 196 | int i; | ||
| 197 | |||
| 198 | irq_chg[0] = irq_chg[1] = irq_on[0] = irq_on[1] = irq_rtc = 0xFF; | ||
| 199 | |||
| 200 | for (i = 0; i < ARRAY_SIZE(max8907c_irqs); i++) { | ||
| 201 | irq_data = &max8907c_irqs[i]; | ||
| 202 | /* 1 -- disable, 0 -- enable */ | ||
| 203 | switch (irq_data->mask_reg) { | ||
| 204 | case MAX8907C_REG_CHG_IRQ1_MASK: | ||
| 205 | irq_chg[0] &= ~irq_data->enable; | ||
| 206 | break; | ||
| 207 | case MAX8907C_REG_CHG_IRQ2_MASK: | ||
| 208 | irq_chg[1] &= ~irq_data->enable; | ||
| 209 | break; | ||
| 210 | case MAX8907C_REG_ON_OFF_IRQ1_MASK: | ||
| 211 | irq_on[0] &= ~irq_data->enable; | ||
| 212 | break; | ||
| 213 | case MAX8907C_REG_ON_OFF_IRQ2_MASK: | ||
| 214 | irq_on[1] &= ~irq_data->enable; | ||
| 215 | break; | ||
| 216 | case MAX8907C_REG_RTC_IRQ_MASK: | ||
| 217 | irq_rtc &= ~irq_data->enable; | ||
| 218 | break; | ||
| 219 | default: | ||
| 220 | dev_err(chip->dev, "wrong IRQ\n"); | ||
| 221 | break; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | /* update mask into registers */ | ||
| 225 | if (chip->cache_chg[0] != irq_chg[0]) { | ||
| 226 | chip->cache_chg[0] = irq_chg[0]; | ||
| 227 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ1_MASK, | ||
| 228 | irq_chg[0]); | ||
| 229 | } | ||
| 230 | if (chip->cache_chg[1] != irq_chg[1]) { | ||
| 231 | chip->cache_chg[1] = irq_chg[1]; | ||
| 232 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ2_MASK, | ||
| 233 | irq_chg[1]); | ||
| 234 | } | ||
| 235 | if (chip->cache_on[0] != irq_on[0]) { | ||
| 236 | chip->cache_on[0] = irq_on[0]; | ||
| 237 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ1_MASK, | ||
| 238 | irq_on[0]); | ||
| 239 | } | ||
| 240 | if (chip->cache_on[1] != irq_on[1]) { | ||
| 241 | chip->cache_on[1] = irq_on[1]; | ||
| 242 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ2_MASK, | ||
| 243 | irq_on[1]); | ||
| 244 | } | ||
| 245 | if (chip->cache_rtc != irq_rtc) { | ||
| 246 | chip->cache_rtc = irq_rtc; | ||
| 247 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_RTC_IRQ_MASK, | ||
| 248 | irq_rtc); | ||
| 249 | } | ||
| 250 | |||
| 251 | mutex_unlock(&chip->irq_lock); | ||
| 252 | } | ||
| 253 | |||
| 254 | static void max8907c_irq_enable(struct irq_data *data) | ||
| 255 | { | ||
| 256 | struct max8907c *chip = irq_data_get_irq_chip_data(data); | ||
| 257 | max8907c_irqs[data->irq - chip->irq_base].enable | ||
| 258 | = max8907c_irqs[data->irq - chip->irq_base].offs; | ||
| 259 | } | ||
| 260 | |||
| 261 | static void max8907c_irq_disable(struct irq_data *data) | ||
| 262 | { | ||
| 263 | struct max8907c *chip = irq_data_get_irq_chip_data(data); | ||
| 264 | max8907c_irqs[data->irq - chip->irq_base].enable = 0; | ||
| 265 | } | ||
| 266 | |||
| 267 | static int max8907c_irq_set_wake(struct irq_data *data, unsigned int on) | ||
| 268 | { | ||
| 269 | struct max8907c *chip = irq_data_get_irq_chip_data(data); | ||
| 270 | if (on) { | ||
| 271 | max8907c_irqs[data->irq - chip->irq_base].wake | ||
| 272 | = max8907c_irqs[data->irq - chip->irq_base].enable; | ||
| 273 | } else { | ||
| 274 | max8907c_irqs[data->irq - chip->irq_base].wake = 0; | ||
| 275 | } | ||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | static struct irq_chip max8907c_irq_chip = { | ||
| 280 | .name = "max8907c", | ||
| 281 | .irq_bus_lock = max8907c_irq_lock, | ||
| 282 | .irq_bus_sync_unlock = max8907c_irq_sync_unlock, | ||
| 283 | .irq_enable = max8907c_irq_enable, | ||
| 284 | .irq_disable = max8907c_irq_disable, | ||
| 285 | .irq_set_wake = max8907c_irq_set_wake, | ||
| 286 | }; | ||
| 287 | |||
| 288 | int max8907c_irq_init(struct max8907c *chip, int irq, int irq_base) | ||
| 289 | { | ||
| 290 | unsigned long flags = IRQF_ONESHOT; | ||
| 291 | struct irq_desc *desc; | ||
| 292 | int i, ret; | ||
| 293 | int __irq; | ||
| 294 | |||
| 295 | if (!irq_base || !irq) { | ||
| 296 | dev_warn(chip->dev, "No interrupt support\n"); | ||
| 297 | return -EINVAL; | ||
| 298 | } | ||
| 299 | /* clear all interrupts */ | ||
| 300 | max8907c_reg_read(chip->i2c_power, MAX8907C_REG_CHG_IRQ1); | ||
| 301 | max8907c_reg_read(chip->i2c_power, MAX8907C_REG_CHG_IRQ2); | ||
| 302 | max8907c_reg_read(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ1); | ||
| 303 | max8907c_reg_read(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ2); | ||
| 304 | max8907c_reg_read(chip->i2c_rtc, MAX8907C_REG_RTC_IRQ); | ||
| 305 | /* mask all interrupts */ | ||
| 306 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_ALARM0_CNTL, 0); | ||
| 307 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_ALARM1_CNTL, 0); | ||
| 308 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ1_MASK, 0xff); | ||
| 309 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ2_MASK, 0xff); | ||
| 310 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ1_MASK, 0xff); | ||
| 311 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ2_MASK, 0xff); | ||
| 312 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_RTC_IRQ_MASK, 0xff); | ||
| 313 | |||
| 314 | chip->cache_chg[0] = chip->cache_chg[1] = | ||
| 315 | chip->cache_on[0] = chip->cache_on[1] = | ||
| 316 | chip->cache_rtc = 0xFF; | ||
| 317 | |||
| 318 | mutex_init(&chip->irq_lock); | ||
| 319 | chip->core_irq = irq; | ||
| 320 | chip->irq_base = irq_base; | ||
| 321 | desc = irq_to_desc(chip->core_irq); | ||
| 322 | |||
| 323 | /* register with genirq */ | ||
| 324 | for (i = 0; i < ARRAY_SIZE(max8907c_irqs); i++) { | ||
| 325 | __irq = i + chip->irq_base; | ||
| 326 | irq_set_chip_data(__irq, chip); | ||
| 327 | irq_set_chip_and_handler(__irq, &max8907c_irq_chip, | ||
| 328 | handle_edge_irq); | ||
| 329 | irq_set_nested_thread(__irq, 1); | ||
| 330 | #ifdef CONFIG_ARM | ||
| 331 | /* ARM requires an extra step to clear IRQ_NOREQUEST, which it | ||
| 332 | * sets on behalf of every irq_chip. | ||
| 333 | */ | ||
| 334 | set_irq_flags(__irq, IRQF_VALID); | ||
| 335 | #else | ||
| 336 | irq_set_noprobe(__irq); | ||
| 337 | #endif | ||
| 338 | } | ||
| 339 | |||
| 340 | ret = request_threaded_irq(irq, NULL, max8907c_irq, flags, | ||
| 341 | "max8907c", chip); | ||
| 342 | if (ret) { | ||
| 343 | dev_err(chip->dev, "Failed to request core IRQ: %d\n", ret); | ||
| 344 | chip->core_irq = 0; | ||
| 345 | } | ||
| 346 | |||
| 347 | device_init_wakeup(chip->dev, 1); | ||
| 348 | |||
| 349 | return ret; | ||
| 350 | } | ||
| 351 | |||
| 352 | int max8907c_suspend(struct i2c_client *i2c, pm_message_t state) | ||
| 353 | { | ||
| 354 | struct max8907c *chip = i2c_get_clientdata(i2c); | ||
| 355 | |||
| 356 | struct max8907c_irq_data *irq_data; | ||
| 357 | unsigned char irq_chg[2], irq_on[2]; | ||
| 358 | unsigned char irq_rtc; | ||
| 359 | int i; | ||
| 360 | |||
| 361 | irq_chg[0] = irq_chg[1] = irq_on[0] = irq_on[1] = irq_rtc = 0xFF; | ||
| 362 | |||
| 363 | for (i = 0; i < ARRAY_SIZE(max8907c_irqs); i++) { | ||
| 364 | irq_data = &max8907c_irqs[i]; | ||
| 365 | /* 1 -- disable, 0 -- enable */ | ||
| 366 | switch (irq_data->mask_reg) { | ||
| 367 | case MAX8907C_REG_CHG_IRQ1_MASK: | ||
| 368 | irq_chg[0] &= ~irq_data->wake; | ||
| 369 | break; | ||
| 370 | case MAX8907C_REG_CHG_IRQ2_MASK: | ||
| 371 | irq_chg[1] &= ~irq_data->wake; | ||
| 372 | break; | ||
| 373 | case MAX8907C_REG_ON_OFF_IRQ1_MASK: | ||
| 374 | irq_on[0] &= ~irq_data->wake; | ||
| 375 | break; | ||
| 376 | case MAX8907C_REG_ON_OFF_IRQ2_MASK: | ||
| 377 | irq_on[1] &= ~irq_data->wake; | ||
| 378 | break; | ||
| 379 | case MAX8907C_REG_RTC_IRQ_MASK: | ||
| 380 | irq_rtc &= ~irq_data->wake; | ||
| 381 | break; | ||
| 382 | default: | ||
| 383 | dev_err(chip->dev, "wrong IRQ\n"); | ||
| 384 | break; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ1_MASK, irq_chg[0]); | ||
| 389 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ2_MASK, irq_chg[1]); | ||
| 390 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ1_MASK, irq_on[0]); | ||
| 391 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ2_MASK, irq_on[1]); | ||
| 392 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_RTC_IRQ_MASK, irq_rtc); | ||
| 393 | |||
| 394 | if (device_may_wakeup(chip->dev)) | ||
| 395 | enable_irq_wake(chip->core_irq); | ||
| 396 | else | ||
| 397 | disable_irq(chip->core_irq); | ||
| 398 | |||
| 399 | return 0; | ||
| 400 | } | ||
| 401 | |||
| 402 | int max8907c_resume(struct i2c_client *i2c) | ||
| 403 | { | ||
| 404 | struct max8907c *chip = i2c_get_clientdata(i2c); | ||
| 405 | |||
| 406 | if (device_may_wakeup(chip->dev)) | ||
| 407 | disable_irq_wake(chip->core_irq); | ||
| 408 | else | ||
| 409 | enable_irq(chip->core_irq); | ||
| 410 | |||
| 411 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ1_MASK, chip->cache_chg[0]); | ||
| 412 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_CHG_IRQ2_MASK, chip->cache_chg[1]); | ||
| 413 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ1_MASK, chip->cache_on[0]); | ||
| 414 | max8907c_reg_write(chip->i2c_power, MAX8907C_REG_ON_OFF_IRQ2_MASK, chip->cache_on[1]); | ||
| 415 | max8907c_reg_write(chip->i2c_rtc, MAX8907C_REG_RTC_IRQ_MASK, chip->cache_rtc); | ||
| 416 | |||
| 417 | return 0; | ||
| 418 | } | ||
| 419 | |||
| 420 | void max8907c_irq_free(struct max8907c *chip) | ||
| 421 | { | ||
| 422 | if (chip->core_irq) | ||
| 423 | free_irq(chip->core_irq, chip); | ||
| 424 | } | ||
| 425 | |||
diff --git a/drivers/mfd/max8907c.c b/drivers/mfd/max8907c.c new file mode 100644 index 00000000000..d03dbced21e --- /dev/null +++ b/drivers/mfd/max8907c.c | |||
| @@ -0,0 +1,376 @@ | |||
| 1 | /* | ||
| 2 | * max8907c.c - mfd driver for MAX8907c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License version 2 as | ||
| 8 | * published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <linux/module.h> | ||
| 12 | #include <linux/init.h> | ||
| 13 | #include <linux/slab.h> | ||
| 14 | #include <linux/i2c.h> | ||
| 15 | #include <linux/mfd/core.h> | ||
| 16 | #include <linux/mfd/max8907c.h> | ||
| 17 | |||
| 18 | static struct mfd_cell cells[] = { | ||
| 19 | {.name = "max8907-regulator",}, | ||
| 20 | {.name = "max8907c-rtc",}, | ||
| 21 | }; | ||
| 22 | |||
| 23 | static int max8907c_i2c_read(struct i2c_client *i2c, u8 reg, u8 count, u8 *dest) | ||
| 24 | { | ||
| 25 | struct i2c_msg xfer[2]; | ||
| 26 | int ret = 0; | ||
| 27 | |||
| 28 | xfer[0].addr = i2c->addr; | ||
| 29 | xfer[0].flags = I2C_M_NOSTART; | ||
| 30 | xfer[0].len = 1; | ||
| 31 | xfer[0].buf = ® | ||
| 32 | |||
| 33 | xfer[1].addr = i2c->addr; | ||
| 34 | xfer[1].flags = I2C_M_RD; | ||
| 35 | xfer[1].len = count; | ||
| 36 | xfer[1].buf = dest; | ||
| 37 | |||
| 38 | ret = i2c_transfer(i2c->adapter, xfer, 2); | ||
| 39 | if (ret < 0) | ||
| 40 | return ret; | ||
| 41 | if (ret != 2) | ||
| 42 | return -EIO; | ||
| 43 | |||
| 44 | return 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | static int max8907c_i2c_write(struct i2c_client *i2c, u8 reg, u8 count, const u8 *src) | ||
| 48 | { | ||
| 49 | u8 msg[0x100 + 1]; | ||
| 50 | int ret = 0; | ||
| 51 | |||
| 52 | msg[0] = reg; | ||
| 53 | memcpy(&msg[1], src, count); | ||
| 54 | |||
| 55 | ret = i2c_master_send(i2c, msg, count + 1); | ||
| 56 | if (ret < 0) | ||
| 57 | return ret; | ||
| 58 | if (ret != count + 1) | ||
| 59 | return -EIO; | ||
| 60 | |||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | int max8907c_reg_read(struct i2c_client *i2c, u8 reg) | ||
| 65 | { | ||
| 66 | int ret; | ||
| 67 | u8 val; | ||
| 68 | |||
| 69 | ret = max8907c_i2c_read(i2c, reg, 1, &val); | ||
| 70 | |||
| 71 | pr_debug("max8907c: reg read reg=%x, val=%x\n", | ||
| 72 | (unsigned int)reg, (unsigned int)val); | ||
| 73 | |||
| 74 | if (ret != 0) | ||
| 75 | pr_err("Failed to read max8907c I2C driver: %d\n", ret); | ||
| 76 | return val; | ||
| 77 | } | ||
| 78 | EXPORT_SYMBOL_GPL(max8907c_reg_read); | ||
| 79 | |||
| 80 | int max8907c_reg_bulk_read(struct i2c_client *i2c, u8 reg, u8 count, u8 *val) | ||
| 81 | { | ||
| 82 | int ret; | ||
| 83 | |||
| 84 | ret = max8907c_i2c_read(i2c, reg, count, val); | ||
| 85 | |||
| 86 | pr_debug("max8907c: reg read reg=%x, val=%x\n", | ||
| 87 | (unsigned int)reg, (unsigned int)*val); | ||
| 88 | |||
| 89 | if (ret != 0) | ||
| 90 | pr_err("Failed to read max8907c I2C driver: %d\n", ret); | ||
| 91 | return ret; | ||
| 92 | } | ||
| 93 | EXPORT_SYMBOL_GPL(max8907c_reg_bulk_read); | ||
| 94 | |||
| 95 | int max8907c_reg_write(struct i2c_client *i2c, u8 reg, u8 val) | ||
| 96 | { | ||
| 97 | struct max8907c *max8907c = i2c_get_clientdata(i2c); | ||
| 98 | int ret; | ||
| 99 | |||
| 100 | pr_debug("max8907c: reg write reg=%x, val=%x\n", | ||
| 101 | (unsigned int)reg, (unsigned int)val); | ||
| 102 | |||
| 103 | mutex_lock(&max8907c->io_lock); | ||
| 104 | ret = max8907c_i2c_write(i2c, reg, 1, &val); | ||
| 105 | mutex_unlock(&max8907c->io_lock); | ||
| 106 | |||
| 107 | if (ret != 0) | ||
| 108 | pr_err("Failed to write max8907c I2C driver: %d\n", ret); | ||
| 109 | return ret; | ||
| 110 | } | ||
| 111 | EXPORT_SYMBOL_GPL(max8907c_reg_write); | ||
| 112 | |||
| 113 | int max8907c_reg_bulk_write(struct i2c_client *i2c, u8 reg, u8 count, u8 *val) | ||
| 114 | { | ||
| 115 | struct max8907c *max8907c = i2c_get_clientdata(i2c); | ||
| 116 | int ret; | ||
| 117 | |||
| 118 | pr_debug("max8907c: reg write reg=%x, val=%x\n", | ||
| 119 | (unsigned int)reg, (unsigned int)*val); | ||
| 120 | |||
| 121 | mutex_lock(&max8907c->io_lock); | ||
| 122 | ret = max8907c_i2c_write(i2c, reg, count, val); | ||
| 123 | mutex_unlock(&max8907c->io_lock); | ||
| 124 | |||
| 125 | if (ret != 0) | ||
| 126 | pr_err("Failed to write max8907c I2C driver: %d\n", ret); | ||
| 127 | return ret; | ||
| 128 | } | ||
| 129 | EXPORT_SYMBOL_GPL(max8907c_reg_bulk_write); | ||
| 130 | |||
| 131 | int max8907c_set_bits(struct i2c_client *i2c, u8 reg, u8 mask, u8 val) | ||
| 132 | { | ||
| 133 | struct max8907c *max8907c = i2c_get_clientdata(i2c); | ||
| 134 | u8 tmp; | ||
| 135 | int ret; | ||
| 136 | |||
| 137 | pr_debug("max8907c: reg write reg=%02X, val=%02X, mask=%02X\n", | ||
| 138 | (unsigned int)reg, (unsigned int)val, (unsigned int)mask); | ||
| 139 | |||
| 140 | mutex_lock(&max8907c->io_lock); | ||
| 141 | ret = max8907c_i2c_read(i2c, reg, 1, &tmp); | ||
| 142 | if (ret == 0) { | ||
| 143 | val = (tmp & ~mask) | (val & mask); | ||
| 144 | ret = max8907c_i2c_write(i2c, reg, 1, &val); | ||
| 145 | } | ||
| 146 | mutex_unlock(&max8907c->io_lock); | ||
| 147 | |||
| 148 | if (ret != 0) | ||
| 149 | pr_err("Failed to write max8907c I2C driver: %d\n", ret); | ||
| 150 | return ret; | ||
| 151 | } | ||
| 152 | EXPORT_SYMBOL_GPL(max8907c_set_bits); | ||
| 153 | |||
| 154 | static struct i2c_client *max8907c_client = NULL; | ||
| 155 | static void max8907c_power_off(void) | ||
| 156 | { | ||
| 157 | if (!max8907c_client) | ||
| 158 | return; | ||
| 159 | |||
| 160 | max8907c_set_bits(max8907c_client, MAX8907C_REG_RESET_CNFG, | ||
| 161 | MAX8907C_MASK_POWER_OFF, 0x40); | ||
| 162 | } | ||
| 163 | |||
| 164 | void max8907c_deep_sleep(int enter) | ||
| 165 | { | ||
| 166 | if (!max8907c_client) | ||
| 167 | return; | ||
| 168 | |||
| 169 | if (enter) { | ||
| 170 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDSEQCNT1, | ||
| 171 | MAX8907C_POWER_UP_DELAY_CNT12); | ||
| 172 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDSEQCNT2, | ||
| 173 | MAX8907C_DELAY_CNT0); | ||
| 174 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDCTL2, | ||
| 175 | MAX8907C_SD_SEQ2); | ||
| 176 | } else { | ||
| 177 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDSEQCNT1, | ||
| 178 | MAX8907C_DELAY_CNT0); | ||
| 179 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDCTL2, | ||
| 180 | MAX8907C_SD_SEQ1); | ||
| 181 | max8907c_reg_write(max8907c_client, MAX8907C_REG_SDSEQCNT2, | ||
| 182 | MAX8907C_POWER_UP_DELAY_CNT1 | MAX8907C_POWER_DOWN_DELAY_CNT12); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | static int max8907c_remove_subdev(struct device *dev, void *unused) | ||
| 187 | { | ||
| 188 | platform_device_unregister(to_platform_device(dev)); | ||
| 189 | return 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | static int max8907c_remove_subdevs(struct max8907c *max8907c) | ||
| 193 | { | ||
| 194 | return device_for_each_child(max8907c->dev, NULL, | ||
| 195 | max8907c_remove_subdev); | ||
| 196 | } | ||
| 197 | |||
| 198 | static int max8097c_add_subdevs(struct max8907c *max8907c, | ||
| 199 | struct max8907c_platform_data *pdata) | ||
| 200 | { | ||
| 201 | struct platform_device *pdev; | ||
| 202 | int ret; | ||
| 203 | int i; | ||
| 204 | |||
| 205 | for (i = 0; i < pdata->num_subdevs; i++) { | ||
| 206 | pdev = platform_device_alloc(pdata->subdevs[i]->name, | ||
| 207 | pdata->subdevs[i]->id); | ||
| 208 | |||
| 209 | pdev->dev.parent = max8907c->dev; | ||
| 210 | pdev->dev.platform_data = pdata->subdevs[i]->dev.platform_data; | ||
| 211 | |||
| 212 | ret = platform_device_add(pdev); | ||
| 213 | if (ret) | ||
| 214 | goto error; | ||
| 215 | } | ||
| 216 | return 0; | ||
| 217 | |||
| 218 | error: | ||
| 219 | max8907c_remove_subdevs(max8907c); | ||
| 220 | return ret; | ||
| 221 | } | ||
| 222 | |||
| 223 | int max8907c_pwr_en_config(void) | ||
| 224 | { | ||
| 225 | int ret; | ||
| 226 | u8 data; | ||
| 227 | |||
| 228 | if (!max8907c_client) | ||
| 229 | return -EINVAL; | ||
| 230 | |||
| 231 | /* | ||
| 232 | * Enable/disable PWREN h/w control mechanism (PWREN signal must be | ||
| 233 | * inactive = high at this time) | ||
| 234 | */ | ||
| 235 | ret = max8907c_set_bits(max8907c_client, MAX8907C_REG_RESET_CNFG, | ||
| 236 | MAX8907C_MASK_PWR_EN, MAX8907C_PWR_EN); | ||
| 237 | if (ret != 0) | ||
| 238 | return ret; | ||
| 239 | |||
| 240 | /* | ||
| 241 | * When enabled, connect PWREN to SEQ2 by clearing SEQ2 configuration | ||
| 242 | * settings for silicon revision that requires s/w WAR. On other | ||
| 243 | * MAX8907B revisions PWREN is always connected to SEQ2. | ||
| 244 | */ | ||
| 245 | data = max8907c_reg_read(max8907c_client, MAX8907C_REG_II2RR); | ||
| 246 | |||
| 247 | if (data == MAX8907B_II2RR_PWREN_WAR) { | ||
| 248 | data = 0x00; | ||
| 249 | ret = max8907c_reg_write(max8907c_client, MAX8907C_REG_SEQ2CNFG, data); | ||
| 250 | } | ||
| 251 | return ret; | ||
| 252 | } | ||
| 253 | |||
| 254 | int max8907c_pwr_en_attach(void) | ||
| 255 | { | ||
| 256 | int ret; | ||
| 257 | |||
| 258 | if (!max8907c_client) | ||
| 259 | return -EINVAL; | ||
| 260 | |||
| 261 | /* No sequencer delay for CPU rail when it is attached */ | ||
| 262 | ret = max8907c_reg_write(max8907c_client, MAX8907C_REG_SDSEQCNT1, | ||
| 263 | MAX8907C_DELAY_CNT0); | ||
| 264 | if (ret != 0) | ||
| 265 | return ret; | ||
| 266 | |||
| 267 | return max8907c_set_bits(max8907c_client, MAX8907C_REG_SDCTL1, | ||
| 268 | MAX8907C_MASK_CTL_SEQ, MAX8907C_CTL_SEQ); | ||
| 269 | } | ||
| 270 | |||
| 271 | static int max8907c_i2c_probe(struct i2c_client *i2c, | ||
| 272 | const struct i2c_device_id *id) | ||
| 273 | { | ||
| 274 | struct max8907c *max8907c; | ||
| 275 | struct max8907c_platform_data *pdata = i2c->dev.platform_data; | ||
| 276 | int ret; | ||
| 277 | int i; | ||
| 278 | |||
| 279 | max8907c = kzalloc(sizeof(struct max8907c), GFP_KERNEL); | ||
| 280 | if (max8907c == NULL) | ||
| 281 | return -ENOMEM; | ||
| 282 | |||
| 283 | max8907c->dev = &i2c->dev; | ||
| 284 | dev_set_drvdata(max8907c->dev, max8907c); | ||
| 285 | |||
| 286 | max8907c->i2c_power = i2c; | ||
| 287 | i2c_set_clientdata(i2c, max8907c); | ||
| 288 | |||
| 289 | max8907c->i2c_rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR); | ||
| 290 | i2c_set_clientdata(max8907c->i2c_rtc, max8907c); | ||
| 291 | |||
| 292 | mutex_init(&max8907c->io_lock); | ||
| 293 | |||
| 294 | for (i = 0; i < ARRAY_SIZE(cells); i++) { | ||
| 295 | cells[i].platform_data = max8907c; | ||
| 296 | cells[i].pdata_size = sizeof(*max8907c); | ||
| 297 | } | ||
| 298 | ret = mfd_add_devices(max8907c->dev, -1, cells, ARRAY_SIZE(cells), | ||
| 299 | NULL, 0); | ||
| 300 | if (ret != 0) { | ||
| 301 | i2c_unregister_device(max8907c->i2c_rtc); | ||
| 302 | kfree(max8907c); | ||
| 303 | pr_debug("max8907c: failed to add MFD devices %X\n", ret); | ||
| 304 | return ret; | ||
| 305 | } | ||
| 306 | |||
| 307 | max8907c_client = i2c; | ||
| 308 | |||
| 309 | max8907c_irq_init(max8907c, i2c->irq, pdata->irq_base); | ||
| 310 | |||
| 311 | ret = max8097c_add_subdevs(max8907c, pdata); | ||
| 312 | |||
| 313 | if (pdata->use_power_off && !pm_power_off) | ||
| 314 | pm_power_off = max8907c_power_off; | ||
| 315 | |||
| 316 | if (pdata->max8907c_setup) | ||
| 317 | return pdata->max8907c_setup(); | ||
| 318 | |||
| 319 | return ret; | ||
| 320 | } | ||
| 321 | |||
| 322 | static int max8907c_i2c_remove(struct i2c_client *i2c) | ||
| 323 | { | ||
| 324 | struct max8907c *max8907c = i2c_get_clientdata(i2c); | ||
| 325 | |||
| 326 | max8907c_remove_subdevs(max8907c); | ||
| 327 | i2c_unregister_device(max8907c->i2c_rtc); | ||
| 328 | mfd_remove_devices(max8907c->dev); | ||
| 329 | max8907c_irq_free(max8907c); | ||
| 330 | kfree(max8907c); | ||
| 331 | |||
| 332 | return 0; | ||
| 333 | } | ||
| 334 | |||
| 335 | static const struct i2c_device_id max8907c_i2c_id[] = { | ||
| 336 | {"max8907c", 0}, | ||
| 337 | {} | ||
| 338 | }; | ||
| 339 | |||
| 340 | MODULE_DEVICE_TABLE(i2c, max8907c_i2c_id); | ||
| 341 | |||
| 342 | static struct i2c_driver max8907c_i2c_driver = { | ||
| 343 | .driver = { | ||
| 344 | .name = "max8907c", | ||
| 345 | .owner = THIS_MODULE, | ||
| 346 | }, | ||
| 347 | .probe = max8907c_i2c_probe, | ||
| 348 | .remove = max8907c_i2c_remove, | ||
| 349 | .suspend = max8907c_suspend, | ||
| 350 | .resume = max8907c_resume, | ||
| 351 | .id_table = max8907c_i2c_id, | ||
| 352 | }; | ||
| 353 | |||
| 354 | static int __init max8907c_i2c_init(void) | ||
| 355 | { | ||
| 356 | int ret = -ENODEV; | ||
| 357 | |||
| 358 | ret = i2c_add_driver(&max8907c_i2c_driver); | ||
| 359 | if (ret != 0) | ||
| 360 | pr_err("Failed to register I2C driver: %d\n", ret); | ||
| 361 | |||
| 362 | return ret; | ||
| 363 | } | ||
| 364 | |||
| 365 | subsys_initcall(max8907c_i2c_init); | ||
| 366 | |||
| 367 | static void __exit max8907c_i2c_exit(void) | ||
| 368 | { | ||
| 369 | i2c_del_driver(&max8907c_i2c_driver); | ||
| 370 | } | ||
| 371 | |||
| 372 | module_exit(max8907c_i2c_exit); | ||
| 373 | |||
| 374 | MODULE_DESCRIPTION("MAX8907C multi-function core driver"); | ||
| 375 | MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>"); | ||
| 376 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mfd/ricoh583.c b/drivers/mfd/ricoh583.c new file mode 100644 index 00000000000..a29053ebaf8 --- /dev/null +++ b/drivers/mfd/ricoh583.c | |||
| @@ -0,0 +1,1213 @@ | |||
| 1 | /* | ||
| 2 | * driver/mfd/ricoh583.c | ||
| 3 | * | ||
| 4 | * Core driver implementation to access RICOH583 power management chip. | ||
| 5 | * | ||
| 6 | * Copyright (C) 2011 NVIDIA Corporation | ||
| 7 | * | ||
| 8 | * Copyright (C) 2011 RICOH COMPANY,LTD | ||
| 9 | * | ||
| 10 | * This program is free software; you can redistribute it and/or modify | ||
| 11 | * it under the terms of the GNU General Public License as published by | ||
| 12 | * the Free Software Foundation; either version 2 of the License, or | ||
| 13 | * (at your option) any later version. | ||
| 14 | * | ||
| 15 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 18 | * more details. | ||
| 19 | * | ||
| 20 | * You should have received a copy of the GNU General Public License along | ||
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 23 | * | ||
| 24 | */ | ||
| 25 | /*#define DEBUG 1*/ | ||
| 26 | /*#define VERBOSE_DEBUG 1*/ | ||
| 27 | #include <linux/interrupt.h> | ||
| 28 | #include <linux/irq.h> | ||
| 29 | #include <linux/kernel.h> | ||
| 30 | #include <linux/module.h> | ||
| 31 | #include <linux/init.h> | ||
| 32 | #include <linux/slab.h> | ||
| 33 | #include <linux/gpio.h> | ||
| 34 | #include <linux/i2c.h> | ||
| 35 | #include <linux/mfd/core.h> | ||
| 36 | #include <linux/mfd/ricoh583.h> | ||
| 37 | |||
| 38 | #define RICOH_ONOFFSEL_REG 0x10 | ||
| 39 | #define RICOH_SWCTL_REG 0x5E | ||
| 40 | |||
| 41 | /* Interrupt enable register */ | ||
| 42 | #define RICOH583_INT_EN_SYS1 0x19 | ||
| 43 | #define RICOH583_INT_EN_SYS2 0x1D | ||
| 44 | #define RICOH583_INT_EN_DCDC 0x41 | ||
| 45 | #define RICOH583_INT_EN_RTC 0xED | ||
| 46 | #define RICOH583_INT_EN_ADC1 0x90 | ||
| 47 | #define RICOH583_INT_EN_ADC2 0x91 | ||
| 48 | #define RICOH583_INT_EN_ADC3 0x92 | ||
| 49 | #define RICOH583_INT_EN_GPIO 0xA8 | ||
| 50 | |||
| 51 | /* interrupt status registers (monitor regs in Ricoh)*/ | ||
| 52 | #define RICOH583_INTC_INTPOL 0xAD | ||
| 53 | #define RICOH583_INTC_INTEN 0xAE | ||
| 54 | #define RICOH583_INTC_INTMON 0xAF | ||
| 55 | |||
| 56 | #define RICOH583_INT_MON_GRP 0xAF | ||
| 57 | #define RICOH583_INT_MON_SYS1 0x1B | ||
| 58 | #define RICOH583_INT_MON_SYS2 0x1F | ||
| 59 | #define RICOH583_INT_MON_DCDC 0x43 | ||
| 60 | #define RICOH583_INT_MON_RTC 0xEE | ||
| 61 | |||
| 62 | /* interrupt clearing registers */ | ||
| 63 | #define RICOH583_INT_IR_SYS1 0x1A | ||
| 64 | #define RICOH583_INT_IR_SYS2 0x1E | ||
| 65 | #define RICOH583_INT_IR_DCDC 0x42 | ||
| 66 | #define RICOH583_INT_IR_RTC 0xEE | ||
| 67 | #define RICOH583_INT_IR_ADCL 0x94 | ||
| 68 | #define RICOH583_INT_IR_ADCH 0x95 | ||
| 69 | #define RICOH583_INT_IR_ADCEND 0x96 | ||
| 70 | #define RICOH583_INT_IR_GPIOR 0xA9 | ||
| 71 | #define RICOH583_INT_IR_GPIOF 0xAA | ||
| 72 | |||
| 73 | /* GPIO register base address */ | ||
| 74 | #define RICOH583_GPIO_IOSEL 0xA0 | ||
| 75 | #define RICOH583_GPIO_PDEN 0xA1 | ||
| 76 | #define RICOH583_GPIO_IOOUT 0xA2 | ||
| 77 | #define RICOH583_GPIO_PGSEL 0xA3 | ||
| 78 | #define RICOH583_GPIO_GPINV 0xA4 | ||
| 79 | #define RICOH583_GPIO_GPDEB 0xA5 | ||
| 80 | #define RICOH583_GPIO_GPEDGE1 0xA6 | ||
| 81 | #define RICOH583_GPIO_GPEDGE2 0xA7 | ||
| 82 | #define RICOH583_GPIO_EN_GPIR 0xA8 | ||
| 83 | #define RICOH583_GPIO_MON_IOIN 0xAB | ||
| 84 | #define RICOH583_GPIO_GPOFUNC 0xAC | ||
| 85 | #define RICOH583_INTC_INTEN 0xAE | ||
| 86 | |||
| 87 | enum int_type { | ||
| 88 | SYS_INT = 0x1, | ||
| 89 | DCDC_INT = 0x2, | ||
| 90 | RTC_INT = 0x4, | ||
| 91 | ADC_INT = 0x8, | ||
| 92 | GPIO_INT = 0x10, | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct ricoh583_irq_data { | ||
| 96 | u8 int_type; | ||
| 97 | u8 master_bit; | ||
| 98 | u8 int_en_bit; | ||
| 99 | u8 mask_reg_index; | ||
| 100 | int grp_index; | ||
| 101 | }; | ||
| 102 | |||
| 103 | struct deepsleep_control_data { | ||
| 104 | u8 reg_add; | ||
| 105 | u8 ds_pos_bit; | ||
| 106 | }; | ||
| 107 | |||
| 108 | #define RICOH583_IRQ(_int_type, _master_bit, _grp_index, _int_bit, _mask_ind) \ | ||
| 109 | { \ | ||
| 110 | .int_type = _int_type, \ | ||
| 111 | .master_bit = _master_bit, \ | ||
| 112 | .grp_index = _grp_index, \ | ||
| 113 | .int_en_bit = _int_bit, \ | ||
| 114 | .mask_reg_index = _mask_ind, \ | ||
| 115 | } | ||
| 116 | |||
| 117 | static const struct ricoh583_irq_data ricoh583_irqs[] = { | ||
| 118 | [RICOH583_IRQ_ONKEY] = RICOH583_IRQ(SYS_INT, 0, 0, 0, 0), | ||
| 119 | [RICOH583_IRQ_ACOK] = RICOH583_IRQ(SYS_INT, 0, 1, 1, 0), | ||
| 120 | [RICOH583_IRQ_LIDOPEN] = RICOH583_IRQ(SYS_INT, 0, 2, 2, 0), | ||
| 121 | [RICOH583_IRQ_PREOT] = RICOH583_IRQ(SYS_INT, 0, 3, 3, 0), | ||
| 122 | [RICOH583_IRQ_CLKSTP] = RICOH583_IRQ(SYS_INT, 0, 4, 4, 0), | ||
| 123 | [RICOH583_IRQ_ONKEY_OFF] = RICOH583_IRQ(SYS_INT, 0, 5, 5, 0), | ||
| 124 | [RICOH583_IRQ_WD] = RICOH583_IRQ(SYS_INT, 0, 7, 7, 0), | ||
| 125 | [RICOH583_IRQ_EN_PWRREQ1] = RICOH583_IRQ(SYS_INT, 0, 8, 0, 1), | ||
| 126 | [RICOH583_IRQ_EN_PWRREQ2] = RICOH583_IRQ(SYS_INT, 0, 9, 1, 1), | ||
| 127 | [RICOH583_IRQ_PRE_VINDET] = RICOH583_IRQ(SYS_INT, 0, 10, 2, 1), | ||
| 128 | |||
| 129 | [RICOH583_IRQ_DC0LIM] = RICOH583_IRQ(DCDC_INT, 1, 0, 0, 2), | ||
| 130 | [RICOH583_IRQ_DC1LIM] = RICOH583_IRQ(DCDC_INT, 1, 1, 1, 2), | ||
| 131 | [RICOH583_IRQ_DC2LIM] = RICOH583_IRQ(DCDC_INT, 1, 2, 2, 2), | ||
| 132 | [RICOH583_IRQ_DC3LIM] = RICOH583_IRQ(DCDC_INT, 1, 3, 3, 2), | ||
| 133 | |||
| 134 | [RICOH583_IRQ_CTC] = RICOH583_IRQ(RTC_INT, 2, 0, 0, 3), | ||
| 135 | [RICOH583_IRQ_YALE] = RICOH583_IRQ(RTC_INT, 2, 5, 5, 3), | ||
| 136 | [RICOH583_IRQ_DALE] = RICOH583_IRQ(RTC_INT, 2, 6, 6, 3), | ||
| 137 | [RICOH583_IRQ_WALE] = RICOH583_IRQ(RTC_INT, 2, 7, 7, 3), | ||
| 138 | |||
| 139 | [RICOH583_IRQ_AIN1L] = RICOH583_IRQ(ADC_INT, 3, 0, 0, 4), | ||
| 140 | [RICOH583_IRQ_AIN2L] = RICOH583_IRQ(ADC_INT, 3, 1, 1, 4), | ||
| 141 | [RICOH583_IRQ_AIN3L] = RICOH583_IRQ(ADC_INT, 3, 2, 2, 4), | ||
| 142 | [RICOH583_IRQ_VBATL] = RICOH583_IRQ(ADC_INT, 3, 3, 3, 4), | ||
| 143 | [RICOH583_IRQ_VIN3L] = RICOH583_IRQ(ADC_INT, 3, 4, 4, 4), | ||
| 144 | [RICOH583_IRQ_VIN8L] = RICOH583_IRQ(ADC_INT, 3, 5, 5, 4), | ||
| 145 | [RICOH583_IRQ_AIN1H] = RICOH583_IRQ(ADC_INT, 3, 6, 0, 5), | ||
| 146 | [RICOH583_IRQ_AIN2H] = RICOH583_IRQ(ADC_INT, 3, 7, 1, 5), | ||
| 147 | [RICOH583_IRQ_AIN3H] = RICOH583_IRQ(ADC_INT, 3, 8, 2, 5), | ||
| 148 | [RICOH583_IRQ_VBATH] = RICOH583_IRQ(ADC_INT, 3, 9, 3, 5), | ||
| 149 | [RICOH583_IRQ_VIN3H] = RICOH583_IRQ(ADC_INT, 3, 10, 4, 5), | ||
| 150 | [RICOH583_IRQ_VIN8H] = RICOH583_IRQ(ADC_INT, 3, 11, 5, 5), | ||
| 151 | [RICOH583_IRQ_ADCEND] = RICOH583_IRQ(ADC_INT, 3, 12, 0, 6), | ||
| 152 | |||
| 153 | [RICOH583_IRQ_GPIO0] = RICOH583_IRQ(GPIO_INT, 4, 0, 0, 7), | ||
| 154 | [RICOH583_IRQ_GPIO1] = RICOH583_IRQ(GPIO_INT, 4, 1, 1, 7), | ||
| 155 | [RICOH583_IRQ_GPIO2] = RICOH583_IRQ(GPIO_INT, 4, 2, 2, 7), | ||
| 156 | [RICOH583_IRQ_GPIO3] = RICOH583_IRQ(GPIO_INT, 4, 3, 3, 7), | ||
| 157 | [RICOH583_IRQ_GPIO4] = RICOH583_IRQ(GPIO_INT, 4, 4, 4, 7), | ||
| 158 | [RICOH583_IRQ_GPIO5] = RICOH583_IRQ(GPIO_INT, 4, 5, 5, 7), | ||
| 159 | [RICOH583_IRQ_GPIO6] = RICOH583_IRQ(GPIO_INT, 4, 6, 6, 7), | ||
| 160 | [RICOH583_IRQ_GPIO7] = RICOH583_IRQ(GPIO_INT, 4, 7, 7, 7), | ||
| 161 | [RICOH583_NR_IRQS] = RICOH583_IRQ(GPIO_INT, 4, 8, 8, 7), | ||
| 162 | }; | ||
| 163 | |||
| 164 | #define DEEPSLEEP_INIT(_id, _reg, _pos) \ | ||
| 165 | [RICOH583_DS_##_id] = {.reg_add = _reg, .ds_pos_bit = _pos} | ||
| 166 | |||
| 167 | static struct deepsleep_control_data deepsleep_data[] = { | ||
| 168 | DEEPSLEEP_INIT(DC1, 0x21, 4), | ||
| 169 | DEEPSLEEP_INIT(DC2, 0x22, 0), | ||
| 170 | DEEPSLEEP_INIT(DC3, 0x22, 4), | ||
| 171 | DEEPSLEEP_INIT(LDO0, 0x23, 0), | ||
| 172 | DEEPSLEEP_INIT(LDO1, 0x23, 4), | ||
| 173 | DEEPSLEEP_INIT(LDO2, 0x24, 0), | ||
| 174 | DEEPSLEEP_INIT(LDO3, 0x24, 4), | ||
| 175 | DEEPSLEEP_INIT(LDO4, 0x25, 0), | ||
| 176 | DEEPSLEEP_INIT(LDO5, 0x25, 4), | ||
| 177 | DEEPSLEEP_INIT(LDO6, 0x26, 0), | ||
| 178 | DEEPSLEEP_INIT(LDO7, 0x26, 4), | ||
| 179 | DEEPSLEEP_INIT(LDO8, 0x27, 0), | ||
| 180 | DEEPSLEEP_INIT(LDO9, 0x27, 4), | ||
| 181 | DEEPSLEEP_INIT(PSO0, 0x28, 0), | ||
| 182 | DEEPSLEEP_INIT(PSO1, 0x28, 4), | ||
| 183 | DEEPSLEEP_INIT(PSO2, 0x29, 0), | ||
| 184 | DEEPSLEEP_INIT(PSO3, 0x29, 4), | ||
| 185 | DEEPSLEEP_INIT(PSO4, 0x2A, 0), | ||
| 186 | DEEPSLEEP_INIT(PSO5, 0x2A, 4), | ||
| 187 | DEEPSLEEP_INIT(PSO6, 0x2B, 0), | ||
| 188 | DEEPSLEEP_INIT(PSO7, 0x2B, 4), | ||
| 189 | }; | ||
| 190 | |||
| 191 | #define MAX_INTERRUPT_MASKS 8 | ||
| 192 | #define MAX_MAIN_INTERRUPT 5 | ||
| 193 | #define EXT_PWR_REQ \ | ||
| 194 | (RICOH583_EXT_PWRREQ1_CONTROL | RICOH583_EXT_PWRREQ2_CONTROL) | ||
| 195 | |||
| 196 | struct ricoh583 { | ||
| 197 | struct device *dev; | ||
| 198 | struct i2c_client *client; | ||
| 199 | struct mutex io_lock; | ||
| 200 | int gpio_base; | ||
| 201 | struct gpio_chip gpio; | ||
| 202 | int irq_base; | ||
| 203 | struct irq_chip irq_chip; | ||
| 204 | struct mutex irq_lock; | ||
| 205 | unsigned long group_irq_en[MAX_MAIN_INTERRUPT]; | ||
| 206 | |||
| 207 | /* For main interrupt bits in INTC */ | ||
| 208 | u8 intc_inten_cache; | ||
| 209 | u8 intc_inten_reg; | ||
| 210 | |||
| 211 | /* For group interrupt bits and address */ | ||
| 212 | u8 irq_en_cache[MAX_INTERRUPT_MASKS]; | ||
| 213 | u8 irq_en_reg[MAX_INTERRUPT_MASKS]; | ||
| 214 | u8 irq_en_add[MAX_INTERRUPT_MASKS]; | ||
| 215 | |||
| 216 | /* Interrupt monitor and clear register */ | ||
| 217 | u8 irq_mon_add[MAX_INTERRUPT_MASKS + 1]; | ||
| 218 | u8 irq_clr_add[MAX_INTERRUPT_MASKS + 1]; | ||
| 219 | u8 main_int_type[MAX_INTERRUPT_MASKS + 1]; | ||
| 220 | |||
| 221 | /* For gpio edge */ | ||
| 222 | u8 gpedge_cache[2]; | ||
| 223 | u8 gpedge_reg[2]; | ||
| 224 | u8 gpedge_add[2]; | ||
| 225 | }; | ||
| 226 | |||
| 227 | static inline int __ricoh583_read(struct i2c_client *client, | ||
| 228 | u8 reg, uint8_t *val) | ||
| 229 | { | ||
| 230 | int ret; | ||
| 231 | |||
| 232 | ret = i2c_smbus_read_byte_data(client, reg); | ||
| 233 | if (ret < 0) { | ||
| 234 | dev_err(&client->dev, "failed reading at 0x%02x\n", reg); | ||
| 235 | return ret; | ||
| 236 | } | ||
| 237 | |||
| 238 | *val = (uint8_t)ret; | ||
| 239 | dev_dbg(&client->dev, "ricoh583: reg read reg=%x, val=%x\n", | ||
| 240 | reg, *val); | ||
| 241 | return 0; | ||
| 242 | } | ||
| 243 | |||
| 244 | static inline int __ricoh583_bulk_reads(struct i2c_client *client, u8 reg, | ||
| 245 | int len, uint8_t *val) | ||
| 246 | { | ||
| 247 | int ret; | ||
| 248 | int i; | ||
| 249 | |||
| 250 | ret = i2c_smbus_read_i2c_block_data(client, reg, len, val); | ||
| 251 | if (ret < 0) { | ||
| 252 | dev_err(&client->dev, "failed reading from 0x%02x\n", reg); | ||
| 253 | return ret; | ||
| 254 | } | ||
| 255 | for (i = 0; i < len; ++i) { | ||
| 256 | dev_dbg(&client->dev, "ricoh583: reg read reg=%x, val=%x\n", | ||
| 257 | reg + i, *(val + i)); | ||
| 258 | } | ||
| 259 | return 0; | ||
| 260 | } | ||
| 261 | |||
| 262 | static inline int __ricoh583_write(struct i2c_client *client, | ||
| 263 | u8 reg, uint8_t val) | ||
| 264 | { | ||
| 265 | int ret; | ||
| 266 | |||
| 267 | dev_dbg(&client->dev, "ricoh583: reg write reg=%x, val=%x\n", | ||
| 268 | reg, val); | ||
| 269 | ret = i2c_smbus_write_byte_data(client, reg, val); | ||
| 270 | if (ret < 0) { | ||
| 271 | dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n", | ||
| 272 | val, reg); | ||
| 273 | return ret; | ||
| 274 | } | ||
| 275 | |||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | static inline int __ricoh583_bulk_writes(struct i2c_client *client, u8 reg, | ||
| 280 | int len, uint8_t *val) | ||
| 281 | { | ||
| 282 | int ret; | ||
| 283 | int i; | ||
| 284 | |||
| 285 | for (i = 0; i < len; ++i) { | ||
| 286 | dev_dbg(&client->dev, "ricoh583: reg write reg=%x, val=%x\n", | ||
| 287 | reg + i, *(val + i)); | ||
| 288 | } | ||
| 289 | |||
| 290 | ret = i2c_smbus_write_i2c_block_data(client, reg, len, val); | ||
| 291 | if (ret < 0) { | ||
| 292 | dev_err(&client->dev, "failed writings to 0x%02x\n", reg); | ||
| 293 | return ret; | ||
| 294 | } | ||
| 295 | |||
| 296 | return 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | int ricoh583_write(struct device *dev, u8 reg, uint8_t val) | ||
| 300 | { | ||
| 301 | struct ricoh583 *ricoh583 = dev_get_drvdata(dev); | ||
| 302 | int ret = 0; | ||
| 303 | |||
| 304 | mutex_lock(&ricoh583->io_lock); | ||
| 305 | ret = __ricoh583_write(to_i2c_client(dev), reg, val); | ||
| 306 | mutex_unlock(&ricoh583->io_lock); | ||
| 307 | |||
| 308 | return ret; | ||
| 309 | } | ||
| 310 | EXPORT_SYMBOL_GPL(ricoh583_write); | ||
| 311 | |||
| 312 | int ricoh583_bulk_writes(struct device *dev, u8 reg, u8 len, uint8_t *val) | ||
| 313 | { | ||
| 314 | struct ricoh583 *ricoh583 = dev_get_drvdata(dev); | ||
| 315 | int ret = 0; | ||
| 316 | |||
| 317 | mutex_lock(&ricoh583->io_lock); | ||
| 318 | ret = __ricoh583_bulk_writes(to_i2c_client(dev), reg, len, val); | ||
| 319 | mutex_unlock(&ricoh583->io_lock); | ||
| 320 | |||
| 321 | return ret; | ||
| 322 | } | ||
| 323 | EXPORT_SYMBOL_GPL(ricoh583_bulk_writes); | ||
| 324 | |||
| 325 | int ricoh583_read(struct device *dev, u8 reg, uint8_t *val) | ||
| 326 | { | ||
| 327 | return __ricoh583_read(to_i2c_client(dev), reg, val); | ||
| 328 | } | ||
| 329 | EXPORT_SYMBOL_GPL(ricoh583_read); | ||
| 330 | |||
| 331 | int ricoh583_bulk_reads(struct device *dev, u8 reg, u8 len, uint8_t *val) | ||
| 332 | { | ||
| 333 | return __ricoh583_bulk_reads(to_i2c_client(dev), reg, len, val); | ||
| 334 | } | ||
| 335 | EXPORT_SYMBOL_GPL(ricoh583_bulk_reads); | ||
| 336 | |||
| 337 | int ricoh583_set_bits(struct device *dev, u8 reg, uint8_t bit_mask) | ||
| 338 | { | ||
| 339 | struct ricoh583 *ricoh583 = dev_get_drvdata(dev); | ||
| 340 | uint8_t reg_val; | ||
| 341 | int ret = 0; | ||
| 342 | |||
| 343 | mutex_lock(&ricoh583->io_lock); | ||
| 344 | |||
| 345 | ret = __ricoh583_read(to_i2c_client(dev), reg, ®_val); | ||
| 346 | if (ret) | ||
| 347 | goto out; | ||
| 348 | |||
| 349 | if ((reg_val & bit_mask) != bit_mask) { | ||
| 350 | reg_val |= bit_mask; | ||
| 351 | ret = __ricoh583_write(to_i2c_client(dev), reg, reg_val); | ||
| 352 | } | ||
| 353 | out: | ||
| 354 | mutex_unlock(&ricoh583->io_lock); | ||
| 355 | return ret; | ||
| 356 | } | ||
| 357 | EXPORT_SYMBOL_GPL(ricoh583_set_bits); | ||
| 358 | |||
| 359 | int ricoh583_clr_bits(struct device *dev, u8 reg, uint8_t bit_mask) | ||
| 360 | { | ||
| 361 | struct ricoh583 *ricoh583 = dev_get_drvdata(dev); | ||
| 362 | uint8_t reg_val; | ||
| 363 | int ret = 0; | ||
| 364 | |||
| 365 | mutex_lock(&ricoh583->io_lock); | ||
| 366 | |||
| 367 | ret = __ricoh583_read(to_i2c_client(dev), reg, ®_val); | ||
| 368 | if (ret) | ||
| 369 | goto out; | ||
| 370 | |||
| 371 | if (reg_val & bit_mask) { | ||
| 372 | reg_val &= ~bit_mask; | ||
| 373 | ret = __ricoh583_write(to_i2c_client(dev), reg, reg_val); | ||
| 374 | } | ||
| 375 | out: | ||
| 376 | mutex_unlock(&ricoh583->io_lock); | ||
| 377 | return ret; | ||
| 378 | } | ||
| 379 | EXPORT_SYMBOL_GPL(ricoh583_clr_bits); | ||
| 380 | |||
| 381 | int ricoh583_update(struct device *dev, u8 reg, uint8_t val, uint8_t mask) | ||
| 382 | { | ||
| 383 | struct ricoh583 *ricoh583 = dev_get_drvdata(dev); | ||
| 384 | uint8_t reg_val; | ||
| 385 | int ret = 0; | ||
| 386 | |||
| 387 | mutex_lock(&ricoh583->io_lock); | ||
| 388 | |||
| 389 | ret = __ricoh583_read(ricoh583->client, reg, ®_val); | ||
| 390 | if (ret) | ||
| 391 | goto out; | ||
| 392 | |||
| 393 | if ((reg_val & mask) != val) { | ||
| 394 | reg_val = (reg_val & ~mask) | (val & mask); | ||
| 395 | ret = __ricoh583_write(ricoh583->client, reg, reg_val); | ||
| 396 | } | ||
| 397 | out: | ||
| 398 | mutex_unlock(&ricoh583->io_lock); | ||
| 399 | return ret; | ||
| 400 | } | ||
| 401 | EXPORT_SYMBOL_GPL(ricoh583_update); | ||
| 402 | |||
| 403 | static int __ricoh583_set_ext_pwrreq1_control(struct device *dev, | ||
| 404 | enum ricoh583_deepsleep_control_id id, | ||
| 405 | enum ricoh583_ext_pwrreq_control ext_pwr, int slots) | ||
| 406 | { | ||
| 407 | int ret; | ||
| 408 | uint8_t sleepseq_val; | ||
| 409 | u8 en_bit; | ||
| 410 | u8 slot_bit; | ||
| 411 | |||
| 412 | if (!(ext_pwr & RICOH583_EXT_PWRREQ1_CONTROL)) | ||
| 413 | return 0; | ||
| 414 | |||
| 415 | if (id == RICOH583_DS_DC0) { | ||
| 416 | dev_err(dev, "PWRREQ1 is invalid control for rail %d\n", id); | ||
| 417 | return -EINVAL; | ||
| 418 | } | ||
| 419 | |||
| 420 | en_bit = deepsleep_data[id].ds_pos_bit; | ||
| 421 | slot_bit = en_bit + 1; | ||
| 422 | ret = ricoh583_read(dev, deepsleep_data[id].reg_add, &sleepseq_val); | ||
| 423 | if (ret < 0) { | ||
| 424 | dev_err(dev, "Error in reading reg 0x%x\n", | ||
| 425 | deepsleep_data[id].reg_add); | ||
| 426 | return ret; | ||
| 427 | } | ||
| 428 | |||
| 429 | sleepseq_val &= ~(0xF << en_bit); | ||
| 430 | sleepseq_val |= (1 << en_bit); | ||
| 431 | sleepseq_val |= ((slots & 0x7) << slot_bit); | ||
| 432 | ret = ricoh583_set_bits(dev, RICOH_ONOFFSEL_REG, (1 << 1)); | ||
| 433 | if (ret < 0) { | ||
| 434 | dev_err(dev, "Error in updating the 0x%02x register\n", | ||
| 435 | RICOH_ONOFFSEL_REG); | ||
| 436 | return ret; | ||
| 437 | } | ||
| 438 | |||
| 439 | ret = ricoh583_write(dev, deepsleep_data[id].reg_add, sleepseq_val); | ||
| 440 | if (ret < 0) { | ||
| 441 | dev_err(dev, "Error in writing reg 0x%x\n", | ||
| 442 | deepsleep_data[id].reg_add); | ||
| 443 | return ret; | ||
| 444 | } | ||
| 445 | |||
| 446 | if (id == RICOH583_DS_LDO4) { | ||
| 447 | ret = ricoh583_write(dev, RICOH_SWCTL_REG, 0x1); | ||
| 448 | if (ret < 0) | ||
| 449 | dev_err(dev, "Error in writing reg 0x%x\n", | ||
| 450 | RICOH_SWCTL_REG); | ||
| 451 | } | ||
| 452 | return ret; | ||
| 453 | } | ||
| 454 | |||
| 455 | static int __ricoh583_set_ext_pwrreq2_control(struct device *dev, | ||
| 456 | enum ricoh583_deepsleep_control_id id, | ||
| 457 | enum ricoh583_ext_pwrreq_control ext_pwr) | ||
| 458 | { | ||
| 459 | int ret; | ||
| 460 | |||
| 461 | if (!(ext_pwr & RICOH583_EXT_PWRREQ2_CONTROL)) | ||
| 462 | return 0; | ||
| 463 | |||
| 464 | if (id != RICOH583_DS_DC0) { | ||
| 465 | dev_err(dev, "PWRREQ2 is invalid control for rail %d\n", id); | ||
| 466 | return -EINVAL; | ||
| 467 | } | ||
| 468 | |||
| 469 | ret = ricoh583_set_bits(dev, RICOH_ONOFFSEL_REG, (1 << 2)); | ||
| 470 | if (ret < 0) | ||
| 471 | dev_err(dev, "Error in updating the ONOFFSEL 0x10 register\n"); | ||
| 472 | return ret; | ||
| 473 | } | ||
| 474 | |||
| 475 | int ricoh583_ext_power_req_config(struct device *dev, | ||
| 476 | enum ricoh583_deepsleep_control_id id, | ||
| 477 | enum ricoh583_ext_pwrreq_control ext_pwr_req, | ||
| 478 | int deepsleep_slot_nr) | ||
| 479 | { | ||
| 480 | if ((ext_pwr_req & EXT_PWR_REQ) == EXT_PWR_REQ) | ||
| 481 | return -EINVAL; | ||
| 482 | |||
| 483 | if (ext_pwr_req & RICOH583_EXT_PWRREQ1_CONTROL) | ||
| 484 | return __ricoh583_set_ext_pwrreq1_control(dev, id, | ||
| 485 | ext_pwr_req, deepsleep_slot_nr); | ||
| 486 | |||
| 487 | if (ext_pwr_req & RICOH583_EXT_PWRREQ2_CONTROL) | ||
| 488 | return __ricoh583_set_ext_pwrreq2_control(dev, | ||
| 489 | id, ext_pwr_req); | ||
| 490 | return 0; | ||
| 491 | } | ||
| 492 | EXPORT_SYMBOL_GPL(ricoh583_ext_power_req_config); | ||
| 493 | |||
| 494 | static int __devinit ricoh583_ext_power_init(struct ricoh583 *ricoh583, | ||
| 495 | struct ricoh583_platform_data *pdata) | ||
| 496 | { | ||
| 497 | int ret; | ||
| 498 | int i; | ||
| 499 | uint8_t on_off_val = 0; | ||
| 500 | |||
| 501 | /* Clear ONOFFSEL register */ | ||
| 502 | mutex_lock(&ricoh583->io_lock); | ||
| 503 | if (pdata->enable_shutdown_pin) | ||
| 504 | on_off_val |= 0x1; | ||
| 505 | |||
| 506 | ret = __ricoh583_write(ricoh583->client, RICOH_ONOFFSEL_REG, | ||
| 507 | on_off_val); | ||
| 508 | if (ret < 0) | ||
| 509 | dev_err(ricoh583->dev, "Error in writing reg %d error: " | ||
| 510 | "%d\n", RICOH_ONOFFSEL_REG, ret); | ||
| 511 | |||
| 512 | ret = __ricoh583_write(ricoh583->client, RICOH_SWCTL_REG, 0x0); | ||
| 513 | if (ret < 0) | ||
| 514 | dev_err(ricoh583->dev, "Error in writing reg %d error: " | ||
| 515 | "%d\n", RICOH_SWCTL_REG, ret); | ||
| 516 | |||
| 517 | /* Clear sleepseq register */ | ||
| 518 | for (i = 0x21; i < 0x2B; ++i) { | ||
| 519 | ret = __ricoh583_write(ricoh583->client, i, 0x0); | ||
| 520 | if (ret < 0) | ||
| 521 | dev_err(ricoh583->dev, "Error in writing reg 0x%02x " | ||
| 522 | "error: %d\n", i, ret); | ||
| 523 | } | ||
| 524 | mutex_unlock(&ricoh583->io_lock); | ||
| 525 | return 0; | ||
| 526 | } | ||
| 527 | |||
| 528 | static struct i2c_client *ricoh583_i2c_client; | ||
| 529 | int ricoh583_power_off(void) | ||
| 530 | { | ||
| 531 | if (!ricoh583_i2c_client) | ||
| 532 | return -EINVAL; | ||
| 533 | |||
| 534 | return 0; | ||
| 535 | } | ||
| 536 | |||
| 537 | static int ricoh583_gpio_get(struct gpio_chip *gc, unsigned offset) | ||
| 538 | { | ||
| 539 | struct ricoh583 *ricoh583 = container_of(gc, struct ricoh583, gpio); | ||
| 540 | uint8_t val; | ||
| 541 | int ret; | ||
| 542 | |||
| 543 | ret = __ricoh583_read(ricoh583->client, RICOH583_GPIO_MON_IOIN, &val); | ||
| 544 | if (ret < 0) | ||
| 545 | return ret; | ||
| 546 | |||
| 547 | return ((val & (0x1 << offset)) != 0); | ||
| 548 | } | ||
| 549 | |||
| 550 | static void ricoh583_gpio_set(struct gpio_chip *chip, unsigned offset, | ||
| 551 | int value) | ||
| 552 | { | ||
| 553 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 554 | if (value) | ||
| 555 | ricoh583_set_bits(ricoh583->dev, RICOH583_GPIO_IOOUT, | ||
| 556 | 1 << offset); | ||
| 557 | else | ||
| 558 | ricoh583_clr_bits(ricoh583->dev, RICOH583_GPIO_IOOUT, | ||
| 559 | 1 << offset); | ||
| 560 | } | ||
| 561 | |||
| 562 | static int ricoh583_gpio_input(struct gpio_chip *chip, unsigned offset) | ||
| 563 | { | ||
| 564 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 565 | |||
| 566 | return ricoh583_clr_bits(ricoh583->dev, RICOH583_GPIO_IOSEL, | ||
| 567 | 1 << offset); | ||
| 568 | } | ||
| 569 | |||
| 570 | static int ricoh583_gpio_output(struct gpio_chip *chip, unsigned offset, | ||
| 571 | int value) | ||
| 572 | { | ||
| 573 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 574 | |||
| 575 | ricoh583_gpio_set(chip, offset, value); | ||
| 576 | return ricoh583_set_bits(ricoh583->dev, RICOH583_GPIO_IOSEL, | ||
| 577 | 1 << offset); | ||
| 578 | } | ||
| 579 | |||
| 580 | static int ricoh583_gpio_to_irq(struct gpio_chip *chip, unsigned off) | ||
| 581 | { | ||
| 582 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 583 | |||
| 584 | if ((off >= 0) && (off < 8)) | ||
| 585 | return ricoh583->irq_base + RICOH583_IRQ_GPIO0 + off; | ||
| 586 | |||
| 587 | return -EIO; | ||
| 588 | } | ||
| 589 | |||
| 590 | static int ricoh583_gpio_request(struct gpio_chip *chip, unsigned offset) | ||
| 591 | { | ||
| 592 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 593 | int ret; | ||
| 594 | |||
| 595 | ret = ricoh583_clr_bits(ricoh583->dev, RICOH583_GPIO_PGSEL, | ||
| 596 | 1 << offset); | ||
| 597 | if (ret < 0) | ||
| 598 | dev_err(ricoh583->dev, "%s(): The error in writing register " | ||
| 599 | "0x%02x\n", __func__, RICOH583_GPIO_PGSEL); | ||
| 600 | return ret; | ||
| 601 | } | ||
| 602 | |||
| 603 | static void ricoh583_gpio_free(struct gpio_chip *chip, unsigned offset) | ||
| 604 | { | ||
| 605 | struct ricoh583 *ricoh583 = container_of(chip, struct ricoh583, gpio); | ||
| 606 | int ret; | ||
| 607 | |||
| 608 | ret = ricoh583_set_bits(ricoh583->dev, RICOH583_GPIO_PGSEL, | ||
| 609 | 1 << offset); | ||
| 610 | if (ret < 0) | ||
| 611 | dev_err(ricoh583->dev, "%s(): The error in writing register " | ||
| 612 | "0x%02x\n", __func__, RICOH583_GPIO_PGSEL); | ||
| 613 | } | ||
| 614 | |||
| 615 | static void __devinit ricoh583_gpio_init(struct ricoh583 *ricoh583, | ||
| 616 | struct ricoh583_platform_data *pdata) | ||
| 617 | { | ||
| 618 | int ret; | ||
| 619 | int i; | ||
| 620 | struct ricoh583_gpio_init_data *ginit; | ||
| 621 | |||
| 622 | if (pdata->gpio_base <= 0) | ||
| 623 | return; | ||
| 624 | |||
| 625 | ret = ricoh583_write(ricoh583->dev, RICOH583_GPIO_PGSEL, 0xEF); | ||
| 626 | if (ret < 0) { | ||
| 627 | dev_err(ricoh583->dev, "%s(): The error in writing register " | ||
| 628 | "0x%02x\n", __func__, RICOH583_GPIO_PGSEL); | ||
| 629 | return; | ||
| 630 | } | ||
| 631 | |||
| 632 | for (i = 0; i < pdata->num_gpioinit_data; ++i) { | ||
| 633 | ginit = &pdata->gpio_init_data[i]; | ||
| 634 | if (!ginit->init_apply) | ||
| 635 | continue; | ||
| 636 | if (ginit->pulldn_en) | ||
| 637 | ret = ricoh583_set_bits(ricoh583->dev, | ||
| 638 | RICOH583_GPIO_PDEN, 1 << i); | ||
| 639 | else | ||
| 640 | ret = ricoh583_clr_bits(ricoh583->dev, | ||
| 641 | RICOH583_GPIO_PDEN, 1 << i); | ||
| 642 | if (ret < 0) | ||
| 643 | dev_err(ricoh583->dev, "Gpio %d init " | ||
| 644 | "pden configuration failed: %d\n", i, ret); | ||
| 645 | |||
| 646 | if (ginit->output_mode_en) { | ||
| 647 | if (ginit->output_val) | ||
| 648 | ret = ricoh583_set_bits(ricoh583->dev, | ||
| 649 | RICOH583_GPIO_IOOUT, 1 << i); | ||
| 650 | else | ||
| 651 | ret = ricoh583_clr_bits(ricoh583->dev, | ||
| 652 | RICOH583_GPIO_IOOUT, 1 << i); | ||
| 653 | if (!ret) | ||
| 654 | ret = ricoh583_set_bits(ricoh583->dev, | ||
| 655 | RICOH583_GPIO_IOSEL, 1 << i); | ||
| 656 | } else | ||
| 657 | ret = ricoh583_clr_bits(ricoh583->dev, | ||
| 658 | RICOH583_GPIO_IOSEL, 1 << i); | ||
| 659 | |||
| 660 | if (ret < 0) | ||
| 661 | dev_err(ricoh583->dev, "Gpio %d init " | ||
| 662 | "dir configuration failed: %d\n", i, ret); | ||
| 663 | |||
| 664 | ret = ricoh583_clr_bits(ricoh583->dev, RICOH583_GPIO_PGSEL, | ||
| 665 | 1 << i); | ||
| 666 | if (ret < 0) | ||
| 667 | dev_err(ricoh583->dev, "%s(): The error in writing " | ||
| 668 | "register 0x%02x\n", __func__, | ||
| 669 | RICOH583_GPIO_PGSEL); | ||
| 670 | } | ||
| 671 | |||
| 672 | ricoh583->gpio.owner = THIS_MODULE; | ||
| 673 | ricoh583->gpio.label = ricoh583->client->name; | ||
| 674 | ricoh583->gpio.dev = ricoh583->dev; | ||
| 675 | ricoh583->gpio.base = pdata->gpio_base; | ||
| 676 | ricoh583->gpio.ngpio = RICOH583_NR_GPIO; | ||
| 677 | ricoh583->gpio.can_sleep = 1; | ||
| 678 | |||
| 679 | ricoh583->gpio.request = ricoh583_gpio_request; | ||
| 680 | ricoh583->gpio.free = ricoh583_gpio_free; | ||
| 681 | ricoh583->gpio.direction_input = ricoh583_gpio_input; | ||
| 682 | ricoh583->gpio.direction_output = ricoh583_gpio_output; | ||
| 683 | ricoh583->gpio.set = ricoh583_gpio_set; | ||
| 684 | ricoh583->gpio.get = ricoh583_gpio_get; | ||
| 685 | ricoh583->gpio.to_irq = ricoh583_gpio_to_irq; | ||
| 686 | |||
| 687 | ret = gpiochip_add(&ricoh583->gpio); | ||
| 688 | if (ret) | ||
| 689 | dev_warn(ricoh583->dev, "GPIO registration failed: %d\n", ret); | ||
| 690 | } | ||
| 691 | |||
| 692 | static void ricoh583_irq_lock(struct irq_data *irq_data) | ||
| 693 | { | ||
| 694 | struct ricoh583 *ricoh583 = irq_data_get_irq_chip_data(irq_data); | ||
| 695 | |||
| 696 | mutex_lock(&ricoh583->irq_lock); | ||
| 697 | } | ||
| 698 | |||
| 699 | static void ricoh583_irq_unmask(struct irq_data *irq_data) | ||
| 700 | { | ||
| 701 | struct ricoh583 *ricoh583 = irq_data_get_irq_chip_data(irq_data); | ||
| 702 | unsigned int __irq = irq_data->irq - ricoh583->irq_base; | ||
| 703 | const struct ricoh583_irq_data *data = &ricoh583_irqs[__irq]; | ||
| 704 | |||
| 705 | ricoh583->group_irq_en[data->grp_index] |= (1 << data->grp_index); | ||
| 706 | if (ricoh583->group_irq_en[data->grp_index]) | ||
| 707 | ricoh583->intc_inten_reg |= 1 << data->master_bit; | ||
| 708 | |||
| 709 | ricoh583->irq_en_reg[data->mask_reg_index] |= 1 << data->int_en_bit; | ||
| 710 | } | ||
| 711 | |||
| 712 | static void ricoh583_irq_mask(struct irq_data *irq_data) | ||
| 713 | { | ||
| 714 | struct ricoh583 *ricoh583 = irq_data_get_irq_chip_data(irq_data); | ||
| 715 | unsigned int __irq = irq_data->irq - ricoh583->irq_base; | ||
| 716 | const struct ricoh583_irq_data *data = &ricoh583_irqs[__irq]; | ||
| 717 | |||
| 718 | ricoh583->group_irq_en[data->grp_index] &= ~(1 << data->grp_index); | ||
| 719 | if (!ricoh583->group_irq_en[data->grp_index]) | ||
| 720 | ricoh583->intc_inten_reg &= ~(1 << data->master_bit); | ||
| 721 | |||
| 722 | ricoh583->irq_en_reg[data->mask_reg_index] &= ~(1 << data->int_en_bit); | ||
| 723 | } | ||
| 724 | |||
| 725 | static void ricoh583_irq_sync_unlock(struct irq_data *irq_data) | ||
| 726 | { | ||
| 727 | struct ricoh583 *ricoh583 = irq_data_get_irq_chip_data(irq_data); | ||
| 728 | int i; | ||
| 729 | |||
| 730 | for (i = 0; i < ARRAY_SIZE(ricoh583->gpedge_reg); i++) { | ||
| 731 | if (ricoh583->gpedge_reg[i] != ricoh583->gpedge_cache[i]) { | ||
| 732 | if (!WARN_ON(__ricoh583_write(ricoh583->client, | ||
| 733 | ricoh583->gpedge_add[i], | ||
| 734 | ricoh583->gpedge_reg[i]))) | ||
| 735 | ricoh583->gpedge_cache[i] = | ||
| 736 | ricoh583->gpedge_reg[i]; | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | for (i = 0; i < ARRAY_SIZE(ricoh583->irq_en_reg); i++) { | ||
| 741 | if (ricoh583->irq_en_reg[i] != ricoh583->irq_en_cache[i]) { | ||
| 742 | if (!WARN_ON(__ricoh583_write(ricoh583->client, | ||
| 743 | ricoh583->irq_en_add[i], | ||
| 744 | ricoh583->irq_en_reg[i]))) | ||
| 745 | ricoh583->irq_en_cache[i] = | ||
| 746 | ricoh583->irq_en_reg[i]; | ||
| 747 | } | ||
| 748 | } | ||
| 749 | |||
| 750 | if (ricoh583->intc_inten_reg != ricoh583->intc_inten_cache) { | ||
| 751 | if (!WARN_ON(__ricoh583_write(ricoh583->client, | ||
| 752 | RICOH583_INTC_INTEN, ricoh583->intc_inten_reg))) | ||
| 753 | ricoh583->intc_inten_cache = ricoh583->intc_inten_reg; | ||
| 754 | } | ||
| 755 | |||
| 756 | mutex_unlock(&ricoh583->irq_lock); | ||
| 757 | } | ||
| 758 | |||
| 759 | static int ricoh583_irq_set_type(struct irq_data *irq_data, unsigned int type) | ||
| 760 | { | ||
| 761 | struct ricoh583 *ricoh583 = irq_data_get_irq_chip_data(irq_data); | ||
| 762 | unsigned int __irq = irq_data->irq - ricoh583->irq_base; | ||
| 763 | const struct ricoh583_irq_data *data = &ricoh583_irqs[__irq]; | ||
| 764 | int val = 0; | ||
| 765 | int gpedge_index; | ||
| 766 | int gpedge_bit_pos; | ||
| 767 | |||
| 768 | if (data->int_type & GPIO_INT) { | ||
| 769 | gpedge_index = data->int_en_bit / 4; | ||
| 770 | gpedge_bit_pos = data->int_en_bit % 4; | ||
| 771 | |||
| 772 | if (type & IRQ_TYPE_EDGE_FALLING) | ||
| 773 | val |= 0x2; | ||
| 774 | |||
| 775 | if (type & IRQ_TYPE_EDGE_RISING) | ||
| 776 | val |= 0x1; | ||
| 777 | |||
| 778 | ricoh583->gpedge_reg[gpedge_index] &= ~(3 << gpedge_bit_pos); | ||
| 779 | ricoh583->gpedge_reg[gpedge_index] |= (val << gpedge_bit_pos); | ||
| 780 | ricoh583_irq_unmask(irq_data); | ||
| 781 | } | ||
| 782 | return 0; | ||
| 783 | } | ||
| 784 | |||
| 785 | static irqreturn_t ricoh583_irq(int irq, void *data) | ||
| 786 | { | ||
| 787 | struct ricoh583 *ricoh583 = data; | ||
| 788 | u8 int_sts[9]; | ||
| 789 | u8 master_int; | ||
| 790 | int i; | ||
| 791 | int ret; | ||
| 792 | u8 rtc_int_sts = 0; | ||
| 793 | |||
| 794 | /* Clear the status */ | ||
| 795 | for (i = 0; i < 9; i++) | ||
| 796 | int_sts[i] = 0; | ||
| 797 | |||
| 798 | ret = __ricoh583_read(ricoh583->client, RICOH583_INTC_INTMON, | ||
| 799 | &master_int); | ||
| 800 | if (ret < 0) { | ||
| 801 | dev_err(ricoh583->dev, "Error in reading reg 0x%02x " | ||
| 802 | "error: %d\n", RICOH583_INTC_INTMON, ret); | ||
| 803 | return IRQ_HANDLED; | ||
| 804 | } | ||
| 805 | |||
| 806 | for (i = 0; i < 9; ++i) { | ||
| 807 | if (!(master_int & ricoh583->main_int_type[i])) | ||
| 808 | continue; | ||
| 809 | ret = __ricoh583_read(ricoh583->client, | ||
| 810 | ricoh583->irq_mon_add[i], &int_sts[i]); | ||
| 811 | if (ret < 0) { | ||
| 812 | dev_err(ricoh583->dev, "Error in reading reg 0x%02x " | ||
| 813 | "error: %d\n", ricoh583->irq_mon_add[i], ret); | ||
| 814 | int_sts[i] = 0; | ||
| 815 | continue; | ||
| 816 | } | ||
| 817 | |||
| 818 | if (ricoh583->main_int_type[i] & RTC_INT) { | ||
| 819 | rtc_int_sts = 0; | ||
| 820 | if (int_sts[i] & 0x1) | ||
| 821 | rtc_int_sts |= BIT(6); | ||
| 822 | if (int_sts[i] & 0x2) | ||
| 823 | rtc_int_sts |= BIT(7); | ||
| 824 | if (int_sts[i] & 0x4) | ||
| 825 | rtc_int_sts |= BIT(0); | ||
| 826 | if (int_sts[i] & 0x8) | ||
| 827 | rtc_int_sts |= BIT(5); | ||
| 828 | } | ||
| 829 | |||
| 830 | ret = __ricoh583_write(ricoh583->client, | ||
| 831 | ricoh583->irq_clr_add[i], ~int_sts[i]); | ||
| 832 | if (ret < 0) { | ||
| 833 | dev_err(ricoh583->dev, "Error in reading reg 0x%02x " | ||
| 834 | "error: %d\n", ricoh583->irq_clr_add[i], ret); | ||
| 835 | } | ||
| 836 | if (ricoh583->main_int_type[i] & RTC_INT) | ||
| 837 | int_sts[i] = rtc_int_sts; | ||
| 838 | } | ||
| 839 | |||
| 840 | /* Merge gpio interrupts for rising and falling case*/ | ||
| 841 | int_sts[7] |= int_sts[8]; | ||
| 842 | |||
| 843 | /* Call interrupt handler if enabled */ | ||
| 844 | for (i = 0; i < RICOH583_NR_IRQS; ++i) { | ||
| 845 | const struct ricoh583_irq_data *data = &ricoh583_irqs[i]; | ||
| 846 | if ((int_sts[data->mask_reg_index] & (1 << data->int_en_bit)) && | ||
| 847 | (ricoh583->group_irq_en[data->master_bit] & | ||
| 848 | (1 << data->grp_index))) | ||
| 849 | handle_nested_irq(ricoh583->irq_base + i); | ||
| 850 | } | ||
| 851 | return IRQ_HANDLED; | ||
| 852 | } | ||
| 853 | |||
| 854 | static int __devinit ricoh583_irq_init(struct ricoh583 *ricoh583, int irq, | ||
| 855 | int irq_base) | ||
| 856 | { | ||
| 857 | int i, ret; | ||
| 858 | |||
| 859 | if (!irq_base) { | ||
| 860 | dev_warn(ricoh583->dev, "No interrupt support on IRQ base\n"); | ||
| 861 | return -EINVAL; | ||
| 862 | } | ||
| 863 | |||
| 864 | mutex_init(&ricoh583->irq_lock); | ||
| 865 | |||
| 866 | /* Initialize all locals to 0 */ | ||
| 867 | for (i = 0; i < MAX_INTERRUPT_MASKS; i++) { | ||
| 868 | ricoh583->irq_en_cache[i] = 0; | ||
| 869 | ricoh583->irq_en_reg[i] = 0; | ||
| 870 | } | ||
| 871 | ricoh583->intc_inten_cache = 0; | ||
| 872 | ricoh583->intc_inten_reg = 0; | ||
| 873 | for (i = 0; i < 2; i++) { | ||
| 874 | ricoh583->gpedge_cache[i] = 0; | ||
| 875 | ricoh583->gpedge_reg[i] = 0; | ||
| 876 | } | ||
| 877 | |||
| 878 | /* Interrupt enable register */ | ||
| 879 | ricoh583->gpedge_add[0] = RICOH583_GPIO_GPEDGE2; | ||
| 880 | ricoh583->gpedge_add[1] = RICOH583_GPIO_GPEDGE1; | ||
| 881 | ricoh583->irq_en_add[0] = RICOH583_INT_EN_SYS1; | ||
| 882 | ricoh583->irq_en_add[1] = RICOH583_INT_EN_SYS2; | ||
| 883 | ricoh583->irq_en_add[2] = RICOH583_INT_EN_DCDC; | ||
| 884 | ricoh583->irq_en_add[3] = RICOH583_INT_EN_RTC; | ||
| 885 | ricoh583->irq_en_add[4] = RICOH583_INT_EN_ADC1; | ||
| 886 | ricoh583->irq_en_add[5] = RICOH583_INT_EN_ADC2; | ||
| 887 | ricoh583->irq_en_add[6] = RICOH583_INT_EN_ADC3; | ||
| 888 | ricoh583->irq_en_add[7] = RICOH583_INT_EN_GPIO; | ||
| 889 | |||
| 890 | /* Interrupt status monitor register */ | ||
| 891 | ricoh583->irq_mon_add[0] = RICOH583_INT_MON_SYS1; | ||
| 892 | ricoh583->irq_mon_add[1] = RICOH583_INT_MON_SYS2; | ||
| 893 | ricoh583->irq_mon_add[2] = RICOH583_INT_MON_DCDC; | ||
| 894 | ricoh583->irq_mon_add[3] = RICOH583_INT_MON_RTC; | ||
| 895 | ricoh583->irq_mon_add[4] = RICOH583_INT_IR_ADCL; | ||
| 896 | ricoh583->irq_mon_add[5] = RICOH583_INT_IR_ADCH; | ||
| 897 | ricoh583->irq_mon_add[6] = RICOH583_INT_IR_ADCEND; | ||
| 898 | ricoh583->irq_mon_add[7] = RICOH583_INT_IR_GPIOF; | ||
| 899 | ricoh583->irq_mon_add[8] = RICOH583_INT_IR_GPIOR; | ||
| 900 | |||
| 901 | /* Interrupt status clear register */ | ||
| 902 | ricoh583->irq_clr_add[0] = RICOH583_INT_IR_SYS1; | ||
| 903 | ricoh583->irq_clr_add[1] = RICOH583_INT_IR_SYS2; | ||
| 904 | ricoh583->irq_clr_add[2] = RICOH583_INT_IR_DCDC; | ||
| 905 | ricoh583->irq_clr_add[3] = RICOH583_INT_IR_RTC; | ||
| 906 | ricoh583->irq_clr_add[4] = RICOH583_INT_IR_ADCL; | ||
| 907 | ricoh583->irq_clr_add[5] = RICOH583_INT_IR_ADCH; | ||
| 908 | ricoh583->irq_clr_add[6] = RICOH583_INT_IR_ADCEND; | ||
| 909 | ricoh583->irq_clr_add[7] = RICOH583_INT_IR_GPIOF; | ||
| 910 | ricoh583->irq_clr_add[8] = RICOH583_INT_IR_GPIOR; | ||
| 911 | |||
| 912 | ricoh583->main_int_type[0] = SYS_INT; | ||
| 913 | ricoh583->main_int_type[1] = SYS_INT; | ||
| 914 | ricoh583->main_int_type[2] = DCDC_INT; | ||
| 915 | ricoh583->main_int_type[3] = RTC_INT; | ||
| 916 | ricoh583->main_int_type[4] = ADC_INT; | ||
| 917 | ricoh583->main_int_type[5] = ADC_INT; | ||
| 918 | ricoh583->main_int_type[6] = ADC_INT; | ||
| 919 | ricoh583->main_int_type[7] = GPIO_INT; | ||
| 920 | ricoh583->main_int_type[8] = GPIO_INT; | ||
| 921 | |||
| 922 | /* Initailize all int register to 0 */ | ||
| 923 | for (i = 0; i < MAX_INTERRUPT_MASKS; i++) { | ||
| 924 | ret = __ricoh583_write(ricoh583->client, | ||
| 925 | ricoh583->irq_en_add[i], | ||
| 926 | ricoh583->irq_en_reg[i]); | ||
| 927 | if (ret < 0) | ||
| 928 | dev_err(ricoh583->dev, "Error in writing reg 0x%02x " | ||
| 929 | "error: %d\n", ricoh583->irq_en_add[i], ret); | ||
| 930 | } | ||
| 931 | |||
| 932 | for (i = 0; i < 2; i++) { | ||
| 933 | ret = __ricoh583_write(ricoh583->client, | ||
| 934 | ricoh583->gpedge_add[i], | ||
| 935 | ricoh583->gpedge_reg[i]); | ||
| 936 | if (ret < 0) | ||
| 937 | dev_err(ricoh583->dev, "Error in writing reg 0x%02x " | ||
| 938 | "error: %d\n", ricoh583->gpedge_add[i], ret); | ||
| 939 | } | ||
| 940 | |||
| 941 | ret = __ricoh583_write(ricoh583->client, RICOH583_INTC_INTEN, 0x0); | ||
| 942 | if (ret < 0) | ||
| 943 | dev_err(ricoh583->dev, "Error in writing reg 0x%02x " | ||
| 944 | "error: %d\n", RICOH583_INTC_INTEN, ret); | ||
| 945 | |||
| 946 | /* Clear all interrupts in case they woke up active. */ | ||
| 947 | for (i = 0; i < 9; i++) { | ||
| 948 | ret = __ricoh583_write(ricoh583->client, | ||
| 949 | ricoh583->irq_clr_add[i], 0); | ||
| 950 | if (ret < 0) | ||
| 951 | dev_err(ricoh583->dev, "Error in writing reg 0x%02x " | ||
| 952 | "error: %d\n", ricoh583->irq_clr_add[i], ret); | ||
| 953 | } | ||
| 954 | |||
| 955 | ricoh583->irq_base = irq_base; | ||
| 956 | ricoh583->irq_chip.name = "ricoh583"; | ||
| 957 | ricoh583->irq_chip.irq_mask = ricoh583_irq_mask; | ||
| 958 | ricoh583->irq_chip.irq_unmask = ricoh583_irq_unmask; | ||
| 959 | ricoh583->irq_chip.irq_bus_lock = ricoh583_irq_lock; | ||
| 960 | ricoh583->irq_chip.irq_bus_sync_unlock = ricoh583_irq_sync_unlock; | ||
| 961 | ricoh583->irq_chip.irq_set_type = ricoh583_irq_set_type; | ||
| 962 | |||
| 963 | for (i = 0; i < RICOH583_NR_IRQS; i++) { | ||
| 964 | int __irq = i + ricoh583->irq_base; | ||
| 965 | irq_set_chip_data(__irq, ricoh583); | ||
| 966 | irq_set_chip_and_handler(__irq, &ricoh583->irq_chip, | ||
| 967 | handle_simple_irq); | ||
| 968 | irq_set_nested_thread(__irq, 1); | ||
| 969 | #ifdef CONFIG_ARM | ||
| 970 | set_irq_flags(__irq, IRQF_VALID); | ||
| 971 | #endif | ||
| 972 | } | ||
| 973 | |||
| 974 | ret = request_threaded_irq(irq, NULL, ricoh583_irq, IRQF_ONESHOT, | ||
| 975 | "ricoh583", ricoh583); | ||
| 976 | if (ret < 0) | ||
| 977 | dev_err(ricoh583->dev, "Error in registering interrupt " | ||
| 978 | "error: %d\n", ret); | ||
| 979 | if (!ret) { | ||
| 980 | device_init_wakeup(ricoh583->dev, 1); | ||
| 981 | enable_irq_wake(irq); | ||
| 982 | } | ||
| 983 | return ret; | ||
| 984 | } | ||
| 985 | |||
| 986 | static int ricoh583_remove_subdev(struct device *dev, void *unused) | ||
| 987 | { | ||
| 988 | platform_device_unregister(to_platform_device(dev)); | ||
| 989 | return 0; | ||
| 990 | } | ||
| 991 | |||
| 992 | static int ricoh583_remove_subdevs(struct ricoh583 *ricoh583) | ||
| 993 | { | ||
| 994 | return device_for_each_child(ricoh583->dev, NULL, | ||
| 995 | ricoh583_remove_subdev); | ||
| 996 | } | ||
| 997 | |||
| 998 | static int __devinit ricoh583_add_subdevs(struct ricoh583 *ricoh583, | ||
| 999 | struct ricoh583_platform_data *pdata) | ||
| 1000 | { | ||
| 1001 | struct ricoh583_subdev_info *subdev; | ||
| 1002 | struct platform_device *pdev; | ||
| 1003 | int i, ret = 0; | ||
| 1004 | |||
| 1005 | for (i = 0; i < pdata->num_subdevs; i++) { | ||
| 1006 | subdev = &pdata->subdevs[i]; | ||
| 1007 | |||
| 1008 | pdev = platform_device_alloc(subdev->name, subdev->id); | ||
| 1009 | |||
| 1010 | pdev->dev.parent = ricoh583->dev; | ||
| 1011 | pdev->dev.platform_data = subdev->platform_data; | ||
| 1012 | |||
| 1013 | ret = platform_device_add(pdev); | ||
| 1014 | if (ret) | ||
| 1015 | goto failed; | ||
| 1016 | } | ||
| 1017 | return 0; | ||
| 1018 | |||
| 1019 | failed: | ||
| 1020 | ricoh583_remove_subdevs(ricoh583); | ||
| 1021 | return ret; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | #ifdef CONFIG_DEBUG_FS | ||
| 1025 | #include <linux/debugfs.h> | ||
| 1026 | #include <linux/seq_file.h> | ||
| 1027 | static void print_regs(const char *header, struct seq_file *s, | ||
| 1028 | struct i2c_client *client, int start_offset, | ||
| 1029 | int end_offset) | ||
| 1030 | { | ||
| 1031 | uint8_t reg_val; | ||
| 1032 | int i; | ||
| 1033 | int ret; | ||
| 1034 | |||
| 1035 | seq_printf(s, "%s\n", header); | ||
| 1036 | for (i = start_offset; i <= end_offset; ++i) { | ||
| 1037 | ret = __ricoh583_read(client, i, ®_val); | ||
| 1038 | if (ret >= 0) | ||
| 1039 | seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val); | ||
| 1040 | } | ||
| 1041 | seq_printf(s, "------------------\n"); | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | static int dbg_tps_show(struct seq_file *s, void *unused) | ||
| 1045 | { | ||
| 1046 | struct ricoh583 *tps = s->private; | ||
| 1047 | struct i2c_client *client = tps->client; | ||
| 1048 | |||
| 1049 | seq_printf(s, "RICOH583 Registers\n"); | ||
| 1050 | seq_printf(s, "------------------\n"); | ||
| 1051 | |||
| 1052 | print_regs("System Regs", s, client, 0x0, 0xF); | ||
| 1053 | print_regs("Power Control Regs", s, client, 0x10, 0x2B); | ||
| 1054 | print_regs("DCDC1 Regs", s, client, 0x30, 0x43); | ||
| 1055 | print_regs("DCDC1 Regs", s, client, 0x60, 0x63); | ||
| 1056 | print_regs("LDO Regs", s, client, 0x50, 0x5F); | ||
| 1057 | print_regs("LDO Regs", s, client, 0x64, 0x6D); | ||
| 1058 | print_regs("ADC Regs", s, client, 0x70, 0x72); | ||
| 1059 | print_regs("ADC Regs", s, client, 0x74, 0x8B); | ||
| 1060 | print_regs("ADC Regs", s, client, 0x90, 0x96); | ||
| 1061 | print_regs("GPIO Regs", s, client, 0xA0, 0xAC); | ||
| 1062 | print_regs("INTC Regs", s, client, 0xAD, 0xAF); | ||
| 1063 | print_regs("RTC Regs", s, client, 0xE0, 0xEE); | ||
| 1064 | print_regs("RTC Regs", s, client, 0xF0, 0xF4); | ||
| 1065 | return 0; | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | static int dbg_tps_open(struct inode *inode, struct file *file) | ||
| 1069 | { | ||
| 1070 | return single_open(file, dbg_tps_show, inode->i_private); | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | static const struct file_operations debug_fops = { | ||
| 1074 | .open = dbg_tps_open, | ||
| 1075 | .read = seq_read, | ||
| 1076 | .llseek = seq_lseek, | ||
| 1077 | .release = single_release, | ||
| 1078 | }; | ||
| 1079 | static void __init ricoh583_debuginit(struct ricoh583 *tps) | ||
| 1080 | { | ||
| 1081 | (void)debugfs_create_file("ricoh583", S_IRUGO, NULL, | ||
| 1082 | tps, &debug_fops); | ||
| 1083 | } | ||
| 1084 | #else | ||
| 1085 | static void __init ricoh583_debuginit(struct ricoh583 *tpsi) | ||
| 1086 | { | ||
| 1087 | return; | ||
| 1088 | } | ||
| 1089 | #endif | ||
| 1090 | |||
| 1091 | static int ricoh583_i2c_probe(struct i2c_client *i2c, | ||
| 1092 | const struct i2c_device_id *id) | ||
| 1093 | { | ||
| 1094 | struct ricoh583 *ricoh583; | ||
| 1095 | struct ricoh583_platform_data *pdata = i2c->dev.platform_data; | ||
| 1096 | int ret; | ||
| 1097 | |||
| 1098 | ricoh583 = kzalloc(sizeof(struct ricoh583), GFP_KERNEL); | ||
| 1099 | if (ricoh583 == NULL) | ||
| 1100 | return -ENOMEM; | ||
| 1101 | |||
| 1102 | ricoh583->client = i2c; | ||
| 1103 | ricoh583->dev = &i2c->dev; | ||
| 1104 | i2c_set_clientdata(i2c, ricoh583); | ||
| 1105 | |||
| 1106 | mutex_init(&ricoh583->io_lock); | ||
| 1107 | |||
| 1108 | ret = ricoh583_ext_power_init(ricoh583, pdata); | ||
| 1109 | if (ret < 0) | ||
| 1110 | goto err_irq_init; | ||
| 1111 | |||
| 1112 | if (i2c->irq) { | ||
| 1113 | ret = ricoh583_irq_init(ricoh583, i2c->irq, pdata->irq_base); | ||
| 1114 | if (ret) { | ||
| 1115 | dev_err(&i2c->dev, "IRQ init failed: %d\n", ret); | ||
| 1116 | goto err_irq_init; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | ret = ricoh583_add_subdevs(ricoh583, pdata); | ||
| 1121 | if (ret) { | ||
| 1122 | dev_err(&i2c->dev, "add devices failed: %d\n", ret); | ||
| 1123 | goto err_add_devs; | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | ricoh583_gpio_init(ricoh583, pdata); | ||
| 1127 | |||
| 1128 | ricoh583_debuginit(ricoh583); | ||
| 1129 | |||
| 1130 | ricoh583_i2c_client = i2c; | ||
| 1131 | return 0; | ||
| 1132 | |||
| 1133 | err_add_devs: | ||
| 1134 | if (i2c->irq) | ||
| 1135 | free_irq(i2c->irq, ricoh583); | ||
| 1136 | err_irq_init: | ||
| 1137 | kfree(ricoh583); | ||
| 1138 | return ret; | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | static int __devexit ricoh583_i2c_remove(struct i2c_client *i2c) | ||
| 1142 | { | ||
| 1143 | struct ricoh583 *ricoh583 = i2c_get_clientdata(i2c); | ||
| 1144 | |||
| 1145 | if (i2c->irq) | ||
| 1146 | free_irq(i2c->irq, ricoh583); | ||
| 1147 | |||
| 1148 | ricoh583_remove_subdevs(ricoh583); | ||
| 1149 | kfree(ricoh583); | ||
| 1150 | return 0; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | #ifdef CONFIG_PM | ||
| 1154 | static int ricoh583_i2c_suspend(struct i2c_client *i2c, pm_message_t state) | ||
| 1155 | { | ||
| 1156 | if (i2c->irq) | ||
| 1157 | disable_irq(i2c->irq); | ||
| 1158 | return 0; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | |||
| 1162 | static int ricoh583_i2c_resume(struct i2c_client *i2c) | ||
| 1163 | { | ||
| 1164 | if (i2c->irq) | ||
| 1165 | enable_irq(i2c->irq); | ||
| 1166 | return 0; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | #endif | ||
| 1170 | |||
| 1171 | static const struct i2c_device_id ricoh583_i2c_id[] = { | ||
| 1172 | {"ricoh583", 0}, | ||
| 1173 | {} | ||
| 1174 | }; | ||
| 1175 | |||
| 1176 | MODULE_DEVICE_TABLE(i2c, ricoh583_i2c_id); | ||
| 1177 | |||
| 1178 | static struct i2c_driver ricoh583_i2c_driver = { | ||
| 1179 | .driver = { | ||
| 1180 | .name = "ricoh583", | ||
| 1181 | .owner = THIS_MODULE, | ||
| 1182 | }, | ||
| 1183 | .probe = ricoh583_i2c_probe, | ||
| 1184 | .remove = __devexit_p(ricoh583_i2c_remove), | ||
| 1185 | #ifdef CONFIG_PM | ||
| 1186 | .suspend = ricoh583_i2c_suspend, | ||
| 1187 | .resume = ricoh583_i2c_resume, | ||
| 1188 | #endif | ||
| 1189 | .id_table = ricoh583_i2c_id, | ||
| 1190 | }; | ||
| 1191 | |||
| 1192 | |||
| 1193 | static int __init ricoh583_i2c_init(void) | ||
| 1194 | { | ||
| 1195 | int ret = -ENODEV; | ||
| 1196 | ret = i2c_add_driver(&ricoh583_i2c_driver); | ||
| 1197 | if (ret != 0) | ||
| 1198 | pr_err("Failed to register I2C driver: %d\n", ret); | ||
| 1199 | |||
| 1200 | return ret; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | subsys_initcall(ricoh583_i2c_init); | ||
| 1204 | |||
| 1205 | static void __exit ricoh583_i2c_exit(void) | ||
| 1206 | { | ||
| 1207 | i2c_del_driver(&ricoh583_i2c_driver); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | module_exit(ricoh583_i2c_exit); | ||
| 1211 | |||
| 1212 | MODULE_DESCRIPTION("RICOH583 multi-function core driver"); | ||
| 1213 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mfd/tps65910-irq.c b/drivers/mfd/tps65910-irq.c new file mode 100644 index 00000000000..c9ed5c00a62 --- /dev/null +++ b/drivers/mfd/tps65910-irq.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | /* | ||
| 2 | * tps65910-irq.c -- TI TPS6591x | ||
| 3 | * | ||
| 4 | * Copyright 2010 Texas Instruments Inc. | ||
| 5 | * | ||
| 6 | * Author: Graeme Gregory <gg@slimlogic.co.uk> | ||
| 7 | * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify it | ||
| 10 | * under the terms of the GNU General Public License as published by the | ||
| 11 | * Free Software Foundation; either version 2 of the License, or (at your | ||
| 12 | * option) any later version. | ||
| 13 | * | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include <linux/kernel.h> | ||
| 17 | #include <linux/module.h> | ||
| 18 | #include <linux/init.h> | ||
| 19 | #include <linux/bug.h> | ||
| 20 | #include <linux/device.h> | ||
| 21 | #include <linux/interrupt.h> | ||
| 22 | #include <linux/irq.h> | ||
| 23 | #include <linux/gpio.h> | ||
| 24 | #include <linux/mfd/tps65910.h> | ||
| 25 | |||
| 26 | static inline int irq_to_tps65910_irq(struct tps65910 *tps65910, | ||
| 27 | int irq) | ||
| 28 | { | ||
| 29 | return (irq - tps65910->irq_base); | ||
| 30 | } | ||
| 31 | |||
| 32 | /* | ||
| 33 | * This is a threaded IRQ handler so can access I2C/SPI. Since all | ||
| 34 | * interrupts are clear on read the IRQ line will be reasserted and | ||
| 35 | * the physical IRQ will be handled again if another interrupt is | ||
| 36 | * asserted while we run - in the normal course of events this is a | ||
| 37 | * rare occurrence so we save I2C/SPI reads. We're also assuming that | ||
| 38 | * it's rare to get lots of interrupts firing simultaneously so try to | ||
| 39 | * minimise I/O. | ||
| 40 | */ | ||
| 41 | static irqreturn_t tps65910_irq(int irq, void *irq_data) | ||
| 42 | { | ||
| 43 | struct tps65910 *tps65910 = irq_data; | ||
| 44 | u32 irq_sts; | ||
| 45 | u32 irq_mask; | ||
| 46 | u8 reg; | ||
| 47 | int i; | ||
| 48 | |||
| 49 | tps65910->read(tps65910, TPS65910_INT_STS, 1, ®); | ||
| 50 | irq_sts = reg; | ||
| 51 | tps65910->read(tps65910, TPS65910_INT_STS2, 1, ®); | ||
| 52 | irq_sts |= reg << 8; | ||
| 53 | switch (tps65910_chip_id(tps65910)) { | ||
| 54 | case TPS65911: | ||
| 55 | tps65910->read(tps65910, TPS65910_INT_STS3, 1, ®); | ||
| 56 | irq_sts |= reg << 16; | ||
| 57 | } | ||
| 58 | |||
| 59 | tps65910->read(tps65910, TPS65910_INT_MSK, 1, ®); | ||
| 60 | irq_mask = reg; | ||
| 61 | tps65910->read(tps65910, TPS65910_INT_MSK2, 1, ®); | ||
| 62 | irq_mask |= reg << 8; | ||
| 63 | switch (tps65910_chip_id(tps65910)) { | ||
| 64 | case TPS65911: | ||
| 65 | tps65910->read(tps65910, TPS65910_INT_MSK3, 1, ®); | ||
| 66 | irq_mask |= reg << 16; | ||
| 67 | } | ||
| 68 | |||
| 69 | irq_sts &= ~irq_mask; | ||
| 70 | |||
| 71 | if (!irq_sts) | ||
| 72 | return IRQ_NONE; | ||
| 73 | |||
| 74 | for (i = 0; i < tps65910->irq_num; i++) { | ||
| 75 | |||
| 76 | if (!(irq_sts & (1 << i))) | ||
| 77 | continue; | ||
| 78 | |||
| 79 | handle_nested_irq(tps65910->irq_base + i); | ||
| 80 | } | ||
| 81 | |||
| 82 | /* Write the STS register back to clear IRQs we handled */ | ||
| 83 | reg = irq_sts & 0xFF; | ||
| 84 | irq_sts >>= 8; | ||
| 85 | tps65910->write(tps65910, TPS65910_INT_STS, 1, ®); | ||
| 86 | reg = irq_sts & 0xFF; | ||
| 87 | tps65910->write(tps65910, TPS65910_INT_STS2, 1, ®); | ||
| 88 | switch (tps65910_chip_id(tps65910)) { | ||
| 89 | case TPS65911: | ||
| 90 | reg = irq_sts >> 8; | ||
| 91 | tps65910->write(tps65910, TPS65910_INT_STS3, 1, ®); | ||
| 92 | } | ||
| 93 | |||
| 94 | return IRQ_HANDLED; | ||
| 95 | } | ||
| 96 | |||
| 97 | static void tps65910_irq_lock(struct irq_data *data) | ||
| 98 | { | ||
| 99 | struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data); | ||
| 100 | |||
| 101 | mutex_lock(&tps65910->irq_lock); | ||
| 102 | } | ||
| 103 | |||
| 104 | static void tps65910_irq_sync_unlock(struct irq_data *data) | ||
| 105 | { | ||
| 106 | struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data); | ||
| 107 | u32 reg_mask; | ||
| 108 | u8 reg; | ||
| 109 | |||
| 110 | tps65910->read(tps65910, TPS65910_INT_MSK, 1, ®); | ||
| 111 | reg_mask = reg; | ||
| 112 | tps65910->read(tps65910, TPS65910_INT_MSK2, 1, ®); | ||
| 113 | reg_mask |= reg << 8; | ||
| 114 | switch (tps65910_chip_id(tps65910)) { | ||
| 115 | case TPS65911: | ||
| 116 | tps65910->read(tps65910, TPS65910_INT_MSK3, 1, ®); | ||
| 117 | reg_mask |= reg << 16; | ||
| 118 | } | ||
| 119 | |||
| 120 | if (tps65910->irq_mask != reg_mask) { | ||
| 121 | reg = tps65910->irq_mask & 0xFF; | ||
| 122 | tps65910->write(tps65910, TPS65910_INT_MSK, 1, ®); | ||
| 123 | reg = tps65910->irq_mask >> 8 & 0xFF; | ||
| 124 | tps65910->write(tps65910, TPS65910_INT_MSK2, 1, ®); | ||
| 125 | switch (tps65910_chip_id(tps65910)) { | ||
| 126 | case TPS65911: | ||
| 127 | reg = tps65910->irq_mask >> 16; | ||
| 128 | tps65910->write(tps65910, TPS65910_INT_MSK3, 1, ®); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | mutex_unlock(&tps65910->irq_lock); | ||
| 132 | } | ||
| 133 | |||
| 134 | static void tps65910_irq_enable(struct irq_data *data) | ||
| 135 | { | ||
| 136 | struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data); | ||
| 137 | |||
| 138 | tps65910->irq_mask &= ~( 1 << irq_to_tps65910_irq(tps65910, data->irq)); | ||
| 139 | } | ||
| 140 | |||
| 141 | static void tps65910_irq_disable(struct irq_data *data) | ||
| 142 | { | ||
| 143 | struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data); | ||
| 144 | |||
| 145 | tps65910->irq_mask |= ( 1 << irq_to_tps65910_irq(tps65910, data->irq)); | ||
| 146 | } | ||
| 147 | |||
| 148 | #ifdef CONFIG_PM_SLEEP | ||
| 149 | static int tps65910_irq_set_wake(struct irq_data *data, unsigned int enable) | ||
| 150 | { | ||
| 151 | struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data); | ||
| 152 | return irq_set_irq_wake(tps65910->chip_irq, enable); | ||
| 153 | } | ||
| 154 | #else | ||
| 155 | #define tps65910_irq_set_wake NULL | ||
| 156 | #endif | ||
| 157 | |||
| 158 | static struct irq_chip tps65910_irq_chip = { | ||
| 159 | .name = "tps65910", | ||
| 160 | .irq_bus_lock = tps65910_irq_lock, | ||
| 161 | .irq_bus_sync_unlock = tps65910_irq_sync_unlock, | ||
| 162 | .irq_disable = tps65910_irq_disable, | ||
| 163 | .irq_enable = tps65910_irq_enable, | ||
| 164 | .irq_set_wake = tps65910_irq_set_wake, | ||
| 165 | }; | ||
| 166 | |||
| 167 | int tps65910_irq_init(struct tps65910 *tps65910, int irq, | ||
| 168 | struct tps65910_platform_data *pdata) | ||
| 169 | { | ||
| 170 | int ret, cur_irq; | ||
| 171 | int flags = IRQF_ONESHOT; | ||
| 172 | |||
| 173 | if (!irq) { | ||
| 174 | dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n"); | ||
| 175 | return -EINVAL; | ||
| 176 | } | ||
| 177 | |||
| 178 | if (!pdata || !pdata->irq_base) { | ||
| 179 | dev_warn(tps65910->dev, "No interrupt support, no IRQ base\n"); | ||
| 180 | return -EINVAL; | ||
| 181 | } | ||
| 182 | |||
| 183 | tps65910->irq_mask = 0xFFFFFF; | ||
| 184 | |||
| 185 | mutex_init(&tps65910->irq_lock); | ||
| 186 | tps65910->chip_irq = irq; | ||
| 187 | tps65910->irq_base = pdata->irq_base; | ||
| 188 | |||
| 189 | switch (tps65910_chip_id(tps65910)) { | ||
| 190 | case TPS65910: | ||
| 191 | tps65910->irq_num = TPS65910_NUM_IRQ; | ||
| 192 | break; | ||
| 193 | case TPS65911: | ||
| 194 | tps65910->irq_num = TPS65911_NUM_IRQ; | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* Register with genirq */ | ||
| 199 | for (cur_irq = tps65910->irq_base; | ||
| 200 | cur_irq < tps65910->irq_num + tps65910->irq_base; | ||
| 201 | cur_irq++) { | ||
| 202 | irq_set_chip_data(cur_irq, tps65910); | ||
| 203 | irq_set_chip_and_handler(cur_irq, &tps65910_irq_chip, | ||
| 204 | handle_edge_irq); | ||
| 205 | irq_set_nested_thread(cur_irq, 1); | ||
| 206 | |||
| 207 | /* ARM needs us to explicitly flag the IRQ as valid | ||
| 208 | * and will set them noprobe when we do so. */ | ||
| 209 | #ifdef CONFIG_ARM | ||
| 210 | set_irq_flags(cur_irq, IRQF_VALID); | ||
| 211 | #else | ||
| 212 | irq_set_noprobe(cur_irq); | ||
| 213 | #endif | ||
| 214 | } | ||
| 215 | |||
| 216 | ret = request_threaded_irq(irq, NULL, tps65910_irq, flags, | ||
| 217 | "tps65910", tps65910); | ||
| 218 | |||
| 219 | irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW); | ||
| 220 | |||
| 221 | if (ret != 0) | ||
| 222 | dev_err(tps65910->dev, "Failed to request IRQ: %d\n", ret); | ||
| 223 | |||
| 224 | return ret; | ||
| 225 | } | ||
| 226 | |||
| 227 | int tps65910_irq_exit(struct tps65910 *tps65910) | ||
| 228 | { | ||
| 229 | if (tps65910->chip_irq) | ||
| 230 | free_irq(tps65910->chip_irq, tps65910); | ||
| 231 | return 0; | ||
| 232 | } | ||
diff --git a/drivers/mfd/tps6591x.c b/drivers/mfd/tps6591x.c new file mode 100644 index 00000000000..bd60e09c316 --- /dev/null +++ b/drivers/mfd/tps6591x.c | |||
| @@ -0,0 +1,930 @@ | |||
| 1 | /* | ||
| 2 | * driver/mfd/tps6591x.c | ||
| 3 | * | ||
| 4 | * Core driver for TI TPS6591x PMIC family | ||
| 5 | * | ||
| 6 | * Copyright (C) 2011 NVIDIA Corporation | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License as published by | ||
| 10 | * the Free Software Foundation; either version 2 of the License, or | ||
| 11 | * (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 16 | * more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU General Public License along | ||
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 21 | * | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <linux/interrupt.h> | ||
| 25 | #include <linux/irq.h> | ||
| 26 | #include <linux/kernel.h> | ||
| 27 | #include <linux/module.h> | ||
| 28 | #include <linux/mutex.h> | ||
| 29 | #include <linux/slab.h> | ||
| 30 | #include <linux/gpio.h> | ||
| 31 | #include <linux/i2c.h> | ||
| 32 | |||
| 33 | #include <linux/mfd/core.h> | ||
| 34 | #include <linux/mfd/tps6591x.h> | ||
| 35 | |||
| 36 | /* device control registers */ | ||
| 37 | #define TPS6591X_DEVCTRL 0x3F | ||
| 38 | #define DEVCTRL_PWR_OFF_SEQ (1 << 7) | ||
| 39 | #define DEVCTRL_DEV_ON (1 << 2) | ||
| 40 | #define DEVCTRL_DEV_SLP (1 << 1) | ||
| 41 | #define TPS6591X_DEVCTRL2 0x40 | ||
| 42 | |||
| 43 | /* device sleep on registers */ | ||
| 44 | #define TPS6591X_SLEEP_KEEP_ON 0x42 | ||
| 45 | #define SLEEP_KEEP_ON_THERM (1 << 7) | ||
| 46 | #define SLEEP_KEEP_ON_CLKOUT32K (1 << 6) | ||
| 47 | #define SLEEP_KEEP_ON_VRTC (1 << 5) | ||
| 48 | #define SLEEP_KEEP_ON_I2CHS (1 << 4) | ||
| 49 | |||
| 50 | /* interrupt status registers */ | ||
| 51 | #define TPS6591X_INT_STS 0x50 | ||
| 52 | #define TPS6591X_INT_STS2 0x52 | ||
| 53 | #define TPS6591X_INT_STS3 0x54 | ||
| 54 | |||
| 55 | /* interrupt mask registers */ | ||
| 56 | #define TPS6591X_INT_MSK 0x51 | ||
| 57 | #define TPS6591X_INT_MSK2 0x53 | ||
| 58 | #define TPS6591X_INT_MSK3 0x55 | ||
| 59 | |||
| 60 | /* GPIO register base address */ | ||
| 61 | #define TPS6591X_GPIO_BASE_ADDR 0x60 | ||
| 62 | |||
| 63 | /* silicon version number */ | ||
| 64 | #define TPS6591X_VERNUM 0x80 | ||
| 65 | |||
| 66 | #define TPS6591X_GPIO_SLEEP 7 | ||
| 67 | #define TPS6591X_GPIO_PDEN 3 | ||
| 68 | #define TPS6591X_GPIO_DIR 2 | ||
| 69 | |||
| 70 | enum irq_type { | ||
| 71 | EVENT, | ||
| 72 | GPIO, | ||
| 73 | }; | ||
| 74 | |||
| 75 | struct tps6591x_irq_data { | ||
| 76 | u8 mask_reg; | ||
| 77 | u8 mask_pos; | ||
| 78 | enum irq_type type; | ||
| 79 | }; | ||
| 80 | |||
| 81 | #define TPS6591X_IRQ(_reg, _mask_pos, _type) \ | ||
| 82 | { \ | ||
| 83 | .mask_reg = (_reg), \ | ||
| 84 | .mask_pos = (_mask_pos), \ | ||
| 85 | .type = (_type), \ | ||
| 86 | } | ||
| 87 | |||
| 88 | static const struct tps6591x_irq_data tps6591x_irqs[] = { | ||
| 89 | [TPS6591X_INT_PWRHOLD_F] = TPS6591X_IRQ(0, 0, EVENT), | ||
| 90 | [TPS6591X_INT_VMBHI] = TPS6591X_IRQ(0, 1, EVENT), | ||
| 91 | [TPS6591X_INT_PWRON] = TPS6591X_IRQ(0, 2, EVENT), | ||
| 92 | [TPS6591X_INT_PWRON_LP] = TPS6591X_IRQ(0, 3, EVENT), | ||
| 93 | [TPS6591X_INT_PWRHOLD_R] = TPS6591X_IRQ(0, 4, EVENT), | ||
| 94 | [TPS6591X_INT_HOTDIE] = TPS6591X_IRQ(0, 5, EVENT), | ||
| 95 | [TPS6591X_INT_RTC_ALARM] = TPS6591X_IRQ(0, 6, EVENT), | ||
| 96 | [TPS6591X_INT_RTC_PERIOD] = TPS6591X_IRQ(0, 7, EVENT), | ||
| 97 | [TPS6591X_INT_GPIO0] = TPS6591X_IRQ(1, 0, GPIO), | ||
| 98 | [TPS6591X_INT_GPIO1] = TPS6591X_IRQ(1, 2, GPIO), | ||
| 99 | [TPS6591X_INT_GPIO2] = TPS6591X_IRQ(1, 4, GPIO), | ||
| 100 | [TPS6591X_INT_GPIO3] = TPS6591X_IRQ(1, 6, GPIO), | ||
| 101 | [TPS6591X_INT_GPIO4] = TPS6591X_IRQ(2, 0, GPIO), | ||
| 102 | [TPS6591X_INT_GPIO5] = TPS6591X_IRQ(2, 2, GPIO), | ||
| 103 | [TPS6591X_INT_WTCHDG] = TPS6591X_IRQ(2, 4, EVENT), | ||
| 104 | [TPS6591X_INT_VMBCH2_H] = TPS6591X_IRQ(2, 5, EVENT), | ||
| 105 | [TPS6591X_INT_VMBCH2_L] = TPS6591X_IRQ(2, 6, EVENT), | ||
| 106 | [TPS6591X_INT_PWRDN] = TPS6591X_IRQ(2, 7, EVENT), | ||
| 107 | }; | ||
| 108 | |||
| 109 | struct tps6591x { | ||
| 110 | struct mutex lock; | ||
| 111 | struct device *dev; | ||
| 112 | struct i2c_client *client; | ||
| 113 | |||
| 114 | struct gpio_chip gpio; | ||
| 115 | struct irq_chip irq_chip; | ||
| 116 | struct mutex irq_lock; | ||
| 117 | int irq_base; | ||
| 118 | int irq_main; | ||
| 119 | u32 irq_en; | ||
| 120 | u8 mask_cache[3]; | ||
| 121 | u8 mask_reg[3]; | ||
| 122 | }; | ||
| 123 | |||
| 124 | static inline int __tps6591x_read(struct i2c_client *client, | ||
| 125 | int reg, uint8_t *val) | ||
| 126 | { | ||
| 127 | int ret; | ||
| 128 | |||
| 129 | ret = i2c_smbus_read_byte_data(client, reg); | ||
| 130 | if (ret < 0) { | ||
| 131 | dev_err(&client->dev, "failed reading at 0x%02x\n", reg); | ||
| 132 | return ret; | ||
| 133 | } | ||
| 134 | |||
| 135 | *val = (uint8_t)ret; | ||
| 136 | |||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | static inline int __tps6591x_reads(struct i2c_client *client, int reg, | ||
| 141 | int len, uint8_t *val) | ||
| 142 | { | ||
| 143 | int ret; | ||
| 144 | |||
| 145 | ret = i2c_smbus_read_i2c_block_data(client, reg, len, val); | ||
| 146 | if (ret < 0) { | ||
| 147 | dev_err(&client->dev, "failed reading from 0x%02x\n", reg); | ||
| 148 | return ret; | ||
| 149 | } | ||
| 150 | |||
| 151 | return 0; | ||
| 152 | } | ||
| 153 | |||
| 154 | static inline int __tps6591x_write(struct i2c_client *client, | ||
| 155 | int reg, uint8_t val) | ||
| 156 | { | ||
| 157 | int ret; | ||
| 158 | ret = i2c_smbus_write_byte_data(client, reg, val); | ||
| 159 | if (ret < 0) { | ||
| 160 | dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n", | ||
| 161 | val, reg); | ||
| 162 | return ret; | ||
| 163 | } | ||
| 164 | |||
| 165 | return 0; | ||
| 166 | } | ||
| 167 | |||
| 168 | static inline int __tps6591x_writes(struct i2c_client *client, int reg, | ||
| 169 | int len, uint8_t *val) | ||
| 170 | { | ||
| 171 | int ret; | ||
| 172 | |||
| 173 | ret = i2c_smbus_write_i2c_block_data(client, reg, len, val); | ||
| 174 | if (ret < 0) { | ||
| 175 | dev_err(&client->dev, "failed writings to 0x%02x\n", reg); | ||
| 176 | return ret; | ||
| 177 | } | ||
| 178 | |||
| 179 | return 0; | ||
| 180 | } | ||
| 181 | |||
| 182 | int tps6591x_write(struct device *dev, int reg, uint8_t val) | ||
| 183 | { | ||
| 184 | struct tps6591x *tps6591x = dev_get_drvdata(dev); | ||
| 185 | int ret = 0; | ||
| 186 | |||
| 187 | mutex_lock(&tps6591x->lock); | ||
| 188 | ret = __tps6591x_write(to_i2c_client(dev), reg, val); | ||
| 189 | mutex_unlock(&tps6591x->lock); | ||
| 190 | |||
| 191 | return ret; | ||
| 192 | } | ||
| 193 | EXPORT_SYMBOL_GPL(tps6591x_write); | ||
| 194 | |||
| 195 | int tps6591x_writes(struct device *dev, int reg, int len, uint8_t *val) | ||
| 196 | { | ||
| 197 | struct tps6591x *tps6591x = dev_get_drvdata(dev); | ||
| 198 | int ret = 0; | ||
| 199 | |||
| 200 | mutex_lock(&tps6591x->lock); | ||
| 201 | ret = __tps6591x_writes(to_i2c_client(dev), reg, len, val); | ||
| 202 | mutex_unlock(&tps6591x->lock); | ||
| 203 | |||
| 204 | return ret; | ||
| 205 | } | ||
| 206 | EXPORT_SYMBOL_GPL(tps6591x_writes); | ||
| 207 | |||
| 208 | int tps6591x_read(struct device *dev, int reg, uint8_t *val) | ||
| 209 | { | ||
| 210 | return __tps6591x_read(to_i2c_client(dev), reg, val); | ||
| 211 | } | ||
| 212 | EXPORT_SYMBOL_GPL(tps6591x_read); | ||
| 213 | |||
| 214 | int tps6591x_reads(struct device *dev, int reg, int len, uint8_t *val) | ||
| 215 | { | ||
| 216 | return __tps6591x_reads(to_i2c_client(dev), reg, len, val); | ||
| 217 | } | ||
| 218 | EXPORT_SYMBOL_GPL(tps6591x_reads); | ||
| 219 | |||
| 220 | int tps6591x_set_bits(struct device *dev, int reg, uint8_t bit_mask) | ||
| 221 | { | ||
| 222 | struct tps6591x *tps6591x = dev_get_drvdata(dev); | ||
| 223 | uint8_t reg_val; | ||
| 224 | int ret = 0; | ||
| 225 | |||
| 226 | mutex_lock(&tps6591x->lock); | ||
| 227 | |||
| 228 | ret = __tps6591x_read(to_i2c_client(dev), reg, ®_val); | ||
| 229 | if (ret) | ||
| 230 | goto out; | ||
| 231 | |||
| 232 | if ((reg_val & bit_mask) != bit_mask) { | ||
| 233 | reg_val |= bit_mask; | ||
| 234 | ret = __tps6591x_write(to_i2c_client(dev), reg, reg_val); | ||
| 235 | } | ||
| 236 | out: | ||
| 237 | mutex_unlock(&tps6591x->lock); | ||
| 238 | return ret; | ||
| 239 | } | ||
| 240 | EXPORT_SYMBOL_GPL(tps6591x_set_bits); | ||
| 241 | |||
| 242 | int tps6591x_clr_bits(struct device *dev, int reg, uint8_t bit_mask) | ||
| 243 | { | ||
| 244 | struct tps6591x *tps6591x = dev_get_drvdata(dev); | ||
| 245 | uint8_t reg_val; | ||
| 246 | int ret = 0; | ||
| 247 | |||
| 248 | mutex_lock(&tps6591x->lock); | ||
| 249 | |||
| 250 | ret = __tps6591x_read(to_i2c_client(dev), reg, ®_val); | ||
| 251 | if (ret) | ||
| 252 | goto out; | ||
| 253 | |||
| 254 | if (reg_val & bit_mask) { | ||
| 255 | reg_val &= ~bit_mask; | ||
| 256 | ret = __tps6591x_write(to_i2c_client(dev), reg, reg_val); | ||
| 257 | } | ||
| 258 | out: | ||
| 259 | mutex_unlock(&tps6591x->lock); | ||
| 260 | return ret; | ||
| 261 | } | ||
| 262 | EXPORT_SYMBOL_GPL(tps6591x_clr_bits); | ||
| 263 | |||
| 264 | int tps6591x_update(struct device *dev, int reg, uint8_t val, uint8_t mask) | ||
| 265 | { | ||
| 266 | struct tps6591x *tps6591x = dev_get_drvdata(dev); | ||
| 267 | uint8_t reg_val; | ||
| 268 | int ret = 0; | ||
| 269 | |||
| 270 | mutex_lock(&tps6591x->lock); | ||
| 271 | |||
| 272 | ret = __tps6591x_read(tps6591x->client, reg, ®_val); | ||
| 273 | if (ret) | ||
| 274 | goto out; | ||
| 275 | |||
| 276 | if ((reg_val & mask) != val) { | ||
| 277 | reg_val = (reg_val & ~mask) | (val & mask); | ||
| 278 | ret = __tps6591x_write(tps6591x->client, reg, reg_val); | ||
| 279 | } | ||
| 280 | out: | ||
| 281 | mutex_unlock(&tps6591x->lock); | ||
| 282 | return ret; | ||
| 283 | } | ||
| 284 | EXPORT_SYMBOL_GPL(tps6591x_update); | ||
| 285 | |||
| 286 | static struct i2c_client *tps6591x_i2c_client; | ||
| 287 | static void tps6591x_power_off(void) | ||
| 288 | { | ||
| 289 | struct device *dev = NULL; | ||
| 290 | |||
| 291 | if (!tps6591x_i2c_client) | ||
| 292 | return; | ||
| 293 | |||
| 294 | dev = &tps6591x_i2c_client->dev; | ||
| 295 | |||
| 296 | if (tps6591x_set_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_PWR_OFF_SEQ) < 0) | ||
| 297 | return; | ||
| 298 | |||
| 299 | tps6591x_clr_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_ON); | ||
| 300 | } | ||
| 301 | |||
| 302 | static int tps6591x_gpio_get(struct gpio_chip *gc, unsigned offset) | ||
| 303 | { | ||
| 304 | struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio); | ||
| 305 | uint8_t val; | ||
| 306 | int ret; | ||
| 307 | |||
| 308 | ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR + | ||
| 309 | offset, &val); | ||
| 310 | if (ret) | ||
| 311 | return ret; | ||
| 312 | |||
| 313 | if (val & 0x4) | ||
| 314 | return val & 0x1; | ||
| 315 | else | ||
| 316 | return (val & 0x2) ? 1 : 0; | ||
| 317 | } | ||
| 318 | |||
| 319 | static void tps6591x_gpio_set(struct gpio_chip *chip, unsigned offset, | ||
| 320 | int value) | ||
| 321 | { | ||
| 322 | |||
| 323 | struct tps6591x *tps6591x = container_of(chip, struct tps6591x, gpio); | ||
| 324 | |||
| 325 | tps6591x_update(tps6591x->dev, TPS6591X_GPIO_BASE_ADDR + offset, | ||
| 326 | value, 0x1); | ||
| 327 | } | ||
| 328 | |||
| 329 | static int tps6591x_gpio_input(struct gpio_chip *gc, unsigned offset) | ||
| 330 | { | ||
| 331 | struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio); | ||
| 332 | uint8_t reg_val; | ||
| 333 | int ret; | ||
| 334 | |||
| 335 | ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR + | ||
| 336 | offset, ®_val); | ||
| 337 | if (ret) | ||
| 338 | return ret; | ||
| 339 | |||
| 340 | reg_val &= ~0x4; | ||
| 341 | return __tps6591x_write(tps6591x->client, TPS6591X_GPIO_BASE_ADDR + | ||
| 342 | offset, reg_val); | ||
| 343 | } | ||
| 344 | |||
| 345 | static int tps6591x_gpio_output(struct gpio_chip *gc, unsigned offset, | ||
| 346 | int value) | ||
| 347 | { | ||
| 348 | struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio); | ||
| 349 | uint8_t reg_val, val; | ||
| 350 | int ret; | ||
| 351 | |||
| 352 | ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR + | ||
| 353 | offset, ®_val); | ||
| 354 | if (ret) | ||
| 355 | return ret; | ||
| 356 | |||
| 357 | reg_val &= ~0x1; | ||
| 358 | val = (value & 0x1) | 0x4; | ||
| 359 | reg_val = reg_val | val; | ||
| 360 | return __tps6591x_write(tps6591x->client, TPS6591X_GPIO_BASE_ADDR + | ||
| 361 | offset, reg_val); | ||
| 362 | } | ||
| 363 | |||
| 364 | static int tps6591x_gpio_to_irq(struct gpio_chip *gc, unsigned off) | ||
| 365 | { | ||
| 366 | struct tps6591x *tps6591x; | ||
| 367 | tps6591x = container_of(gc, struct tps6591x, gpio); | ||
| 368 | |||
| 369 | if ((off >= 0) && (off <= TPS6591X_INT_GPIO5 - TPS6591X_INT_GPIO0)) | ||
| 370 | return tps6591x->irq_base + TPS6591X_INT_GPIO0 + off; | ||
| 371 | |||
| 372 | return -EIO; | ||
| 373 | } | ||
| 374 | |||
| 375 | static void tps6591x_gpio_init(struct tps6591x *tps6591x, | ||
| 376 | struct tps6591x_platform_data *pdata) | ||
| 377 | { | ||
| 378 | int ret; | ||
| 379 | int gpio_base = pdata->gpio_base; | ||
| 380 | int i; | ||
| 381 | u8 gpio_reg; | ||
| 382 | struct tps6591x_gpio_init_data *ginit; | ||
| 383 | |||
| 384 | if (gpio_base <= 0) | ||
| 385 | return; | ||
| 386 | |||
| 387 | for (i = 0; i < pdata->num_gpioinit_data; ++i) { | ||
| 388 | ginit = &pdata->gpio_init_data[i]; | ||
| 389 | if (!ginit->init_apply) | ||
| 390 | continue; | ||
| 391 | gpio_reg = (ginit->sleep_en << TPS6591X_GPIO_SLEEP) | | ||
| 392 | (ginit->pulldn_en << TPS6591X_GPIO_PDEN) | | ||
| 393 | (ginit->output_mode_en << TPS6591X_GPIO_DIR); | ||
| 394 | |||
| 395 | if (ginit->output_mode_en) | ||
| 396 | gpio_reg |= ginit->output_val; | ||
| 397 | |||
| 398 | ret = __tps6591x_write(tps6591x->client, | ||
| 399 | TPS6591X_GPIO_BASE_ADDR + i, gpio_reg); | ||
| 400 | if (ret < 0) | ||
| 401 | dev_err(&tps6591x->client->dev, "Gpio %d init " | ||
| 402 | "configuration failed: %d\n", i, ret); | ||
| 403 | } | ||
| 404 | |||
| 405 | tps6591x->gpio.owner = THIS_MODULE; | ||
| 406 | tps6591x->gpio.label = tps6591x->client->name; | ||
| 407 | tps6591x->gpio.dev = tps6591x->dev; | ||
| 408 | tps6591x->gpio.base = gpio_base; | ||
| 409 | tps6591x->gpio.ngpio = TPS6591X_GPIO_NR; | ||
| 410 | tps6591x->gpio.can_sleep = 1; | ||
| 411 | |||
| 412 | tps6591x->gpio.direction_input = tps6591x_gpio_input; | ||
| 413 | tps6591x->gpio.direction_output = tps6591x_gpio_output; | ||
| 414 | tps6591x->gpio.set = tps6591x_gpio_set; | ||
| 415 | tps6591x->gpio.get = tps6591x_gpio_get; | ||
| 416 | tps6591x->gpio.to_irq = tps6591x_gpio_to_irq; | ||
| 417 | |||
| 418 | ret = gpiochip_add(&tps6591x->gpio); | ||
| 419 | if (ret) | ||
| 420 | dev_warn(tps6591x->dev, "GPIO registration failed: %d\n", ret); | ||
| 421 | } | ||
| 422 | |||
| 423 | static int __remove_subdev(struct device *dev, void *unused) | ||
| 424 | { | ||
| 425 | platform_device_unregister(to_platform_device(dev)); | ||
| 426 | return 0; | ||
| 427 | } | ||
| 428 | |||
| 429 | static int tps6591x_remove_subdevs(struct tps6591x *tps6591x) | ||
| 430 | { | ||
| 431 | return device_for_each_child(tps6591x->dev, NULL, __remove_subdev); | ||
| 432 | } | ||
| 433 | |||
| 434 | static void tps6591x_irq_lock(struct irq_data *data) | ||
| 435 | { | ||
| 436 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(data); | ||
| 437 | |||
| 438 | mutex_lock(&tps6591x->irq_lock); | ||
| 439 | } | ||
| 440 | |||
| 441 | static void tps6591x_irq_mask(struct irq_data *irq_data) | ||
| 442 | { | ||
| 443 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data); | ||
| 444 | unsigned int __irq = irq_data->irq - tps6591x->irq_base; | ||
| 445 | const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq]; | ||
| 446 | |||
| 447 | if (data->type == EVENT) | ||
| 448 | tps6591x->mask_reg[data->mask_reg] |= (1 << data->mask_pos); | ||
| 449 | else | ||
| 450 | tps6591x->mask_reg[data->mask_reg] |= (3 << data->mask_pos); | ||
| 451 | |||
| 452 | tps6591x->irq_en &= ~(1 << __irq); | ||
| 453 | } | ||
| 454 | |||
| 455 | static void tps6591x_irq_unmask(struct irq_data *irq_data) | ||
| 456 | { | ||
| 457 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data); | ||
| 458 | |||
| 459 | unsigned int __irq = irq_data->irq - tps6591x->irq_base; | ||
| 460 | const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq]; | ||
| 461 | |||
| 462 | if (data->type == EVENT) { | ||
| 463 | tps6591x->mask_reg[data->mask_reg] &= ~(1 << data->mask_pos); | ||
| 464 | tps6591x->irq_en |= (1 << __irq); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | static void tps6591x_irq_sync_unlock(struct irq_data *data) | ||
| 469 | { | ||
| 470 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(data); | ||
| 471 | int i; | ||
| 472 | |||
| 473 | for (i = 0; i < ARRAY_SIZE(tps6591x->mask_reg); i++) { | ||
| 474 | if (tps6591x->mask_reg[i] != tps6591x->mask_cache[i]) { | ||
| 475 | if (!WARN_ON(tps6591x_write(tps6591x->dev, | ||
| 476 | TPS6591X_INT_MSK + 2*i, | ||
| 477 | tps6591x->mask_reg[i]))) | ||
| 478 | tps6591x->mask_cache[i] = tps6591x->mask_reg[i]; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | mutex_unlock(&tps6591x->irq_lock); | ||
| 483 | } | ||
| 484 | |||
| 485 | static int tps6591x_irq_set_type(struct irq_data *irq_data, unsigned int type) | ||
| 486 | { | ||
| 487 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data); | ||
| 488 | |||
| 489 | unsigned int __irq = irq_data->irq - tps6591x->irq_base; | ||
| 490 | const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq]; | ||
| 491 | |||
| 492 | if (data->type == GPIO) { | ||
| 493 | if (type & IRQ_TYPE_EDGE_FALLING) | ||
| 494 | tps6591x->mask_reg[data->mask_reg] | ||
| 495 | &= ~(1 << data->mask_pos); | ||
| 496 | else | ||
| 497 | tps6591x->mask_reg[data->mask_reg] | ||
| 498 | |= (1 << data->mask_pos); | ||
| 499 | |||
| 500 | if (type & IRQ_TYPE_EDGE_RISING) | ||
| 501 | tps6591x->mask_reg[data->mask_reg] | ||
| 502 | &= ~(2 << data->mask_pos); | ||
| 503 | else | ||
| 504 | tps6591x->mask_reg[data->mask_reg] | ||
| 505 | |= (2 << data->mask_pos); | ||
| 506 | |||
| 507 | tps6591x->irq_en |= (1 << __irq); | ||
| 508 | } | ||
| 509 | |||
| 510 | return 0; | ||
| 511 | } | ||
| 512 | |||
| 513 | #ifdef CONFIG_PM_SLEEP | ||
| 514 | static int tps6591x_irq_set_wake(struct irq_data *irq_data, unsigned int on) | ||
| 515 | { | ||
| 516 | struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data); | ||
| 517 | return irq_set_irq_wake(tps6591x->irq_main, on); | ||
| 518 | } | ||
| 519 | #else | ||
| 520 | #define tps6591x_irq_set_wake NULL | ||
| 521 | #endif | ||
| 522 | |||
| 523 | |||
| 524 | static irqreturn_t tps6591x_irq(int irq, void *data) | ||
| 525 | { | ||
| 526 | struct tps6591x *tps6591x = data; | ||
| 527 | int ret = 0; | ||
| 528 | u8 tmp[3]; | ||
| 529 | u8 int_ack; | ||
| 530 | u32 acks, mask = 0; | ||
| 531 | int i; | ||
| 532 | |||
| 533 | for (i = 0; i < 3; i++) { | ||
| 534 | ret = tps6591x_read(tps6591x->dev, TPS6591X_INT_STS + 2*i, | ||
| 535 | &tmp[i]); | ||
| 536 | if (ret < 0) { | ||
| 537 | dev_err(tps6591x->dev, | ||
| 538 | "failed to read interrupt status\n"); | ||
| 539 | return IRQ_NONE; | ||
| 540 | } | ||
| 541 | if (tmp[i]) { | ||
| 542 | /* Ack only those interrupts which are enabled */ | ||
| 543 | int_ack = tmp[i] & (~(tps6591x->mask_cache[i])); | ||
| 544 | ret = tps6591x_write(tps6591x->dev, | ||
| 545 | TPS6591X_INT_STS + 2*i, int_ack); | ||
| 546 | if (ret < 0) { | ||
| 547 | dev_err(tps6591x->dev, | ||
| 548 | "failed to write interrupt status\n"); | ||
| 549 | return IRQ_NONE; | ||
| 550 | } | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | acks = (tmp[2] << 16) | (tmp[1] << 8) | tmp[0]; | ||
| 555 | |||
| 556 | for (i = 0; i < ARRAY_SIZE(tps6591x_irqs); i++) { | ||
| 557 | if (tps6591x_irqs[i].type == GPIO) | ||
| 558 | mask = (3 << (tps6591x_irqs[i].mask_pos | ||
| 559 | + tps6591x_irqs[i].mask_reg*8)); | ||
| 560 | else if (tps6591x_irqs[i].type == EVENT) | ||
| 561 | mask = (1 << (tps6591x_irqs[i].mask_pos | ||
| 562 | + tps6591x_irqs[i].mask_reg*8)); | ||
| 563 | |||
| 564 | if ((acks & mask) && (tps6591x->irq_en & (1 << i))) | ||
| 565 | handle_nested_irq(tps6591x->irq_base + i); | ||
| 566 | } | ||
| 567 | return IRQ_HANDLED; | ||
| 568 | } | ||
| 569 | |||
| 570 | static int __devinit tps6591x_irq_init(struct tps6591x *tps6591x, int irq, | ||
| 571 | int irq_base) | ||
| 572 | { | ||
| 573 | int i, ret; | ||
| 574 | |||
| 575 | if (!irq_base) { | ||
| 576 | dev_warn(tps6591x->dev, "No interrupt support on IRQ base\n"); | ||
| 577 | return -EINVAL; | ||
| 578 | } | ||
| 579 | |||
| 580 | mutex_init(&tps6591x->irq_lock); | ||
| 581 | |||
| 582 | tps6591x->mask_reg[0] = 0xFF; | ||
| 583 | tps6591x->mask_reg[1] = 0xFF; | ||
| 584 | tps6591x->mask_reg[2] = 0xFF; | ||
| 585 | for (i = 0; i < 3; i++) { | ||
| 586 | tps6591x->mask_cache[i] = tps6591x->mask_reg[i]; | ||
| 587 | tps6591x_write(tps6591x->dev, TPS6591X_INT_MSK + 2*i, | ||
| 588 | tps6591x->mask_cache[i]); | ||
| 589 | } | ||
| 590 | |||
| 591 | for (i = 0; i < 3; i++) | ||
| 592 | tps6591x_write(tps6591x->dev, TPS6591X_INT_STS + 2*i, 0xff); | ||
| 593 | |||
| 594 | tps6591x->irq_base = irq_base; | ||
| 595 | tps6591x->irq_main = irq; | ||
| 596 | |||
| 597 | tps6591x->irq_chip.name = "tps6591x"; | ||
| 598 | tps6591x->irq_chip.irq_mask = tps6591x_irq_mask; | ||
| 599 | tps6591x->irq_chip.irq_unmask = tps6591x_irq_unmask; | ||
| 600 | tps6591x->irq_chip.irq_bus_lock = tps6591x_irq_lock; | ||
| 601 | tps6591x->irq_chip.irq_bus_sync_unlock = tps6591x_irq_sync_unlock; | ||
| 602 | tps6591x->irq_chip.irq_set_type = tps6591x_irq_set_type; | ||
| 603 | tps6591x->irq_chip.irq_set_wake = tps6591x_irq_set_wake; | ||
| 604 | |||
| 605 | for (i = 0; i < ARRAY_SIZE(tps6591x_irqs); i++) { | ||
| 606 | int __irq = i + tps6591x->irq_base; | ||
| 607 | irq_set_chip_data(__irq, tps6591x); | ||
| 608 | irq_set_chip_and_handler(__irq, &tps6591x->irq_chip, | ||
| 609 | handle_simple_irq); | ||
| 610 | irq_set_nested_thread(__irq, 1); | ||
| 611 | #ifdef CONFIG_ARM | ||
| 612 | set_irq_flags(__irq, IRQF_VALID); | ||
| 613 | #endif | ||
| 614 | } | ||
| 615 | |||
| 616 | ret = request_threaded_irq(irq, NULL, tps6591x_irq, IRQF_ONESHOT, | ||
| 617 | "tps6591x", tps6591x); | ||
| 618 | if (!ret) { | ||
| 619 | device_init_wakeup(tps6591x->dev, 1); | ||
| 620 | enable_irq_wake(irq); | ||
| 621 | } | ||
| 622 | |||
| 623 | return ret; | ||
| 624 | } | ||
| 625 | |||
| 626 | static int __devinit tps6591x_add_subdevs(struct tps6591x *tps6591x, | ||
| 627 | struct tps6591x_platform_data *pdata) | ||
| 628 | { | ||
| 629 | struct tps6591x_subdev_info *subdev; | ||
| 630 | struct platform_device *pdev; | ||
| 631 | int i, ret = 0; | ||
| 632 | |||
| 633 | for (i = 0; i < pdata->num_subdevs; i++) { | ||
| 634 | subdev = &pdata->subdevs[i]; | ||
| 635 | |||
| 636 | pdev = platform_device_alloc(subdev->name, subdev->id); | ||
| 637 | |||
| 638 | pdev->dev.parent = tps6591x->dev; | ||
| 639 | pdev->dev.platform_data = subdev->platform_data; | ||
| 640 | |||
| 641 | ret = platform_device_add(pdev); | ||
| 642 | if (ret) | ||
| 643 | goto failed; | ||
| 644 | } | ||
| 645 | return 0; | ||
| 646 | |||
| 647 | failed: | ||
| 648 | tps6591x_remove_subdevs(tps6591x); | ||
| 649 | return ret; | ||
| 650 | } | ||
| 651 | #ifdef CONFIG_DEBUG_FS | ||
| 652 | #include <linux/debugfs.h> | ||
| 653 | #include <linux/seq_file.h> | ||
| 654 | static void print_regs(const char *header, struct seq_file *s, | ||
| 655 | struct i2c_client *client, int start_offset, | ||
| 656 | int end_offset) | ||
| 657 | { | ||
| 658 | uint8_t reg_val; | ||
| 659 | int i; | ||
| 660 | int ret; | ||
| 661 | |||
| 662 | seq_printf(s, "%s\n", header); | ||
| 663 | for (i = start_offset; i <= end_offset; ++i) { | ||
| 664 | ret = __tps6591x_read(client, i, ®_val); | ||
| 665 | if (ret >= 0) | ||
| 666 | seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val); | ||
| 667 | } | ||
| 668 | seq_printf(s, "------------------\n"); | ||
| 669 | } | ||
| 670 | |||
| 671 | static int dbg_tps_show(struct seq_file *s, void *unused) | ||
| 672 | { | ||
| 673 | struct tps6591x *tps = s->private; | ||
| 674 | struct i2c_client *client = tps->client; | ||
| 675 | |||
| 676 | seq_printf(s, "TPS6591x Registers\n"); | ||
| 677 | seq_printf(s, "------------------\n"); | ||
| 678 | |||
| 679 | print_regs("Timing Regs", s, client, 0x0, 0x6); | ||
| 680 | print_regs("Alarm Regs", s, client, 0x8, 0xD); | ||
| 681 | print_regs("RTC Regs", s, client, 0x10, 0x16); | ||
| 682 | print_regs("BCK Regs", s, client, 0x17, 0x1B); | ||
| 683 | print_regs("PUADEN Regs", s, client, 0x18, 0x18); | ||
| 684 | print_regs("REF Regs", s, client, 0x1D, 0x1D); | ||
| 685 | print_regs("VDD Regs", s, client, 0x1E, 0x29); | ||
| 686 | print_regs("LDO Regs", s, client, 0x30, 0x37); | ||
| 687 | print_regs("THERM Regs", s, client, 0x38, 0x38); | ||
| 688 | print_regs("BBCH Regs", s, client, 0x39, 0x39); | ||
| 689 | print_regs("DCDCCNTRL Regs", s, client, 0x3E, 0x3E); | ||
| 690 | print_regs("DEV_CNTRL Regs", s, client, 0x3F, 0x40); | ||
| 691 | print_regs("SLEEP Regs", s, client, 0x41, 0x44); | ||
| 692 | print_regs("EN1 Regs", s, client, 0x45, 0x48); | ||
| 693 | print_regs("INT Regs", s, client, 0x50, 0x55); | ||
| 694 | print_regs("GPIO Regs", s, client, 0x60, 0x68); | ||
| 695 | print_regs("WATCHDOG Regs", s, client, 0x69, 0x69); | ||
| 696 | print_regs("VMBCH Regs", s, client, 0x6A, 0x6B); | ||
| 697 | print_regs("LED_CTRL Regs", s, client, 0x6c, 0x6D); | ||
| 698 | print_regs("PWM_CTRL Regs", s, client, 0x6E, 0x6F); | ||
| 699 | print_regs("SPARE Regs", s, client, 0x70, 0x70); | ||
| 700 | print_regs("VERNUM Regs", s, client, 0x80, 0x80); | ||
| 701 | return 0; | ||
| 702 | } | ||
| 703 | |||
| 704 | static int dbg_tps_open(struct inode *inode, struct file *file) | ||
| 705 | { | ||
| 706 | return single_open(file, dbg_tps_show, inode->i_private); | ||
| 707 | } | ||
| 708 | |||
| 709 | static const struct file_operations debug_fops = { | ||
| 710 | .open = dbg_tps_open, | ||
| 711 | .read = seq_read, | ||
| 712 | .llseek = seq_lseek, | ||
| 713 | .release = single_release, | ||
| 714 | }; | ||
| 715 | |||
| 716 | static void __init tps6591x_debuginit(struct tps6591x *tps) | ||
| 717 | { | ||
| 718 | (void)debugfs_create_file("tps6591x", S_IRUGO, NULL, | ||
| 719 | tps, &debug_fops); | ||
| 720 | } | ||
| 721 | #else | ||
| 722 | static void __init tps6591x_debuginit(struct tps6591x *tpsi) | ||
| 723 | { | ||
| 724 | return; | ||
| 725 | } | ||
| 726 | #endif | ||
| 727 | |||
| 728 | static int __init tps6591x_sleepinit(struct tps6591x *tpsi, | ||
| 729 | struct tps6591x_platform_data *pdata) | ||
| 730 | { | ||
| 731 | struct device *dev = NULL; | ||
| 732 | int ret = 0; | ||
| 733 | |||
| 734 | dev = tpsi->dev; | ||
| 735 | |||
| 736 | if (!pdata->dev_slp_en) | ||
| 737 | goto no_err_return; | ||
| 738 | |||
| 739 | /* pmu dev_slp_en is set. Make sure slp_keepon is available before | ||
| 740 | * allowing SLEEP device state */ | ||
| 741 | if (!pdata->slp_keepon) { | ||
| 742 | dev_err(dev, "slp_keepon_data required for slp_en\n"); | ||
| 743 | goto err_sleep_init; | ||
| 744 | } | ||
| 745 | |||
| 746 | /* enabling SLEEP device state */ | ||
| 747 | ret = tps6591x_set_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_SLP); | ||
| 748 | if (ret < 0) { | ||
| 749 | dev_err(dev, "set dev_slp failed: %d\n", ret); | ||
| 750 | goto err_sleep_init; | ||
| 751 | } | ||
| 752 | |||
| 753 | if (pdata->slp_keepon->therm_keepon) { | ||
| 754 | ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON, | ||
| 755 | SLEEP_KEEP_ON_THERM); | ||
| 756 | if (ret < 0) { | ||
| 757 | dev_err(dev, "set therm_keepon failed: %d\n", ret); | ||
| 758 | goto disable_dev_slp; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | if (pdata->slp_keepon->clkout32k_keepon) { | ||
| 763 | ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON, | ||
| 764 | SLEEP_KEEP_ON_CLKOUT32K); | ||
| 765 | if (ret < 0) { | ||
| 766 | dev_err(dev, "set clkout32k_keepon failed: %d\n", ret); | ||
| 767 | goto disable_dev_slp; | ||
| 768 | } | ||
| 769 | } | ||
| 770 | |||
| 771 | |||
| 772 | if (pdata->slp_keepon->vrtc_keepon) { | ||
| 773 | ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON, | ||
| 774 | SLEEP_KEEP_ON_VRTC); | ||
| 775 | if (ret < 0) { | ||
| 776 | dev_err(dev, "set vrtc_keepon failed: %d\n", ret); | ||
| 777 | goto disable_dev_slp; | ||
| 778 | } | ||
| 779 | } | ||
| 780 | |||
| 781 | if (pdata->slp_keepon->i2chs_keepon) { | ||
| 782 | ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON, | ||
| 783 | SLEEP_KEEP_ON_I2CHS); | ||
| 784 | if (ret < 0) { | ||
| 785 | dev_err(dev, "set i2chs_keepon failed: %d\n", ret); | ||
| 786 | goto disable_dev_slp; | ||
| 787 | } | ||
| 788 | } | ||
| 789 | |||
| 790 | no_err_return: | ||
| 791 | return 0; | ||
| 792 | |||
| 793 | disable_dev_slp: | ||
| 794 | tps6591x_clr_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_SLP); | ||
| 795 | |||
| 796 | err_sleep_init: | ||
| 797 | return ret; | ||
| 798 | } | ||
| 799 | |||
| 800 | static int __devinit tps6591x_i2c_probe(struct i2c_client *client, | ||
| 801 | const struct i2c_device_id *id) | ||
| 802 | { | ||
| 803 | struct tps6591x_platform_data *pdata = client->dev.platform_data; | ||
| 804 | struct tps6591x *tps6591x; | ||
| 805 | int ret; | ||
| 806 | |||
| 807 | if (!pdata) { | ||
| 808 | dev_err(&client->dev, "tps6591x requires platform data\n"); | ||
| 809 | return -ENOTSUPP; | ||
| 810 | } | ||
| 811 | |||
| 812 | ret = i2c_smbus_read_byte_data(client, TPS6591X_VERNUM); | ||
| 813 | if (ret < 0) { | ||
| 814 | dev_err(&client->dev, "Silicon version number read" | ||
| 815 | " failed: %d\n", ret); | ||
| 816 | return -EIO; | ||
| 817 | } | ||
| 818 | |||
| 819 | dev_info(&client->dev, "VERNUM is %02x\n", ret); | ||
| 820 | |||
| 821 | tps6591x = kzalloc(sizeof(struct tps6591x), GFP_KERNEL); | ||
| 822 | if (tps6591x == NULL) | ||
| 823 | return -ENOMEM; | ||
| 824 | |||
| 825 | tps6591x->client = client; | ||
| 826 | tps6591x->dev = &client->dev; | ||
| 827 | i2c_set_clientdata(client, tps6591x); | ||
| 828 | |||
| 829 | mutex_init(&tps6591x->lock); | ||
| 830 | |||
| 831 | if (client->irq) { | ||
| 832 | ret = tps6591x_irq_init(tps6591x, client->irq, | ||
| 833 | pdata->irq_base); | ||
| 834 | if (ret) { | ||
| 835 | dev_err(&client->dev, "IRQ init failed: %d\n", ret); | ||
| 836 | goto err_irq_init; | ||
| 837 | } | ||
| 838 | } | ||
| 839 | |||
| 840 | ret = tps6591x_add_subdevs(tps6591x, pdata); | ||
| 841 | if (ret) { | ||
| 842 | dev_err(&client->dev, "add devices failed: %d\n", ret); | ||
| 843 | goto err_add_devs; | ||
| 844 | } | ||
| 845 | |||
| 846 | tps6591x_gpio_init(tps6591x, pdata); | ||
| 847 | |||
| 848 | tps6591x_debuginit(tps6591x); | ||
| 849 | |||
| 850 | tps6591x_sleepinit(tps6591x, pdata); | ||
| 851 | |||
| 852 | if (pdata->use_power_off && !pm_power_off) | ||
| 853 | pm_power_off = tps6591x_power_off; | ||
| 854 | |||
| 855 | tps6591x_i2c_client = client; | ||
| 856 | |||
| 857 | return 0; | ||
| 858 | |||
| 859 | err_add_devs: | ||
| 860 | if (client->irq) | ||
| 861 | free_irq(client->irq, tps6591x); | ||
| 862 | err_irq_init: | ||
| 863 | kfree(tps6591x); | ||
| 864 | return ret; | ||
| 865 | } | ||
| 866 | |||
| 867 | static int __devexit tps6591x_i2c_remove(struct i2c_client *client) | ||
| 868 | { | ||
| 869 | struct tps6591x *tps6591x = i2c_get_clientdata(client); | ||
| 870 | |||
| 871 | if (client->irq) | ||
| 872 | free_irq(client->irq, tps6591x); | ||
| 873 | |||
| 874 | if (gpiochip_remove(&tps6591x->gpio) < 0) | ||
| 875 | dev_err(&client->dev, "Error in removing the gpio driver\n"); | ||
| 876 | |||
| 877 | kfree(tps6591x); | ||
| 878 | return 0; | ||
| 879 | } | ||
| 880 | #ifdef CONFIG_PM | ||
| 881 | static int tps6591x_i2c_suspend(struct i2c_client *client, pm_message_t state) | ||
| 882 | { | ||
| 883 | if (client->irq) | ||
| 884 | disable_irq(client->irq); | ||
| 885 | return 0; | ||
| 886 | } | ||
| 887 | |||
| 888 | static int tps6591x_i2c_resume(struct i2c_client *client) | ||
| 889 | { | ||
| 890 | if (client->irq) | ||
| 891 | enable_irq(client->irq); | ||
| 892 | return 0; | ||
| 893 | } | ||
| 894 | #endif | ||
| 895 | |||
| 896 | |||
| 897 | static const struct i2c_device_id tps6591x_id_table[] = { | ||
| 898 | { "tps6591x", 0 }, | ||
| 899 | { }, | ||
| 900 | }; | ||
| 901 | MODULE_DEVICE_TABLE(i2c, tps6591x_id_table); | ||
| 902 | |||
| 903 | static struct i2c_driver tps6591x_driver = { | ||
| 904 | .driver = { | ||
| 905 | .name = "tps6591x", | ||
| 906 | .owner = THIS_MODULE, | ||
| 907 | }, | ||
| 908 | .probe = tps6591x_i2c_probe, | ||
| 909 | .remove = __devexit_p(tps6591x_i2c_remove), | ||
| 910 | #ifdef CONFIG_PM | ||
| 911 | .suspend = tps6591x_i2c_suspend, | ||
| 912 | .resume = tps6591x_i2c_resume, | ||
| 913 | #endif | ||
| 914 | .id_table = tps6591x_id_table, | ||
| 915 | }; | ||
| 916 | |||
| 917 | static int __init tps6591x_init(void) | ||
| 918 | { | ||
| 919 | return i2c_add_driver(&tps6591x_driver); | ||
| 920 | } | ||
| 921 | subsys_initcall(tps6591x_init); | ||
| 922 | |||
| 923 | static void __exit tps6591x_exit(void) | ||
| 924 | { | ||
| 925 | i2c_del_driver(&tps6591x_driver); | ||
| 926 | } | ||
| 927 | module_exit(tps6591x_exit); | ||
| 928 | |||
| 929 | MODULE_DESCRIPTION("TPS6591X core driver"); | ||
| 930 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mfd/tps8003x-gpadc.c b/drivers/mfd/tps8003x-gpadc.c new file mode 100644 index 00000000000..5db912cc01f --- /dev/null +++ b/drivers/mfd/tps8003x-gpadc.c | |||
| @@ -0,0 +1,650 @@ | |||
| 1 | /* | ||
| 2 | * drivers/mfd/tps8003x-gpadc.c | ||
| 3 | * | ||
| 4 | * Gpadc for TI's tps80031 | ||
| 5 | * | ||
| 6 | * Copyright (c) 2011, NVIDIA Corporation. | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU General Public License | ||
| 10 | * version 2 as published by the Free Software Foundation. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, but | ||
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License | ||
| 18 | * along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
| 20 | * 02110-1301 USA | ||
| 21 | * | ||
| 22 | */ | ||
| 23 | #include <linux/init.h> | ||
| 24 | #include <linux/interrupt.h> | ||
| 25 | #include <linux/kernel.h> | ||
| 26 | #include <linux/types.h> | ||
| 27 | #include <linux/module.h> | ||
| 28 | #include <linux/delay.h> | ||
| 29 | #include <linux/fs.h> | ||
| 30 | #include <linux/platform_device.h> | ||
| 31 | #include <linux/miscdevice.h> | ||
| 32 | #include <linux/slab.h> | ||
| 33 | #include <linux/hwmon-sysfs.h> | ||
| 34 | #include <linux/i2c/twl.h> | ||
| 35 | #include <linux/mfd/tps80031.h> | ||
| 36 | #include <linux/uaccess.h> | ||
| 37 | #include <linux/spinlock.h> | ||
| 38 | |||
| 39 | #define GPADC_CTRL 0x2e | ||
| 40 | #define GPSELECT_ISB 0x35 | ||
| 41 | #define GPCH0_LSB 0x3b | ||
| 42 | #define GPCH0_MSB 0x3c | ||
| 43 | #define CTRL_P1 0x36 | ||
| 44 | #define TOGGLE1 0x90 | ||
| 45 | #define MISC1 0xe4 | ||
| 46 | |||
| 47 | #define CTRL_P1_SP1 BIT(3) | ||
| 48 | #define TOGGLE1_GPADCR BIT(1) | ||
| 49 | #define GPADC_BUSY (1 << 0) | ||
| 50 | #define GPADC_EOC_SW (1 << 1) | ||
| 51 | #define SCALE (1 << 15) | ||
| 52 | |||
| 53 | #define TPS80031_GPADC_MAX_CHANNELS 17 | ||
| 54 | #define TPS80031_GPADC_IOC_MAGIC '`' | ||
| 55 | #define TPS80031_GPADC_IOCX_ADC_RAW_READ _IO(TPS80031_GPADC_IOC_MAGIC, 0) | ||
| 56 | |||
| 57 | struct tps80031_gpadc_user_parms { | ||
| 58 | int channel; | ||
| 59 | int status; | ||
| 60 | u16 result; | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct tps80031_calibration { | ||
| 64 | s32 gain_error; | ||
| 65 | s32 offset_error; | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct tps80031_ideal_code { | ||
| 69 | s16 code1; | ||
| 70 | s16 code2; | ||
| 71 | }; | ||
| 72 | |||
| 73 | struct tps80031_scalar_channel { | ||
| 74 | uint8_t delta1_addr; | ||
| 75 | uint8_t delta1_mask; | ||
| 76 | uint8_t delta2_addr; | ||
| 77 | uint8_t delta2_mask; | ||
| 78 | }; | ||
| 79 | |||
| 80 | static struct tps80031_calibration | ||
| 81 | tps80031_calib_tbl[TPS80031_GPADC_MAX_CHANNELS]; | ||
| 82 | static const uint32_t calibration_bit_map = 0x47FF; | ||
| 83 | static const uint32_t scalar_bit_map = 0x4785; | ||
| 84 | |||
| 85 | #define TPS80031_GPADC_TRIM1 0xCD | ||
| 86 | #define TPS80031_GPADC_TRIM2 0xCE | ||
| 87 | #define TPS80031_GPADC_TRIM3 0xCF | ||
| 88 | #define TPS80031_GPADC_TRIM4 0xD0 | ||
| 89 | #define TPS80031_GPADC_TRIM5 0xD1 | ||
| 90 | #define TPS80031_GPADC_TRIM6 0xD2 | ||
| 91 | #define TPS80031_GPADC_TRIM7 0xD3 | ||
| 92 | #define TPS80031_GPADC_TRIM8 0xD4 | ||
| 93 | #define TPS80031_GPADC_TRIM9 0xD5 | ||
| 94 | #define TPS80031_GPADC_TRIM10 0xD6 | ||
| 95 | #define TPS80031_GPADC_TRIM11 0xD7 | ||
| 96 | #define TPS80031_GPADC_TRIM12 0xD8 | ||
| 97 | #define TPS80031_GPADC_TRIM13 0xD9 | ||
| 98 | #define TPS80031_GPADC_TRIM14 0xDA | ||
| 99 | #define TPS80031_GPADC_TRIM15 0xDB | ||
| 100 | #define TPS80031_GPADC_TRIM16 0xDC | ||
| 101 | #define TPS80031_GPADC_TRIM19 0xFD | ||
| 102 | |||
| 103 | static const struct tps80031_scalar_channel | ||
| 104 | tps80031_trim[TPS80031_GPADC_MAX_CHANNELS] = { | ||
| 105 | { TPS80031_GPADC_TRIM1, 0x7, TPS80031_GPADC_TRIM2, 0x07}, | ||
| 106 | { 0x00, }, | ||
| 107 | { TPS80031_GPADC_TRIM3, 0x1F, TPS80031_GPADC_TRIM4, 0x3F}, | ||
| 108 | { 0x00, }, | ||
| 109 | { 0x00, }, | ||
| 110 | { 0x00, }, | ||
| 111 | { 0x00, }, | ||
| 112 | { TPS80031_GPADC_TRIM7, 0x1F, TPS80031_GPADC_TRIM8, 0x1F }, | ||
| 113 | { TPS80031_GPADC_TRIM9, 0x0F, TPS80031_GPADC_TRIM10, 0x1F }, | ||
| 114 | { TPS80031_GPADC_TRIM11, 0x0F, TPS80031_GPADC_TRIM12, 0x1F }, | ||
| 115 | { TPS80031_GPADC_TRIM13, 0x0F, TPS80031_GPADC_TRIM14, 0x1F }, | ||
| 116 | { 0x00, }, | ||
| 117 | { 0x00, }, | ||
| 118 | { 0x00, }, | ||
| 119 | { TPS80031_GPADC_TRIM15, 0x0f, TPS80031_GPADC_TRIM16, 0x1F }, | ||
| 120 | { 0x00, }, | ||
| 121 | { 0x00 ,}, | ||
| 122 | }; | ||
| 123 | |||
| 124 | /* | ||
| 125 | * actual scaler gain is multiplied by 8 for fixed point operation | ||
| 126 | * 1.875 * 8 = 15 | ||
| 127 | */ | ||
| 128 | static const uint16_t tps80031_gain[TPS80031_GPADC_MAX_CHANNELS] = { | ||
| 129 | 1142, /* CHANNEL 0 */ | ||
| 130 | 8, /* CHANNEL 1 */ | ||
| 131 | /* 1.875 */ | ||
| 132 | 15, /* CHANNEL 2 */ | ||
| 133 | 8, /* CHANNEL 3 */ | ||
| 134 | 8, /* CHANNEL 4 */ | ||
| 135 | 8, /* CHANNEL 5 */ | ||
| 136 | 8, /* CHANNEL 6 */ | ||
| 137 | /* 5 */ | ||
| 138 | 40, /* CHANNEL 7 */ | ||
| 139 | /* 6.25 */ | ||
| 140 | 50, /* CHANNEL 8 */ | ||
| 141 | /* 11.25 */ | ||
| 142 | 90, /* CHANNEL 9 */ | ||
| 143 | /* 6.875 */ | ||
| 144 | 55, /* CHANNEL 10 */ | ||
| 145 | /* 1.875 */ | ||
| 146 | 15, /* CHANNEL 11 */ | ||
| 147 | 8, /* CHANNEL 12 */ | ||
| 148 | 8, /* CHANNEL 13 */ | ||
| 149 | /* 6.875 */ | ||
| 150 | 55, /* CHANNEL 14 */ | ||
| 151 | 8, /* CHANNEL 15 */ | ||
| 152 | 8, /* CHANNEL 16 */ | ||
| 153 | }; | ||
| 154 | |||
| 155 | /* | ||
| 156 | * calibration not needed for channel 11, 12, 13, 15 and 16 | ||
| 157 | * calibration offset is same for channel 1, 3, 4, 5 | ||
| 158 | */ | ||
| 159 | static const struct tps80031_ideal_code | ||
| 160 | tps80031_ideal[TPS80031_GPADC_MAX_CHANNELS] = { | ||
| 161 | {463, 2982}, /* CHANNEL 0 */ | ||
| 162 | {328, 3604}, /* CHANNEL 1 */ | ||
| 163 | {221, 3274}, /* CHANNEL 2 */ | ||
| 164 | {328, 3604}, /* CHANNEL 3 */ | ||
| 165 | {328, 3604}, /* CHANNEL 4 */ | ||
| 166 | {328, 3604}, /* CHANNEL 5 */ | ||
| 167 | {328, 3604}, /* CHANNEL 6 */ | ||
| 168 | {1966, 3013}, /* CHANNEL 7 */ | ||
| 169 | {328, 2754}, /* CHANNEL 8 */ | ||
| 170 | {728, 3275}, /* CHANNEL 9 */ | ||
| 171 | {596, 3274}, /* CHANNEL 10 */ | ||
| 172 | {0, 0}, /* CHANNEL 11 */ | ||
| 173 | {0, 0}, /* CHANNEL 12 */ | ||
| 174 | {0, 0}, /* CHANNEL 13 */ | ||
| 175 | {193, 2859}, /* CHANNEL 14 */ | ||
| 176 | {0, 0}, /* CHANNEL 15 */ | ||
| 177 | {0, 0}, /* CHANNEL 16 */ | ||
| 178 | }; | ||
| 179 | |||
| 180 | struct tps80031_gpadc_data { | ||
| 181 | struct device *dev; | ||
| 182 | struct mutex lock; | ||
| 183 | }; | ||
| 184 | |||
| 185 | static struct tps80031_gpadc_data *the_gpadc; | ||
| 186 | |||
| 187 | static ssize_t show_gain(struct device *dev, | ||
| 188 | struct device_attribute *devattr, char *buf) | ||
| 189 | { | ||
| 190 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 191 | int value; | ||
| 192 | int status; | ||
| 193 | |||
| 194 | value = tps80031_calib_tbl[attr->index].gain_error; | ||
| 195 | status = sprintf(buf, "%d\n", value); | ||
| 196 | return status; | ||
| 197 | } | ||
| 198 | |||
| 199 | static ssize_t set_gain(struct device *dev, | ||
| 200 | struct device_attribute *devattr, const char *buf, size_t count) | ||
| 201 | { | ||
| 202 | long val; | ||
| 203 | int status = count; | ||
| 204 | |||
| 205 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 206 | if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000) | ||
| 207 | || (val > 60000)) | ||
| 208 | return -EINVAL; | ||
| 209 | tps80031_calib_tbl[attr->index].gain_error = val; | ||
| 210 | return status; | ||
| 211 | } | ||
| 212 | |||
| 213 | static ssize_t show_offset(struct device *dev, | ||
| 214 | struct device_attribute *devattr, char *buf) | ||
| 215 | { | ||
| 216 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 217 | int value; | ||
| 218 | int status; | ||
| 219 | |||
| 220 | value = tps80031_calib_tbl[attr->index].offset_error; | ||
| 221 | status = sprintf(buf, "%d\n", value); | ||
| 222 | return status; | ||
| 223 | } | ||
| 224 | |||
| 225 | static ssize_t set_offset(struct device *dev, | ||
| 226 | struct device_attribute *devattr, const char *buf, size_t count) | ||
| 227 | { | ||
| 228 | long val; | ||
| 229 | int status = count; | ||
| 230 | |||
| 231 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
| 232 | if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000) | ||
| 233 | || (val > 60000)) | ||
| 234 | return -EINVAL; | ||
| 235 | tps80031_calib_tbl[attr->index].offset_error = val; | ||
| 236 | return status; | ||
| 237 | } | ||
| 238 | |||
| 239 | static int tps80031_reg_read(struct tps80031_gpadc_data *gpadc, int sid, | ||
| 240 | int reg, uint8_t *val) | ||
| 241 | { | ||
| 242 | int ret; | ||
| 243 | |||
| 244 | ret = tps80031_read(gpadc->dev->parent, sid, reg, val); | ||
| 245 | if (ret < 0) | ||
| 246 | dev_err(gpadc->dev, "Failed read register 0x%02x\n", reg); | ||
| 247 | return ret; | ||
| 248 | } | ||
| 249 | |||
| 250 | static int tps80031_reg_write(struct tps80031_gpadc_data *gpadc, int sid, | ||
| 251 | int reg, uint8_t val) | ||
| 252 | { | ||
| 253 | int ret; | ||
| 254 | |||
| 255 | ret = tps80031_write(gpadc->dev->parent, sid, reg, val); | ||
| 256 | if (ret < 0) | ||
| 257 | dev_err(gpadc->dev, "Failed write register 0x%02x\n", reg); | ||
| 258 | return ret; | ||
| 259 | } | ||
| 260 | |||
| 261 | static int tps80031_gpadc_channel_raw_read(struct tps80031_gpadc_data *gpadc) | ||
| 262 | { | ||
| 263 | uint8_t msb, lsb; | ||
| 264 | int ret; | ||
| 265 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_LSB, &lsb); | ||
| 266 | if (ret < 0) | ||
| 267 | return ret; | ||
| 268 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_MSB, &msb); | ||
| 269 | if (ret < 0) | ||
| 270 | return ret; | ||
| 271 | |||
| 272 | return (int)((msb << 8) | lsb); | ||
| 273 | } | ||
| 274 | |||
| 275 | static int tps80031_gpadc_read_channels(struct tps80031_gpadc_data *gpadc, | ||
| 276 | uint32_t channel) | ||
| 277 | { | ||
| 278 | uint8_t bits; | ||
| 279 | int gain_error; | ||
| 280 | int offset_error; | ||
| 281 | int raw_code; | ||
| 282 | int corrected_code; | ||
| 283 | int channel_value; | ||
| 284 | int raw_channel_value; | ||
| 285 | |||
| 286 | /* TPS80031 has 12bit ADC */ | ||
| 287 | bits = 12; | ||
| 288 | raw_code = tps80031_gpadc_channel_raw_read(gpadc); | ||
| 289 | if (raw_code < 0) | ||
| 290 | return raw_code; | ||
| 291 | /* | ||
| 292 | * Channels 0,2,7,8,9,10,14 offst and gain cannot | ||
| 293 | * be fully compensated by software | ||
| 294 | */ | ||
| 295 | if (channel == 7) | ||
| 296 | return raw_code; | ||
| 297 | /* | ||
| 298 | * multiply by 1000 to convert the unit to milli | ||
| 299 | * division by 1024 (>> bits) for 10/12 bit ADC | ||
| 300 | * division by 8 (>> 3) for actual scaler gain | ||
| 301 | */ | ||
| 302 | raw_channel_value = | ||
| 303 | (raw_code * tps80031_gain[channel] * 1000) >> (bits + 3); | ||
| 304 | |||
| 305 | gain_error = tps80031_calib_tbl[channel].gain_error; | ||
| 306 | offset_error = tps80031_calib_tbl[channel].offset_error; | ||
| 307 | corrected_code = (raw_code * SCALE - offset_error) / gain_error; | ||
| 308 | channel_value = | ||
| 309 | (corrected_code * tps80031_gain[channel] * 1000) >> (bits + 3); | ||
| 310 | return channel_value; | ||
| 311 | } | ||
| 312 | |||
| 313 | static int tps80031_gpadc_wait_conversion_ready( | ||
| 314 | struct tps80031_gpadc_data *gpadc, | ||
| 315 | unsigned int timeout_ms) | ||
| 316 | { | ||
| 317 | int ret; | ||
| 318 | unsigned long timeout; | ||
| 319 | timeout = jiffies + msecs_to_jiffies(timeout_ms); | ||
| 320 | do { | ||
| 321 | uint8_t reg; | ||
| 322 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, CTRL_P1, ®); | ||
| 323 | if (ret < 0) | ||
| 324 | return ret; | ||
| 325 | if (!(reg & GPADC_BUSY) && | ||
| 326 | (reg & GPADC_EOC_SW)) | ||
| 327 | return 0; | ||
| 328 | } while (!time_after(jiffies, timeout)); | ||
| 329 | return -EAGAIN; | ||
| 330 | } | ||
| 331 | |||
| 332 | static inline int tps80031_gpadc_config | ||
| 333 | (struct tps80031_gpadc_data *gpadc, int channel_no) | ||
| 334 | { | ||
| 335 | int ret = 0; | ||
| 336 | |||
| 337 | ret = tps80031_reg_write(gpadc, SLAVE_ID2, TOGGLE1, TOGGLE1_GPADCR); | ||
| 338 | if (ret < 0) | ||
| 339 | return ret; | ||
| 340 | |||
| 341 | ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPSELECT_ISB, channel_no); | ||
| 342 | if (ret < 0) | ||
| 343 | return ret; | ||
| 344 | |||
| 345 | ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPADC_CTRL, 0xef); | ||
| 346 | if (ret < 0) | ||
| 347 | return ret; | ||
| 348 | |||
| 349 | ret = tps80031_reg_write(gpadc, SLAVE_ID1, MISC1, 0x02); | ||
| 350 | if (ret < 0) | ||
| 351 | return ret; | ||
| 352 | |||
| 353 | return ret; | ||
| 354 | } | ||
| 355 | |||
| 356 | int tps80031_gpadc_conversion(int channel_no) | ||
| 357 | { | ||
| 358 | int ret = 0; | ||
| 359 | int read_value; | ||
| 360 | |||
| 361 | mutex_lock(&the_gpadc->lock); | ||
| 362 | |||
| 363 | ret = tps80031_gpadc_config(the_gpadc, channel_no); | ||
| 364 | if (ret < 0) | ||
| 365 | goto err; | ||
| 366 | |||
| 367 | /* start ADC conversion */ | ||
| 368 | ret = tps80031_reg_write(the_gpadc, SLAVE_ID2, CTRL_P1, CTRL_P1_SP1); | ||
| 369 | if (ret < 0) | ||
| 370 | goto err; | ||
| 371 | |||
| 372 | /* Wait until conversion is ready (ctrl register returns EOC) */ | ||
| 373 | ret = tps80031_gpadc_wait_conversion_ready(the_gpadc, 5); | ||
| 374 | if (ret) { | ||
| 375 | dev_dbg(the_gpadc->dev, "conversion timeout!\n"); | ||
| 376 | goto err; | ||
| 377 | } | ||
| 378 | |||
| 379 | read_value = tps80031_gpadc_read_channels(the_gpadc, channel_no); | ||
| 380 | mutex_unlock(&the_gpadc->lock); | ||
| 381 | return read_value; | ||
| 382 | err: | ||
| 383 | mutex_unlock(&the_gpadc->lock); | ||
| 384 | return ret; | ||
| 385 | } | ||
| 386 | EXPORT_SYMBOL_GPL(tps80031_gpadc_conversion); | ||
| 387 | |||
| 388 | static SENSOR_DEVICE_ATTR(in0_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 0); | ||
| 389 | static SENSOR_DEVICE_ATTR(in0_offset, S_IRUGO|S_IWUSR, | ||
| 390 | show_offset, set_offset, 0); | ||
| 391 | static SENSOR_DEVICE_ATTR(in1_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 1); | ||
| 392 | static SENSOR_DEVICE_ATTR(in1_offset, S_IRUGO|S_IWUSR, | ||
| 393 | show_offset, set_offset, 1); | ||
| 394 | static SENSOR_DEVICE_ATTR(in2_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 2); | ||
| 395 | static SENSOR_DEVICE_ATTR(in2_offset, S_IRUGO|S_IWUSR, | ||
| 396 | show_offset, set_offset, 2); | ||
| 397 | static SENSOR_DEVICE_ATTR(in3_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 3); | ||
| 398 | static SENSOR_DEVICE_ATTR(in3_offset, S_IRUGO|S_IWUSR, | ||
| 399 | show_offset, set_offset, 3); | ||
| 400 | static SENSOR_DEVICE_ATTR(in4_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 4); | ||
| 401 | static SENSOR_DEVICE_ATTR(in4_offset, S_IRUGO|S_IWUSR, | ||
| 402 | show_offset, set_offset, 4); | ||
| 403 | static SENSOR_DEVICE_ATTR(in5_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 5); | ||
| 404 | static SENSOR_DEVICE_ATTR(in5_offset, S_IRUGO|S_IWUSR, | ||
| 405 | show_offset, set_offset, 5); | ||
| 406 | static SENSOR_DEVICE_ATTR(in6_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 6); | ||
| 407 | static SENSOR_DEVICE_ATTR(in6_offset, S_IRUGO|S_IWUSR, | ||
| 408 | show_offset, set_offset, 6); | ||
| 409 | static SENSOR_DEVICE_ATTR(in7_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 7); | ||
| 410 | static SENSOR_DEVICE_ATTR(in7_offset, S_IRUGO|S_IWUSR, | ||
| 411 | show_offset, set_offset, 7); | ||
| 412 | static SENSOR_DEVICE_ATTR(in8_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 8); | ||
| 413 | static SENSOR_DEVICE_ATTR(in8_offset, S_IRUGO|S_IWUSR, | ||
| 414 | show_offset, set_offset, 8); | ||
| 415 | static SENSOR_DEVICE_ATTR(in9_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 9); | ||
| 416 | static SENSOR_DEVICE_ATTR(in9_offset, S_IRUGO|S_IWUSR, | ||
| 417 | show_offset, set_offset, 9); | ||
| 418 | static SENSOR_DEVICE_ATTR(in10_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 10); | ||
| 419 | static SENSOR_DEVICE_ATTR(in10_offset, S_IRUGO|S_IWUSR, | ||
| 420 | show_offset, set_offset, 10); | ||
| 421 | static SENSOR_DEVICE_ATTR(in11_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 11); | ||
| 422 | static SENSOR_DEVICE_ATTR(in11_offset, S_IRUGO|S_IWUSR, | ||
| 423 | show_offset, set_offset, 11); | ||
| 424 | static SENSOR_DEVICE_ATTR(in12_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 12); | ||
| 425 | static SENSOR_DEVICE_ATTR(in12_offset, S_IRUGO|S_IWUSR, | ||
| 426 | show_offset, set_offset, 12); | ||
| 427 | static SENSOR_DEVICE_ATTR(in13_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 13); | ||
| 428 | static SENSOR_DEVICE_ATTR(in13_offset, S_IRUGO|S_IWUSR, | ||
| 429 | show_offset, set_offset, 13); | ||
| 430 | static SENSOR_DEVICE_ATTR(in14_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 14); | ||
| 431 | static SENSOR_DEVICE_ATTR(in14_offset, S_IRUGO|S_IWUSR, | ||
| 432 | show_offset, set_offset, 14); | ||
| 433 | static SENSOR_DEVICE_ATTR(in15_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 15); | ||
| 434 | static SENSOR_DEVICE_ATTR(in15_offset, S_IRUGO|S_IWUSR, | ||
| 435 | show_offset, set_offset, 15); | ||
| 436 | static SENSOR_DEVICE_ATTR(in16_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 16); | ||
| 437 | static SENSOR_DEVICE_ATTR(in16_offset, S_IRUGO|S_IWUSR, | ||
| 438 | show_offset, set_offset, 16); | ||
| 439 | |||
| 440 | #define IN_ATTRS(X)\ | ||
| 441 | &sensor_dev_attr_in##X##_gain.dev_attr.attr, \ | ||
| 442 | &sensor_dev_attr_in##X##_offset.dev_attr.attr \ | ||
| 443 | |||
| 444 | static struct attribute *tps80031_gpadc_attributes[] = { | ||
| 445 | IN_ATTRS(0), | ||
| 446 | IN_ATTRS(1), | ||
| 447 | IN_ATTRS(2), | ||
| 448 | IN_ATTRS(3), | ||
| 449 | IN_ATTRS(4), | ||
| 450 | IN_ATTRS(5), | ||
| 451 | IN_ATTRS(6), | ||
| 452 | IN_ATTRS(7), | ||
| 453 | IN_ATTRS(8), | ||
| 454 | IN_ATTRS(9), | ||
| 455 | IN_ATTRS(10), | ||
| 456 | IN_ATTRS(11), | ||
| 457 | IN_ATTRS(12), | ||
| 458 | IN_ATTRS(13), | ||
| 459 | IN_ATTRS(14), | ||
| 460 | IN_ATTRS(15), | ||
| 461 | IN_ATTRS(16), | ||
| 462 | NULL | ||
| 463 | }; | ||
| 464 | |||
| 465 | static const struct attribute_group tps80031_gpadc_group = { | ||
| 466 | .attrs = tps80031_gpadc_attributes, | ||
| 467 | }; | ||
| 468 | |||
| 469 | static long tps80031_gpadc_ioctl(struct file *filp, unsigned int cmd, | ||
| 470 | unsigned long arg) | ||
| 471 | { | ||
| 472 | struct tps80031_gpadc_user_parms par; | ||
| 473 | int val, ret, channel_no; | ||
| 474 | |||
| 475 | ret = copy_from_user(&par, (void __user *) arg, sizeof(par)); | ||
| 476 | if (ret) { | ||
| 477 | dev_dbg(the_gpadc->dev, "copy_from_user: %d\n", ret); | ||
| 478 | return -EACCES; | ||
| 479 | } | ||
| 480 | switch (cmd) { | ||
| 481 | case TPS80031_GPADC_IOCX_ADC_RAW_READ: | ||
| 482 | channel_no = par.channel; | ||
| 483 | val = tps80031_gpadc_conversion(channel_no); | ||
| 484 | if (likely(val > 0)) { | ||
| 485 | par.status = 0; | ||
| 486 | par.result = val; | ||
| 487 | } else if (val == 0) { | ||
| 488 | par.status = -ENODATA; | ||
| 489 | } else { | ||
| 490 | par.status = val; | ||
| 491 | } | ||
| 492 | break; | ||
| 493 | default: | ||
| 494 | return -EINVAL; | ||
| 495 | } | ||
| 496 | ret = copy_to_user((void __user *) arg, &par, sizeof(par)); | ||
| 497 | if (ret) { | ||
| 498 | dev_dbg(the_gpadc->dev, "copy_to_user: %d\n", ret); | ||
| 499 | return -EACCES; | ||
| 500 | } | ||
| 501 | return 0; | ||
| 502 | } | ||
| 503 | |||
| 504 | static const struct file_operations tps80031_gpadc_fileops = { | ||
| 505 | .owner = THIS_MODULE, | ||
| 506 | .unlocked_ioctl = tps80031_gpadc_ioctl, | ||
| 507 | }; | ||
| 508 | |||
| 509 | static struct miscdevice tps80031_gpadc_device = { | ||
| 510 | .minor = MISC_DYNAMIC_MINOR, | ||
| 511 | .name = "tps80031-gpadc", | ||
| 512 | .fops = &tps80031_gpadc_fileops | ||
| 513 | }; | ||
| 514 | |||
| 515 | static int __devinit tps80031_gpadc_probe(struct platform_device *pdev) | ||
| 516 | { | ||
| 517 | struct tps80031_gpadc_data *gpadc; | ||
| 518 | |||
| 519 | s16 delta_error1 = 0, delta_error2 = 0; | ||
| 520 | s16 ideal_code1, ideal_code2; | ||
| 521 | s16 scalar_delta1 = 0, scalar_delta2 = 0; | ||
| 522 | s32 gain_error_1; | ||
| 523 | s32 offset_error; | ||
| 524 | uint8_t l_delta1, l_delta2, h_delta2; | ||
| 525 | uint8_t l_scalar1, l_scalar2; | ||
| 526 | uint8_t sign; | ||
| 527 | uint8_t index; | ||
| 528 | int ret; | ||
| 529 | |||
| 530 | gpadc = devm_kzalloc(&pdev->dev, sizeof *gpadc, GFP_KERNEL); | ||
| 531 | if (!gpadc) | ||
| 532 | return -ENOMEM; | ||
| 533 | |||
| 534 | gpadc->dev = &pdev->dev; | ||
| 535 | ret = misc_register(&tps80031_gpadc_device); | ||
| 536 | if (ret) { | ||
| 537 | dev_dbg(&pdev->dev, "could not register misc_device\n"); | ||
| 538 | return ret; | ||
| 539 | } | ||
| 540 | |||
| 541 | platform_set_drvdata(pdev, gpadc); | ||
| 542 | mutex_init(&gpadc->lock); | ||
| 543 | |||
| 544 | for (index = 0; index < TPS80031_GPADC_MAX_CHANNELS; index++) { | ||
| 545 | if (~calibration_bit_map & (1 << index)) | ||
| 546 | continue; | ||
| 547 | |||
| 548 | if (~scalar_bit_map & (1 << index)) { | ||
| 549 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, | ||
| 550 | tps80031_trim[index].delta1_addr, &l_scalar1); | ||
| 551 | if (ret < 0) | ||
| 552 | goto err; | ||
| 553 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, | ||
| 554 | tps80031_trim[index].delta2_addr, &l_scalar2); | ||
| 555 | if (ret < 0) | ||
| 556 | goto err; | ||
| 557 | |||
| 558 | l_scalar1 &= tps80031_trim[index].delta1_mask; | ||
| 559 | sign = l_scalar1 & 1; | ||
| 560 | scalar_delta1 = l_scalar1 >> 1; | ||
| 561 | if (sign) | ||
| 562 | scalar_delta1 = 0 - scalar_delta1; | ||
| 563 | l_scalar2 &= tps80031_trim[index].delta2_mask; | ||
| 564 | sign = l_scalar2 & 1; | ||
| 565 | scalar_delta2 = l_scalar2 >> 1; | ||
| 566 | if (sign) | ||
| 567 | scalar_delta2 = 0 - scalar_delta2; | ||
| 568 | } else { | ||
| 569 | scalar_delta1 = 0; | ||
| 570 | scalar_delta2 = 0; | ||
| 571 | } | ||
| 572 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM5, | ||
| 573 | &l_delta1); | ||
| 574 | if (ret < 0) | ||
| 575 | goto err; | ||
| 576 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM6, | ||
| 577 | &l_delta2); | ||
| 578 | if (ret < 0) | ||
| 579 | goto err; | ||
| 580 | ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM19, | ||
| 581 | &h_delta2); | ||
| 582 | if (ret < 0) | ||
| 583 | goto err; | ||
| 584 | |||
| 585 | sign = l_delta1 & 1; | ||
| 586 | |||
| 587 | delta_error1 = l_delta1 >> 1; | ||
| 588 | if (sign) | ||
| 589 | delta_error1 = (0 - delta_error1); | ||
| 590 | sign = l_delta2 & 1; | ||
| 591 | |||
| 592 | delta_error2 = (l_delta2 >> 1) | (h_delta2 << 7); | ||
| 593 | if (sign) | ||
| 594 | delta_error2 = (0 - delta_error2); | ||
| 595 | ideal_code1 = tps80031_ideal[index].code1 * 4; | ||
| 596 | ideal_code2 = tps80031_ideal[index].code2 * 4; | ||
| 597 | |||
| 598 | gain_error_1 = ((delta_error2 + scalar_delta2) - | ||
| 599 | (delta_error1 - scalar_delta1)) * | ||
| 600 | SCALE / (ideal_code2 - ideal_code1); | ||
| 601 | offset_error = (delta_error1 + scalar_delta1) * | ||
| 602 | SCALE - gain_error_1 * ideal_code1; | ||
| 603 | |||
| 604 | tps80031_calib_tbl[index].gain_error = gain_error_1 + SCALE; | ||
| 605 | tps80031_calib_tbl[index].offset_error = offset_error; | ||
| 606 | } | ||
| 607 | |||
| 608 | the_gpadc = gpadc; | ||
| 609 | ret = sysfs_create_group(&pdev->dev.kobj, &tps80031_gpadc_group); | ||
| 610 | if (ret) { | ||
| 611 | dev_err(&pdev->dev, "could not create sysfs files\n"); | ||
| 612 | goto err; | ||
| 613 | } | ||
| 614 | return 0; | ||
| 615 | err: | ||
| 616 | misc_deregister(&tps80031_gpadc_device); | ||
| 617 | return ret; | ||
| 618 | } | ||
| 619 | |||
| 620 | static int __devexit tps80031_gpadc_remove(struct platform_device *pdev) | ||
| 621 | { | ||
| 622 | sysfs_remove_group(&pdev->dev.kobj, &tps80031_gpadc_group); | ||
| 623 | misc_deregister(&tps80031_gpadc_device); | ||
| 624 | return 0; | ||
| 625 | } | ||
| 626 | |||
| 627 | static struct platform_driver tps80031_gpadc_driver = { | ||
| 628 | .probe = tps80031_gpadc_probe, | ||
| 629 | .remove = __devexit_p(tps80031_gpadc_remove), | ||
| 630 | .driver = { | ||
| 631 | .name = "tps80031-gpadc", | ||
| 632 | .owner = THIS_MODULE, | ||
| 633 | }, | ||
| 634 | }; | ||
| 635 | |||
| 636 | static int __init tps80031_gpadc_init(void) | ||
| 637 | { | ||
| 638 | return platform_driver_register(&tps80031_gpadc_driver); | ||
| 639 | } | ||
| 640 | |||
| 641 | module_init(tps80031_gpadc_init); | ||
| 642 | |||
| 643 | static void __exit tps80031_gpadc_exit(void) | ||
| 644 | { | ||
| 645 | platform_driver_unregister(&tps80031_gpadc_driver); | ||
| 646 | } | ||
| 647 | |||
| 648 | module_exit(tps80031_gpadc_exit); | ||
| 649 | MODULE_ALIAS("platform:tps80031-gpadc"); | ||
| 650 | MODULE_DESCRIPTION("tps80031 ADC driver"); | ||
diff --git a/drivers/mfd/twl6030-pwm.c b/drivers/mfd/twl6030-pwm.c new file mode 100644 index 00000000000..e8fee147678 --- /dev/null +++ b/drivers/mfd/twl6030-pwm.c | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | /* | ||
| 2 | * twl6030_pwm.c | ||
| 3 | * Driver for PHOENIX (TWL6030) Pulse Width Modulator | ||
| 4 | * | ||
| 5 | * Copyright (C) 2010 Texas Instruments | ||
| 6 | * Author: Hemanth V <hemanthv@ti.com> | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify it | ||
| 9 | * under the terms of the GNU General Public License version 2 as published by | ||
| 10 | * the Free Software Foundation. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 15 | * more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License along with | ||
| 18 | * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <linux/module.h> | ||
| 22 | #include <linux/platform_device.h> | ||
| 23 | #include <linux/i2c/twl.h> | ||
| 24 | #include <linux/slab.h> | ||
| 25 | |||
| 26 | #define LED_PWM_CTRL1 0xF4 | ||
| 27 | #define LED_PWM_CTRL2 0xF5 | ||
| 28 | |||
| 29 | /* Max value for CTRL1 register */ | ||
| 30 | #define PWM_CTRL1_MAX 255 | ||
| 31 | |||
| 32 | /* Pull down disable */ | ||
| 33 | #define PWM_CTRL2_DIS_PD (1 << 6) | ||
| 34 | |||
| 35 | /* Current control 2.5 milli Amps */ | ||
| 36 | #define PWM_CTRL2_CURR_02 (2 << 4) | ||
| 37 | |||
| 38 | /* LED supply source */ | ||
| 39 | #define PWM_CTRL2_SRC_VAC (1 << 2) | ||
| 40 | |||
| 41 | /* LED modes */ | ||
| 42 | #define PWM_CTRL2_MODE_HW (0 << 0) | ||
| 43 | #define PWM_CTRL2_MODE_SW (1 << 0) | ||
| 44 | #define PWM_CTRL2_MODE_DIS (2 << 0) | ||
| 45 | |||
| 46 | #define PWM_CTRL2_MODE_MASK 0x3 | ||
| 47 | |||
| 48 | struct pwm_device { | ||
| 49 | const char *label; | ||
| 50 | unsigned int pwm_id; | ||
| 51 | }; | ||
| 52 | |||
| 53 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) | ||
| 54 | { | ||
| 55 | u8 duty_cycle; | ||
| 56 | int ret; | ||
| 57 | |||
| 58 | if (pwm == NULL || period_ns == 0 || duty_ns > period_ns) | ||
| 59 | return -EINVAL; | ||
| 60 | |||
| 61 | duty_cycle = (duty_ns * PWM_CTRL1_MAX) / period_ns; | ||
| 62 | |||
| 63 | ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, duty_cycle, LED_PWM_CTRL1); | ||
| 64 | |||
| 65 | if (ret < 0) { | ||
| 66 | pr_err("%s: Failed to configure PWM, Error %d\n", | ||
| 67 | pwm->label, ret); | ||
| 68 | return ret; | ||
| 69 | } | ||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | EXPORT_SYMBOL(pwm_config); | ||
| 73 | |||
| 74 | int pwm_enable(struct pwm_device *pwm) | ||
| 75 | { | ||
| 76 | u8 val; | ||
| 77 | int ret; | ||
| 78 | |||
| 79 | ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2); | ||
| 80 | if (ret < 0) { | ||
| 81 | pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret); | ||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | /* Change mode to software control */ | ||
| 86 | val &= ~PWM_CTRL2_MODE_MASK; | ||
| 87 | val |= PWM_CTRL2_MODE_SW; | ||
| 88 | |||
| 89 | ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2); | ||
| 90 | if (ret < 0) { | ||
| 91 | pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret); | ||
| 92 | return ret; | ||
| 93 | } | ||
| 94 | |||
| 95 | twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | EXPORT_SYMBOL(pwm_enable); | ||
| 99 | |||
| 100 | void pwm_disable(struct pwm_device *pwm) | ||
| 101 | { | ||
| 102 | u8 val; | ||
| 103 | int ret; | ||
| 104 | |||
| 105 | ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2); | ||
| 106 | if (ret < 0) { | ||
| 107 | pr_err("%s: Failed to disable PWM, Error %d\n", | ||
| 108 | pwm->label, ret); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | val &= ~PWM_CTRL2_MODE_MASK; | ||
| 113 | val |= PWM_CTRL2_MODE_HW; | ||
| 114 | |||
| 115 | ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2); | ||
| 116 | if (ret < 0) { | ||
| 117 | pr_err("%s: Failed to disable PWM, Error %d\n", | ||
| 118 | pwm->label, ret); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | EXPORT_SYMBOL(pwm_disable); | ||
| 124 | |||
| 125 | struct pwm_device *pwm_request(int pwm_id, const char *label) | ||
| 126 | { | ||
| 127 | u8 val; | ||
| 128 | int ret; | ||
| 129 | struct pwm_device *pwm; | ||
| 130 | |||
| 131 | pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); | ||
| 132 | if (pwm == NULL) { | ||
| 133 | pr_err("%s: failed to allocate memory\n", label); | ||
| 134 | return NULL; | ||
| 135 | } | ||
| 136 | |||
| 137 | pwm->label = label; | ||
| 138 | pwm->pwm_id = pwm_id; | ||
| 139 | |||
| 140 | /* Configure PWM */ | ||
| 141 | val = PWM_CTRL2_DIS_PD | PWM_CTRL2_CURR_02 | PWM_CTRL2_SRC_VAC | | ||
| 142 | PWM_CTRL2_MODE_HW; | ||
| 143 | |||
| 144 | ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2); | ||
| 145 | |||
| 146 | if (ret < 0) { | ||
| 147 | pr_err("%s: Failed to configure PWM, Error %d\n", | ||
| 148 | pwm->label, ret); | ||
| 149 | |||
| 150 | kfree(pwm); | ||
| 151 | return NULL; | ||
| 152 | } | ||
| 153 | |||
| 154 | return pwm; | ||
| 155 | } | ||
| 156 | EXPORT_SYMBOL(pwm_request); | ||
| 157 | |||
| 158 | void pwm_free(struct pwm_device *pwm) | ||
| 159 | { | ||
| 160 | pwm_disable(pwm); | ||
| 161 | kfree(pwm); | ||
| 162 | } | ||
| 163 | EXPORT_SYMBOL(pwm_free); | ||
| 164 | |||
| 165 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mfd/twl6040-core.c b/drivers/mfd/twl6040-core.c new file mode 100644 index 00000000000..24d436c2fe4 --- /dev/null +++ b/drivers/mfd/twl6040-core.c | |||
| @@ -0,0 +1,620 @@ | |||
| 1 | /* | ||
| 2 | * MFD driver for TWL6040 audio device | ||
| 3 | * | ||
| 4 | * Authors: Misael Lopez Cruz <misael.lopez@ti.com> | ||
| 5 | * Jorge Eduardo Candelaria <jorge.candelaria@ti.com> | ||
| 6 | * Peter Ujfalusi <peter.ujfalusi@ti.com> | ||
| 7 | * | ||
| 8 | * Copyright: (C) 2011 Texas Instruments, Inc. | ||
| 9 | * | ||
| 10 | * This program is free software; you can redistribute it and/or modify | ||
| 11 | * it under the terms of the GNU General Public License version 2 as | ||
| 12 | * published by the Free Software Foundation. | ||
| 13 | * | ||
| 14 | * This program is distributed in the hope that it will be useful, but | ||
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU General Public License | ||
| 20 | * along with this program; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
| 22 | * 02110-1301 USA | ||
| 23 | * | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <linux/module.h> | ||
| 27 | #include <linux/types.h> | ||
| 28 | #include <linux/slab.h> | ||
| 29 | #include <linux/kernel.h> | ||
| 30 | #include <linux/platform_device.h> | ||
| 31 | #include <linux/gpio.h> | ||
| 32 | #include <linux/delay.h> | ||
| 33 | #include <linux/i2c/twl.h> | ||
| 34 | #include <linux/mfd/core.h> | ||
| 35 | #include <linux/mfd/twl6040.h> | ||
| 36 | |||
| 37 | static struct platform_device *twl6040_dev; | ||
| 38 | |||
| 39 | int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg) | ||
| 40 | { | ||
| 41 | int ret; | ||
| 42 | u8 val = 0; | ||
| 43 | |||
| 44 | mutex_lock(&twl6040->io_mutex); | ||
| 45 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); | ||
| 46 | if (ret < 0) { | ||
| 47 | mutex_unlock(&twl6040->io_mutex); | ||
| 48 | return ret; | ||
| 49 | } | ||
| 50 | mutex_unlock(&twl6040->io_mutex); | ||
| 51 | |||
| 52 | return val; | ||
| 53 | } | ||
| 54 | EXPORT_SYMBOL(twl6040_reg_read); | ||
| 55 | |||
| 56 | int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val) | ||
| 57 | { | ||
| 58 | int ret; | ||
| 59 | |||
| 60 | mutex_lock(&twl6040->io_mutex); | ||
| 61 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); | ||
| 62 | mutex_unlock(&twl6040->io_mutex); | ||
| 63 | |||
| 64 | return ret; | ||
| 65 | } | ||
| 66 | EXPORT_SYMBOL(twl6040_reg_write); | ||
| 67 | |||
| 68 | int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) | ||
| 69 | { | ||
| 70 | int ret; | ||
| 71 | u8 val; | ||
| 72 | |||
| 73 | mutex_lock(&twl6040->io_mutex); | ||
| 74 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); | ||
| 75 | if (ret) | ||
| 76 | goto out; | ||
| 77 | |||
| 78 | val |= mask; | ||
| 79 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); | ||
| 80 | out: | ||
| 81 | mutex_unlock(&twl6040->io_mutex); | ||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | EXPORT_SYMBOL(twl6040_set_bits); | ||
| 85 | |||
| 86 | int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) | ||
| 87 | { | ||
| 88 | int ret; | ||
| 89 | u8 val; | ||
| 90 | |||
| 91 | mutex_lock(&twl6040->io_mutex); | ||
| 92 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); | ||
| 93 | if (ret) | ||
| 94 | goto out; | ||
| 95 | |||
| 96 | val &= ~mask; | ||
| 97 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); | ||
| 98 | out: | ||
| 99 | mutex_unlock(&twl6040->io_mutex); | ||
| 100 | return ret; | ||
| 101 | } | ||
| 102 | EXPORT_SYMBOL(twl6040_clear_bits); | ||
| 103 | |||
| 104 | /* twl6040 codec manual power-up sequence */ | ||
| 105 | static int twl6040_power_up(struct twl6040 *twl6040) | ||
| 106 | { | ||
| 107 | u8 ldoctl, ncpctl, lppllctl; | ||
| 108 | int ret; | ||
| 109 | |||
| 110 | /* enable high-side LDO, reference system and internal oscillator */ | ||
| 111 | ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA; | ||
| 112 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 113 | if (ret) | ||
| 114 | return ret; | ||
| 115 | usleep_range(10000, 10500); | ||
| 116 | |||
| 117 | /* enable negative charge pump */ | ||
| 118 | ncpctl = TWL6040_NCPENA; | ||
| 119 | ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | ||
| 120 | if (ret) | ||
| 121 | goto ncp_err; | ||
| 122 | usleep_range(1000, 1500); | ||
| 123 | |||
| 124 | /* enable low-side LDO */ | ||
| 125 | ldoctl |= TWL6040_LSLDOENA; | ||
| 126 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 127 | if (ret) | ||
| 128 | goto lsldo_err; | ||
| 129 | usleep_range(1000, 1500); | ||
| 130 | |||
| 131 | /* enable low-power PLL */ | ||
| 132 | lppllctl = TWL6040_LPLLENA; | ||
| 133 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 134 | if (ret) | ||
| 135 | goto lppll_err; | ||
| 136 | usleep_range(5000, 5500); | ||
| 137 | |||
| 138 | /* disable internal oscillator */ | ||
| 139 | ldoctl &= ~TWL6040_OSCENA; | ||
| 140 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 141 | if (ret) | ||
| 142 | goto osc_err; | ||
| 143 | |||
| 144 | return 0; | ||
| 145 | |||
| 146 | osc_err: | ||
| 147 | lppllctl &= ~TWL6040_LPLLENA; | ||
| 148 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 149 | lppll_err: | ||
| 150 | ldoctl &= ~TWL6040_LSLDOENA; | ||
| 151 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 152 | lsldo_err: | ||
| 153 | ncpctl &= ~TWL6040_NCPENA; | ||
| 154 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | ||
| 155 | ncp_err: | ||
| 156 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); | ||
| 157 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 158 | |||
| 159 | return ret; | ||
| 160 | } | ||
| 161 | |||
| 162 | /* twl6040 manual power-down sequence */ | ||
| 163 | static void twl6040_power_down(struct twl6040 *twl6040) | ||
| 164 | { | ||
| 165 | u8 ncpctl, ldoctl, lppllctl; | ||
| 166 | |||
| 167 | ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL); | ||
| 168 | ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL); | ||
| 169 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); | ||
| 170 | |||
| 171 | /* enable internal oscillator */ | ||
| 172 | ldoctl |= TWL6040_OSCENA; | ||
| 173 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 174 | usleep_range(1000, 1500); | ||
| 175 | |||
| 176 | /* disable low-power PLL */ | ||
| 177 | lppllctl &= ~TWL6040_LPLLENA; | ||
| 178 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 179 | |||
| 180 | /* disable low-side LDO */ | ||
| 181 | ldoctl &= ~TWL6040_LSLDOENA; | ||
| 182 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 183 | |||
| 184 | /* disable negative charge pump */ | ||
| 185 | ncpctl &= ~TWL6040_NCPENA; | ||
| 186 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | ||
| 187 | |||
| 188 | /* disable high-side LDO, reference system and internal oscillator */ | ||
| 189 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); | ||
| 190 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | ||
| 191 | } | ||
| 192 | |||
| 193 | static irqreturn_t twl6040_naudint_handler(int irq, void *data) | ||
| 194 | { | ||
| 195 | struct twl6040 *twl6040 = data; | ||
| 196 | u8 intid, status; | ||
| 197 | |||
| 198 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | ||
| 199 | |||
| 200 | if (intid & TWL6040_READYINT) | ||
| 201 | complete(&twl6040->ready); | ||
| 202 | |||
| 203 | if (intid & TWL6040_THINT) { | ||
| 204 | status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS); | ||
| 205 | if (status & TWL6040_TSHUTDET) { | ||
| 206 | dev_warn(&twl6040_dev->dev, | ||
| 207 | "Thermal shutdown, powering-off"); | ||
| 208 | twl6040_power(twl6040, 0); | ||
| 209 | } else { | ||
| 210 | dev_warn(&twl6040_dev->dev, | ||
| 211 | "Leaving thermal shutdown, powering-on"); | ||
| 212 | twl6040_power(twl6040, 1); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | return IRQ_HANDLED; | ||
| 217 | } | ||
| 218 | |||
| 219 | static int twl6040_power_up_completion(struct twl6040 *twl6040, | ||
| 220 | int naudint) | ||
| 221 | { | ||
| 222 | int time_left; | ||
| 223 | u8 intid; | ||
| 224 | |||
| 225 | time_left = wait_for_completion_timeout(&twl6040->ready, | ||
| 226 | msecs_to_jiffies(144)); | ||
| 227 | if (!time_left) { | ||
| 228 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | ||
| 229 | if (!(intid & TWL6040_READYINT)) { | ||
| 230 | dev_err(&twl6040_dev->dev, | ||
| 231 | "timeout waiting for READYINT\n"); | ||
| 232 | return -ETIMEDOUT; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | return 0; | ||
| 237 | } | ||
| 238 | |||
| 239 | int twl6040_power(struct twl6040 *twl6040, int on) | ||
| 240 | { | ||
| 241 | int audpwron = twl6040->audpwron; | ||
| 242 | int naudint = twl6040->irq; | ||
| 243 | int ret = 0; | ||
| 244 | |||
| 245 | mutex_lock(&twl6040->mutex); | ||
| 246 | |||
| 247 | if (on) { | ||
| 248 | /* already powered-up */ | ||
| 249 | if (twl6040->power_count++) | ||
| 250 | goto out; | ||
| 251 | |||
| 252 | if (gpio_is_valid(audpwron)) { | ||
| 253 | /* use AUDPWRON line */ | ||
| 254 | gpio_set_value(audpwron, 1); | ||
| 255 | /* wait for power-up completion */ | ||
| 256 | ret = twl6040_power_up_completion(twl6040, naudint); | ||
| 257 | if (ret) { | ||
| 258 | dev_err(&twl6040_dev->dev, | ||
| 259 | "automatic power-down failed\n"); | ||
| 260 | twl6040->power_count = 0; | ||
| 261 | goto out; | ||
| 262 | } | ||
| 263 | } else { | ||
| 264 | /* use manual power-up sequence */ | ||
| 265 | ret = twl6040_power_up(twl6040); | ||
| 266 | if (ret) { | ||
| 267 | dev_err(&twl6040_dev->dev, | ||
| 268 | "manual power-up failed\n"); | ||
| 269 | twl6040->power_count = 0; | ||
| 270 | goto out; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | /* Default PLL configuration after power up */ | ||
| 274 | twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL; | ||
| 275 | twl6040->sysclk = 19200000; | ||
| 276 | } else { | ||
| 277 | /* already powered-down */ | ||
| 278 | if (!twl6040->power_count) { | ||
| 279 | dev_err(&twl6040_dev->dev, | ||
| 280 | "device is already powered-off\n"); | ||
| 281 | ret = -EPERM; | ||
| 282 | goto out; | ||
| 283 | } | ||
| 284 | |||
| 285 | if (--twl6040->power_count) | ||
| 286 | goto out; | ||
| 287 | |||
| 288 | if (gpio_is_valid(audpwron)) { | ||
| 289 | /* use AUDPWRON line */ | ||
| 290 | gpio_set_value(audpwron, 0); | ||
| 291 | |||
| 292 | /* power-down sequence latency */ | ||
| 293 | usleep_range(500, 700); | ||
| 294 | } else { | ||
| 295 | /* use manual power-down sequence */ | ||
| 296 | twl6040_power_down(twl6040); | ||
| 297 | } | ||
| 298 | twl6040->sysclk = 0; | ||
| 299 | } | ||
| 300 | |||
| 301 | out: | ||
| 302 | mutex_unlock(&twl6040->mutex); | ||
| 303 | return ret; | ||
| 304 | } | ||
| 305 | EXPORT_SYMBOL(twl6040_power); | ||
| 306 | |||
| 307 | int twl6040_set_pll(struct twl6040 *twl6040, int pll_id, | ||
| 308 | unsigned int freq_in, unsigned int freq_out) | ||
| 309 | { | ||
| 310 | u8 hppllctl, lppllctl; | ||
| 311 | int ret = 0; | ||
| 312 | |||
| 313 | mutex_lock(&twl6040->mutex); | ||
| 314 | |||
| 315 | hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL); | ||
| 316 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); | ||
| 317 | |||
| 318 | switch (pll_id) { | ||
| 319 | case TWL6040_SYSCLK_SEL_LPPLL: | ||
| 320 | /* low-power PLL divider */ | ||
| 321 | switch (freq_out) { | ||
| 322 | case 17640000: | ||
| 323 | lppllctl |= TWL6040_LPLLFIN; | ||
| 324 | break; | ||
| 325 | case 19200000: | ||
| 326 | lppllctl &= ~TWL6040_LPLLFIN; | ||
| 327 | break; | ||
| 328 | default: | ||
| 329 | dev_err(&twl6040_dev->dev, | ||
| 330 | "freq_out %d not supported\n", freq_out); | ||
| 331 | ret = -EINVAL; | ||
| 332 | goto pll_out; | ||
| 333 | } | ||
| 334 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 335 | |||
| 336 | switch (freq_in) { | ||
| 337 | case 32768: | ||
| 338 | lppllctl |= TWL6040_LPLLENA; | ||
| 339 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | ||
| 340 | lppllctl); | ||
| 341 | mdelay(5); | ||
| 342 | lppllctl &= ~TWL6040_HPLLSEL; | ||
| 343 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | ||
| 344 | lppllctl); | ||
| 345 | hppllctl &= ~TWL6040_HPLLENA; | ||
| 346 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, | ||
| 347 | hppllctl); | ||
| 348 | break; | ||
| 349 | default: | ||
| 350 | dev_err(&twl6040_dev->dev, | ||
| 351 | "freq_in %d not supported\n", freq_in); | ||
| 352 | ret = -EINVAL; | ||
| 353 | goto pll_out; | ||
| 354 | } | ||
| 355 | break; | ||
| 356 | case TWL6040_SYSCLK_SEL_HPPLL: | ||
| 357 | /* high-performance PLL can provide only 19.2 MHz */ | ||
| 358 | if (freq_out != 19200000) { | ||
| 359 | dev_err(&twl6040_dev->dev, | ||
| 360 | "freq_out %d not supported\n", freq_out); | ||
| 361 | ret = -EINVAL; | ||
| 362 | goto pll_out; | ||
| 363 | } | ||
| 364 | |||
| 365 | hppllctl &= ~TWL6040_MCLK_MSK; | ||
| 366 | |||
| 367 | switch (freq_in) { | ||
| 368 | case 12000000: | ||
| 369 | /* PLL enabled, active mode */ | ||
| 370 | hppllctl |= TWL6040_MCLK_12000KHZ | | ||
| 371 | TWL6040_HPLLENA; | ||
| 372 | break; | ||
| 373 | case 19200000: | ||
| 374 | /* | ||
| 375 | * PLL disabled | ||
| 376 | * (enable PLL if MCLK jitter quality | ||
| 377 | * doesn't meet specification) | ||
| 378 | */ | ||
| 379 | hppllctl |= TWL6040_MCLK_19200KHZ; | ||
| 380 | break; | ||
| 381 | case 26000000: | ||
| 382 | /* PLL enabled, active mode */ | ||
| 383 | hppllctl |= TWL6040_MCLK_26000KHZ | | ||
| 384 | TWL6040_HPLLENA; | ||
| 385 | break; | ||
| 386 | case 38400000: | ||
| 387 | /* PLL enabled, active mode */ | ||
| 388 | hppllctl |= TWL6040_MCLK_38400KHZ | | ||
| 389 | TWL6040_HPLLENA; | ||
| 390 | break; | ||
| 391 | default: | ||
| 392 | dev_err(&twl6040_dev->dev, | ||
| 393 | "freq_in %d not supported\n", freq_in); | ||
| 394 | ret = -EINVAL; | ||
| 395 | goto pll_out; | ||
| 396 | } | ||
| 397 | |||
| 398 | /* enable clock slicer to ensure input waveform is square */ | ||
| 399 | hppllctl |= TWL6040_HPLLSQRENA; | ||
| 400 | |||
| 401 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, hppllctl); | ||
| 402 | usleep_range(500, 700); | ||
| 403 | lppllctl |= TWL6040_HPLLSEL; | ||
| 404 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 405 | lppllctl &= ~TWL6040_LPLLENA; | ||
| 406 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | ||
| 407 | break; | ||
| 408 | default: | ||
| 409 | dev_err(&twl6040_dev->dev, "unknown pll id %d\n", pll_id); | ||
| 410 | ret = -EINVAL; | ||
| 411 | goto pll_out; | ||
| 412 | } | ||
| 413 | |||
| 414 | twl6040->sysclk = freq_out; | ||
| 415 | twl6040->pll = pll_id; | ||
| 416 | |||
| 417 | pll_out: | ||
| 418 | mutex_unlock(&twl6040->mutex); | ||
| 419 | return ret; | ||
| 420 | } | ||
| 421 | EXPORT_SYMBOL(twl6040_set_pll); | ||
| 422 | |||
| 423 | int twl6040_get_pll(struct twl6040 *twl6040) | ||
| 424 | { | ||
| 425 | if (twl6040->power_count) | ||
| 426 | return twl6040->pll; | ||
| 427 | else | ||
| 428 | return -ENODEV; | ||
| 429 | } | ||
| 430 | EXPORT_SYMBOL(twl6040_get_pll); | ||
| 431 | |||
| 432 | unsigned int twl6040_get_sysclk(struct twl6040 *twl6040) | ||
| 433 | { | ||
| 434 | return twl6040->sysclk; | ||
| 435 | } | ||
| 436 | EXPORT_SYMBOL(twl6040_get_sysclk); | ||
| 437 | |||
| 438 | static struct resource twl6040_vibra_rsrc[] = { | ||
| 439 | { | ||
| 440 | .flags = IORESOURCE_IRQ, | ||
| 441 | }, | ||
| 442 | }; | ||
| 443 | |||
| 444 | static struct resource twl6040_codec_rsrc[] = { | ||
| 445 | { | ||
| 446 | .flags = IORESOURCE_IRQ, | ||
| 447 | }, | ||
| 448 | }; | ||
| 449 | |||
| 450 | static int __devinit twl6040_probe(struct platform_device *pdev) | ||
| 451 | { | ||
| 452 | struct twl4030_audio_data *pdata = pdev->dev.platform_data; | ||
| 453 | struct twl6040 *twl6040; | ||
| 454 | struct mfd_cell *cell = NULL; | ||
| 455 | int ret, children = 0; | ||
| 456 | |||
| 457 | if (!pdata) { | ||
| 458 | dev_err(&pdev->dev, "Platform data is missing\n"); | ||
| 459 | return -EINVAL; | ||
| 460 | } | ||
| 461 | |||
| 462 | /* In order to operate correctly we need valid interrupt config */ | ||
| 463 | if (!pdata->naudint_irq || !pdata->irq_base) { | ||
| 464 | dev_err(&pdev->dev, "Invalid IRQ configuration\n"); | ||
| 465 | return -EINVAL; | ||
| 466 | } | ||
| 467 | |||
| 468 | twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL); | ||
| 469 | if (!twl6040) | ||
| 470 | return -ENOMEM; | ||
| 471 | |||
| 472 | platform_set_drvdata(pdev, twl6040); | ||
| 473 | |||
| 474 | twl6040_dev = pdev; | ||
| 475 | twl6040->dev = &pdev->dev; | ||
| 476 | twl6040->audpwron = pdata->audpwron_gpio; | ||
| 477 | twl6040->irq = pdata->naudint_irq; | ||
| 478 | twl6040->irq_base = pdata->irq_base; | ||
| 479 | |||
| 480 | mutex_init(&twl6040->mutex); | ||
| 481 | mutex_init(&twl6040->io_mutex); | ||
| 482 | init_completion(&twl6040->ready); | ||
| 483 | |||
| 484 | twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV); | ||
| 485 | |||
| 486 | if (gpio_is_valid(twl6040->audpwron)) { | ||
| 487 | ret = gpio_request(twl6040->audpwron, "audpwron"); | ||
| 488 | if (ret) | ||
| 489 | goto gpio1_err; | ||
| 490 | |||
| 491 | ret = gpio_direction_output(twl6040->audpwron, 0); | ||
| 492 | if (ret) | ||
| 493 | goto gpio2_err; | ||
| 494 | } | ||
| 495 | |||
| 496 | /* ERRATA: Automatic power-up is not possible in ES1.0 */ | ||
| 497 | if (twl6040->rev == TWL6040_REV_ES1_0) | ||
| 498 | twl6040->audpwron = -EINVAL; | ||
| 499 | |||
| 500 | /* codec interrupt */ | ||
| 501 | ret = twl6040_irq_init(twl6040); | ||
| 502 | if (ret) | ||
| 503 | goto gpio2_err; | ||
| 504 | |||
| 505 | ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY, | ||
| 506 | NULL, twl6040_naudint_handler, 0, | ||
| 507 | "twl6040_irq_ready", twl6040); | ||
| 508 | if (ret) { | ||
| 509 | dev_err(twl6040->dev, "READY IRQ request failed: %d\n", | ||
| 510 | ret); | ||
| 511 | goto irq_err; | ||
| 512 | } | ||
| 513 | |||
| 514 | /* dual-access registers controlled by I2C only */ | ||
| 515 | twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL); | ||
| 516 | |||
| 517 | if (pdata->codec) { | ||
| 518 | int irq = twl6040->irq_base + TWL6040_IRQ_PLUG; | ||
| 519 | |||
| 520 | cell = &twl6040->cells[children]; | ||
| 521 | cell->name = "twl6040-codec"; | ||
| 522 | twl6040_codec_rsrc[0].start = irq; | ||
| 523 | twl6040_codec_rsrc[0].end = irq; | ||
| 524 | cell->resources = twl6040_codec_rsrc; | ||
| 525 | cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc); | ||
| 526 | cell->platform_data = pdata->codec; | ||
| 527 | cell->pdata_size = sizeof(*pdata->codec); | ||
| 528 | children++; | ||
| 529 | } | ||
| 530 | |||
| 531 | if (pdata->vibra) { | ||
| 532 | int irq = twl6040->irq_base + TWL6040_IRQ_VIB; | ||
| 533 | |||
| 534 | cell = &twl6040->cells[children]; | ||
| 535 | cell->name = "twl6040-vibra"; | ||
| 536 | twl6040_vibra_rsrc[0].start = irq; | ||
| 537 | twl6040_vibra_rsrc[0].end = irq; | ||
| 538 | cell->resources = twl6040_vibra_rsrc; | ||
| 539 | cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc); | ||
| 540 | |||
| 541 | cell->platform_data = pdata->vibra; | ||
| 542 | cell->pdata_size = sizeof(*pdata->vibra); | ||
| 543 | children++; | ||
| 544 | } | ||
| 545 | |||
| 546 | if (children) { | ||
| 547 | ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells, | ||
| 548 | children, NULL, 0); | ||
| 549 | if (ret) | ||
| 550 | goto mfd_err; | ||
| 551 | } else { | ||
| 552 | dev_err(&pdev->dev, "No platform data found for children\n"); | ||
| 553 | ret = -ENODEV; | ||
| 554 | goto mfd_err; | ||
| 555 | } | ||
| 556 | |||
| 557 | return 0; | ||
| 558 | |||
| 559 | mfd_err: | ||
| 560 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); | ||
| 561 | irq_err: | ||
| 562 | twl6040_irq_exit(twl6040); | ||
| 563 | gpio2_err: | ||
| 564 | if (gpio_is_valid(twl6040->audpwron)) | ||
| 565 | gpio_free(twl6040->audpwron); | ||
| 566 | gpio1_err: | ||
| 567 | platform_set_drvdata(pdev, NULL); | ||
| 568 | kfree(twl6040); | ||
| 569 | twl6040_dev = NULL; | ||
| 570 | return ret; | ||
| 571 | } | ||
| 572 | |||
| 573 | static int __devexit twl6040_remove(struct platform_device *pdev) | ||
| 574 | { | ||
| 575 | struct twl6040 *twl6040 = platform_get_drvdata(pdev); | ||
| 576 | |||
| 577 | if (twl6040->power_count) | ||
| 578 | twl6040_power(twl6040, 0); | ||
| 579 | |||
| 580 | if (gpio_is_valid(twl6040->audpwron)) | ||
| 581 | gpio_free(twl6040->audpwron); | ||
| 582 | |||
| 583 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); | ||
| 584 | twl6040_irq_exit(twl6040); | ||
| 585 | |||
| 586 | mfd_remove_devices(&pdev->dev); | ||
| 587 | platform_set_drvdata(pdev, NULL); | ||
| 588 | kfree(twl6040); | ||
| 589 | twl6040_dev = NULL; | ||
| 590 | |||
| 591 | return 0; | ||
| 592 | } | ||
| 593 | |||
| 594 | static struct platform_driver twl6040_driver = { | ||
| 595 | .probe = twl6040_probe, | ||
| 596 | .remove = __devexit_p(twl6040_remove), | ||
| 597 | .driver = { | ||
| 598 | .owner = THIS_MODULE, | ||
| 599 | .name = "twl6040", | ||
| 600 | }, | ||
| 601 | }; | ||
| 602 | |||
| 603 | static int __devinit twl6040_init(void) | ||
| 604 | { | ||
| 605 | return platform_driver_register(&twl6040_driver); | ||
| 606 | } | ||
| 607 | module_init(twl6040_init); | ||
| 608 | |||
| 609 | static void __devexit twl6040_exit(void) | ||
| 610 | { | ||
| 611 | platform_driver_unregister(&twl6040_driver); | ||
| 612 | } | ||
| 613 | |||
| 614 | module_exit(twl6040_exit); | ||
| 615 | |||
| 616 | MODULE_DESCRIPTION("TWL6040 MFD"); | ||
| 617 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); | ||
| 618 | MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>"); | ||
| 619 | MODULE_LICENSE("GPL"); | ||
| 620 | MODULE_ALIAS("platform:twl6040"); | ||
diff --git a/drivers/mfd/twl6040-irq.c b/drivers/mfd/twl6040-irq.c new file mode 100644 index 00000000000..b3f8ddaa28a --- /dev/null +++ b/drivers/mfd/twl6040-irq.c | |||
| @@ -0,0 +1,191 @@ | |||
| 1 | /* | ||
| 2 | * Interrupt controller support for TWL6040 | ||
| 3 | * | ||
| 4 | * Author: Misael Lopez Cruz <misael.lopez@ti.com> | ||
| 5 | * | ||
| 6 | * Copyright: (C) 2011 Texas Instruments, Inc. | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License version 2 as | ||
| 10 | * published by the Free Software Foundation. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, but | ||
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License | ||
| 18 | * along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
| 20 | * 02110-1301 USA | ||
| 21 | * | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <linux/kernel.h> | ||
| 25 | #include <linux/module.h> | ||
| 26 | #include <linux/irq.h> | ||
| 27 | #include <linux/interrupt.h> | ||
| 28 | #include <linux/mfd/core.h> | ||
| 29 | #include <linux/mfd/twl6040.h> | ||
| 30 | |||
| 31 | struct twl6040_irq_data { | ||
| 32 | int mask; | ||
| 33 | int status; | ||
| 34 | }; | ||
| 35 | |||
| 36 | static struct twl6040_irq_data twl6040_irqs[] = { | ||
| 37 | { | ||
| 38 | .mask = TWL6040_THMSK, | ||
| 39 | .status = TWL6040_THINT, | ||
| 40 | }, | ||
| 41 | { | ||
| 42 | .mask = TWL6040_PLUGMSK, | ||
| 43 | .status = TWL6040_PLUGINT | TWL6040_UNPLUGINT, | ||
| 44 | }, | ||
| 45 | { | ||
| 46 | .mask = TWL6040_HOOKMSK, | ||
| 47 | .status = TWL6040_HOOKINT, | ||
| 48 | }, | ||
| 49 | { | ||
| 50 | .mask = TWL6040_HFMSK, | ||
| 51 | .status = TWL6040_HFINT, | ||
| 52 | }, | ||
| 53 | { | ||
| 54 | .mask = TWL6040_VIBMSK, | ||
| 55 | .status = TWL6040_VIBINT, | ||
| 56 | }, | ||
| 57 | { | ||
| 58 | .mask = TWL6040_READYMSK, | ||
| 59 | .status = TWL6040_READYINT, | ||
| 60 | }, | ||
| 61 | }; | ||
| 62 | |||
| 63 | static inline | ||
| 64 | struct twl6040_irq_data *irq_to_twl6040_irq(struct twl6040 *twl6040, | ||
| 65 | int irq) | ||
| 66 | { | ||
| 67 | return &twl6040_irqs[irq - twl6040->irq_base]; | ||
| 68 | } | ||
| 69 | |||
| 70 | static void twl6040_irq_lock(struct irq_data *data) | ||
| 71 | { | ||
| 72 | struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data); | ||
| 73 | |||
| 74 | mutex_lock(&twl6040->irq_mutex); | ||
| 75 | } | ||
| 76 | |||
| 77 | static void twl6040_irq_sync_unlock(struct irq_data *data) | ||
| 78 | { | ||
| 79 | struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data); | ||
| 80 | |||
| 81 | /* write back to hardware any change in irq mask */ | ||
| 82 | if (twl6040->irq_masks_cur != twl6040->irq_masks_cache) { | ||
| 83 | twl6040->irq_masks_cache = twl6040->irq_masks_cur; | ||
| 84 | twl6040_reg_write(twl6040, TWL6040_REG_INTMR, | ||
| 85 | twl6040->irq_masks_cur); | ||
| 86 | } | ||
| 87 | |||
| 88 | mutex_unlock(&twl6040->irq_mutex); | ||
| 89 | } | ||
| 90 | |||
| 91 | static void twl6040_irq_enable(struct irq_data *data) | ||
| 92 | { | ||
| 93 | struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data); | ||
| 94 | struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040, | ||
| 95 | data->irq); | ||
| 96 | |||
| 97 | twl6040->irq_masks_cur &= ~irq_data->mask; | ||
| 98 | } | ||
| 99 | |||
| 100 | static void twl6040_irq_disable(struct irq_data *data) | ||
| 101 | { | ||
| 102 | struct twl6040 *twl6040 = irq_data_get_irq_chip_data(data); | ||
| 103 | struct twl6040_irq_data *irq_data = irq_to_twl6040_irq(twl6040, | ||
| 104 | data->irq); | ||
| 105 | |||
| 106 | twl6040->irq_masks_cur |= irq_data->mask; | ||
| 107 | } | ||
| 108 | |||
| 109 | static struct irq_chip twl6040_irq_chip = { | ||
| 110 | .name = "twl6040", | ||
| 111 | .irq_bus_lock = twl6040_irq_lock, | ||
| 112 | .irq_bus_sync_unlock = twl6040_irq_sync_unlock, | ||
| 113 | .irq_enable = twl6040_irq_enable, | ||
| 114 | .irq_disable = twl6040_irq_disable, | ||
| 115 | }; | ||
| 116 | |||
| 117 | static irqreturn_t twl6040_irq_thread(int irq, void *data) | ||
| 118 | { | ||
| 119 | struct twl6040 *twl6040 = data; | ||
| 120 | u8 intid; | ||
| 121 | int i; | ||
| 122 | |||
| 123 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | ||
| 124 | |||
| 125 | /* apply masking and report (backwards to handle READYINT first) */ | ||
| 126 | for (i = ARRAY_SIZE(twl6040_irqs) - 1; i >= 0; i--) { | ||
| 127 | if (twl6040->irq_masks_cur & twl6040_irqs[i].mask) | ||
| 128 | intid &= ~twl6040_irqs[i].status; | ||
| 129 | if (intid & twl6040_irqs[i].status) | ||
| 130 | handle_nested_irq(twl6040->irq_base + i); | ||
| 131 | } | ||
| 132 | |||
| 133 | /* ack unmasked irqs */ | ||
| 134 | twl6040_reg_write(twl6040, TWL6040_REG_INTID, intid); | ||
| 135 | |||
| 136 | return IRQ_HANDLED; | ||
| 137 | } | ||
| 138 | |||
| 139 | int twl6040_irq_init(struct twl6040 *twl6040) | ||
| 140 | { | ||
| 141 | int cur_irq, ret; | ||
| 142 | u8 val; | ||
| 143 | |||
| 144 | mutex_init(&twl6040->irq_mutex); | ||
| 145 | |||
| 146 | /* mask the individual interrupt sources */ | ||
| 147 | twl6040->irq_masks_cur = TWL6040_ALLINT_MSK; | ||
| 148 | twl6040->irq_masks_cache = TWL6040_ALLINT_MSK; | ||
| 149 | twl6040_reg_write(twl6040, TWL6040_REG_INTMR, TWL6040_ALLINT_MSK); | ||
| 150 | |||
| 151 | /* Register them with genirq */ | ||
| 152 | for (cur_irq = twl6040->irq_base; | ||
| 153 | cur_irq < twl6040->irq_base + ARRAY_SIZE(twl6040_irqs); | ||
| 154 | cur_irq++) { | ||
| 155 | irq_set_chip_data(cur_irq, twl6040); | ||
| 156 | irq_set_chip_and_handler(cur_irq, &twl6040_irq_chip, | ||
| 157 | handle_level_irq); | ||
| 158 | irq_set_nested_thread(cur_irq, 1); | ||
| 159 | |||
| 160 | /* ARM needs us to explicitly flag the IRQ as valid | ||
| 161 | * and will set them noprobe when we do so. */ | ||
| 162 | #ifdef CONFIG_ARM | ||
| 163 | set_irq_flags(cur_irq, IRQF_VALID); | ||
| 164 | #else | ||
| 165 | irq_set_noprobe(cur_irq); | ||
| 166 | #endif | ||
| 167 | } | ||
| 168 | |||
| 169 | ret = request_threaded_irq(twl6040->irq, NULL, twl6040_irq_thread, | ||
| 170 | IRQF_ONESHOT, "twl6040", twl6040); | ||
| 171 | if (ret) { | ||
| 172 | dev_err(twl6040->dev, "failed to request IRQ %d: %d\n", | ||
| 173 | twl6040->irq, ret); | ||
| 174 | return ret; | ||
| 175 | } | ||
| 176 | |||
| 177 | /* reset interrupts */ | ||
| 178 | val = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | ||
| 179 | |||
| 180 | /* interrupts cleared on write */ | ||
| 181 | twl6040_clear_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_INTCLRMODE); | ||
| 182 | |||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | EXPORT_SYMBOL(twl6040_irq_init); | ||
| 186 | |||
| 187 | void twl6040_irq_exit(struct twl6040 *twl6040) | ||
| 188 | { | ||
| 189 | free_irq(twl6040->irq, twl6040); | ||
| 190 | } | ||
| 191 | EXPORT_SYMBOL(twl6040_irq_exit); | ||
