Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * License Terms: GNU General Public License, version 2 |
| 5 | * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson |
| 6 | * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/irq.h> |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 12 | #include <linux/irqdomain.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/i2c.h> |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 15 | #include <linux/of.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 16 | #include <linux/mfd/core.h> |
Sundar Iyer | c6eda6c | 2010-12-13 09:33:12 +0530 | [diff] [blame] | 17 | #include <linux/mfd/tc3589x.h> |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 18 | |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 19 | #define TC3589x_CLKMODE_MODCTL_SLEEP 0x0 |
| 20 | #define TC3589x_CLKMODE_MODCTL_OPERATION (1 << 0) |
| 21 | |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 22 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 23 | * tc3589x_reg_read() - read a single TC3589x register |
| 24 | * @tc3589x: Device to read from |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 25 | * @reg: Register to read |
| 26 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 27 | int tc3589x_reg_read(struct tc3589x *tc3589x, u8 reg) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 28 | { |
| 29 | int ret; |
| 30 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 31 | ret = i2c_smbus_read_byte_data(tc3589x->i2c, reg); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 32 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 33 | dev_err(tc3589x->dev, "failed to read reg %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 34 | reg, ret); |
| 35 | |
| 36 | return ret; |
| 37 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(tc3589x_reg_read); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 41 | * tc3589x_reg_read() - write a single TC3589x register |
| 42 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 43 | * @reg: Register to read |
| 44 | * @data: Value to write |
| 45 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 46 | int tc3589x_reg_write(struct tc3589x *tc3589x, u8 reg, u8 data) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 47 | { |
| 48 | int ret; |
| 49 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 50 | ret = i2c_smbus_write_byte_data(tc3589x->i2c, reg, data); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 51 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 52 | dev_err(tc3589x->dev, "failed to write reg %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 53 | reg, ret); |
| 54 | |
| 55 | return ret; |
| 56 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 57 | EXPORT_SYMBOL_GPL(tc3589x_reg_write); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 58 | |
| 59 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 60 | * tc3589x_block_read() - read multiple TC3589x registers |
| 61 | * @tc3589x: Device to read from |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 62 | * @reg: First register |
| 63 | * @length: Number of registers |
| 64 | * @values: Buffer to write to |
| 65 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 66 | int tc3589x_block_read(struct tc3589x *tc3589x, u8 reg, u8 length, u8 *values) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 67 | { |
| 68 | int ret; |
| 69 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 70 | ret = i2c_smbus_read_i2c_block_data(tc3589x->i2c, reg, length, values); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 71 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 72 | dev_err(tc3589x->dev, "failed to read regs %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 73 | reg, ret); |
| 74 | |
| 75 | return ret; |
| 76 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 77 | EXPORT_SYMBOL_GPL(tc3589x_block_read); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 80 | * tc3589x_block_write() - write multiple TC3589x registers |
| 81 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 82 | * @reg: First register |
| 83 | * @length: Number of registers |
| 84 | * @values: Values to write |
| 85 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 86 | int tc3589x_block_write(struct tc3589x *tc3589x, u8 reg, u8 length, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 87 | const u8 *values) |
| 88 | { |
| 89 | int ret; |
| 90 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 91 | ret = i2c_smbus_write_i2c_block_data(tc3589x->i2c, reg, length, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 92 | values); |
| 93 | if (ret < 0) |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 94 | dev_err(tc3589x->dev, "failed to write regs %#x: %d\n", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 95 | reg, ret); |
| 96 | |
| 97 | return ret; |
| 98 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 99 | EXPORT_SYMBOL_GPL(tc3589x_block_write); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 102 | * tc3589x_set_bits() - set the value of a bitfield in a TC3589x register |
| 103 | * @tc3589x: Device to write to |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 104 | * @reg: Register to write |
| 105 | * @mask: Mask of bits to set |
| 106 | * @values: Value to set |
| 107 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 108 | int tc3589x_set_bits(struct tc3589x *tc3589x, u8 reg, u8 mask, u8 val) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 109 | { |
| 110 | int ret; |
| 111 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 112 | mutex_lock(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 113 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 114 | ret = tc3589x_reg_read(tc3589x, reg); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 115 | if (ret < 0) |
| 116 | goto out; |
| 117 | |
| 118 | ret &= ~mask; |
| 119 | ret |= val; |
| 120 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 121 | ret = tc3589x_reg_write(tc3589x, reg, ret); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 122 | |
| 123 | out: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 124 | mutex_unlock(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 125 | return ret; |
| 126 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(tc3589x_set_bits); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 128 | |
| 129 | static struct resource gpio_resources[] = { |
| 130 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 131 | .start = TC3589x_INT_GPIIRQ, |
| 132 | .end = TC3589x_INT_GPIIRQ, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 133 | .flags = IORESOURCE_IRQ, |
| 134 | }, |
| 135 | }; |
| 136 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 137 | static struct resource keypad_resources[] = { |
| 138 | { |
| 139 | .start = TC3589x_INT_KBDIRQ, |
| 140 | .end = TC3589x_INT_KBDIRQ, |
| 141 | .flags = IORESOURCE_IRQ, |
| 142 | }, |
| 143 | }; |
| 144 | |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 145 | static struct mfd_cell tc3589x_dev_gpio[] = { |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 146 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 147 | .name = "tc3589x-gpio", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 148 | .num_resources = ARRAY_SIZE(gpio_resources), |
| 149 | .resources = &gpio_resources[0], |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 150 | .of_compatible = "tc3589x-gpio", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 151 | }, |
| 152 | }; |
| 153 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 154 | static struct mfd_cell tc3589x_dev_keypad[] = { |
| 155 | { |
| 156 | .name = "tc3589x-keypad", |
| 157 | .num_resources = ARRAY_SIZE(keypad_resources), |
| 158 | .resources = &keypad_resources[0], |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 159 | .of_compatible = "tc3589x-keypad", |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 160 | }, |
| 161 | }; |
| 162 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 163 | static irqreturn_t tc3589x_irq(int irq, void *data) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 164 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 165 | struct tc3589x *tc3589x = data; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 166 | int status; |
| 167 | |
Sundar Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 168 | again: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 169 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 170 | if (status < 0) |
| 171 | return IRQ_NONE; |
| 172 | |
| 173 | while (status) { |
| 174 | int bit = __ffs(status); |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 175 | int virq = irq_create_mapping(tc3589x->domain, bit); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 176 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 177 | handle_nested_irq(virq); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 178 | status &= ~(1 << bit); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * A dummy read or write (to any register) appears to be necessary to |
| 183 | * have the last interrupt clear (for example, GPIO IC write) take |
Sundar Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 184 | * effect. In such a case, recheck for any interrupt which is still |
| 185 | * pending. |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 186 | */ |
Sundar Iyer | bd77efd | 2010-12-13 09:33:16 +0530 | [diff] [blame] | 187 | status = tc3589x_reg_read(tc3589x, TC3589x_IRQST); |
| 188 | if (status) |
| 189 | goto again; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 190 | |
| 191 | return IRQ_HANDLED; |
| 192 | } |
| 193 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 194 | static int tc3589x_irq_map(struct irq_domain *d, unsigned int virq, |
| 195 | irq_hw_number_t hwirq) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 196 | { |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 197 | struct tc3589x *tc3589x = d->host_data; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 198 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 199 | irq_set_chip_data(virq, tc3589x); |
| 200 | irq_set_chip_and_handler(virq, &dummy_irq_chip, |
| 201 | handle_edge_irq); |
| 202 | irq_set_nested_thread(virq, 1); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 203 | #ifdef CONFIG_ARM |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 204 | set_irq_flags(virq, IRQF_VALID); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 205 | #else |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 206 | irq_set_noprobe(virq); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 207 | #endif |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 212 | static void tc3589x_irq_unmap(struct irq_domain *d, unsigned int virq) |
| 213 | { |
| 214 | #ifdef CONFIG_ARM |
| 215 | set_irq_flags(virq, 0); |
| 216 | #endif |
| 217 | irq_set_chip_and_handler(virq, NULL, NULL); |
| 218 | irq_set_chip_data(virq, NULL); |
| 219 | } |
| 220 | |
| 221 | static struct irq_domain_ops tc3589x_irq_ops = { |
| 222 | .map = tc3589x_irq_map, |
| 223 | .unmap = tc3589x_irq_unmap, |
| 224 | .xlate = irq_domain_xlate_twocell, |
| 225 | }; |
| 226 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 227 | static int tc3589x_irq_init(struct tc3589x *tc3589x, struct device_node *np) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 228 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 229 | int base = tc3589x->irq_base; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 230 | |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 231 | if (base) { |
| 232 | tc3589x->domain = irq_domain_add_legacy( |
| 233 | NULL, TC3589x_NR_INTERNAL_IRQS, base, |
| 234 | 0, &tc3589x_irq_ops, tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 235 | } |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 236 | else { |
| 237 | tc3589x->domain = irq_domain_add_linear( |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 238 | np, TC3589x_NR_INTERNAL_IRQS, |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 239 | &tc3589x_irq_ops, tc3589x); |
| 240 | } |
| 241 | |
| 242 | if (!tc3589x->domain) { |
| 243 | dev_err(tc3589x->dev, "Failed to create irqdomain\n"); |
| 244 | return -ENOSYS; |
| 245 | } |
| 246 | |
| 247 | return 0; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 250 | static int tc3589x_chip_init(struct tc3589x *tc3589x) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 251 | { |
| 252 | int manf, ver, ret; |
| 253 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 254 | manf = tc3589x_reg_read(tc3589x, TC3589x_MANFCODE); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 255 | if (manf < 0) |
| 256 | return manf; |
| 257 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 258 | ver = tc3589x_reg_read(tc3589x, TC3589x_VERSION); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 259 | if (ver < 0) |
| 260 | return ver; |
| 261 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 262 | if (manf != TC3589x_MANFCODE_MAGIC) { |
| 263 | dev_err(tc3589x->dev, "unknown manufacturer: %#x\n", manf); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 264 | return -EINVAL; |
| 265 | } |
| 266 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 267 | dev_info(tc3589x->dev, "manufacturer: %#x, version: %#x\n", manf, ver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 268 | |
Sundar Iyer | 523bc38 | 2010-12-13 09:33:17 +0530 | [diff] [blame] | 269 | /* |
| 270 | * Put everything except the IRQ module into reset; |
| 271 | * also spare the GPIO module for any pin initialization |
| 272 | * done during pre-kernel boot |
| 273 | */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 274 | ret = tc3589x_reg_write(tc3589x, TC3589x_RSTCTRL, |
| 275 | TC3589x_RSTCTRL_TIMRST |
| 276 | | TC3589x_RSTCTRL_ROTRST |
Sundar Iyer | 523bc38 | 2010-12-13 09:33:17 +0530 | [diff] [blame] | 277 | | TC3589x_RSTCTRL_KBDRST); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | /* Clear the reset interrupt. */ |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 282 | return tc3589x_reg_write(tc3589x, TC3589x_RSTINTCLR, 0x1); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 285 | static int tc3589x_device_init(struct tc3589x *tc3589x) |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 286 | { |
| 287 | int ret = 0; |
| 288 | unsigned int blocks = tc3589x->pdata->block; |
| 289 | |
| 290 | if (blocks & TC3589x_BLOCK_GPIO) { |
| 291 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_gpio, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 292 | ARRAY_SIZE(tc3589x_dev_gpio), NULL, |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 293 | tc3589x->irq_base, tc3589x->domain); |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 294 | if (ret) { |
| 295 | dev_err(tc3589x->dev, "failed to add gpio child\n"); |
| 296 | return ret; |
| 297 | } |
| 298 | dev_info(tc3589x->dev, "added gpio block\n"); |
| 299 | } |
| 300 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 301 | if (blocks & TC3589x_BLOCK_KEYPAD) { |
| 302 | ret = mfd_add_devices(tc3589x->dev, -1, tc3589x_dev_keypad, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 303 | ARRAY_SIZE(tc3589x_dev_keypad), NULL, |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 304 | tc3589x->irq_base, tc3589x->domain); |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 305 | if (ret) { |
| 306 | dev_err(tc3589x->dev, "failed to keypad child\n"); |
| 307 | return ret; |
| 308 | } |
| 309 | dev_info(tc3589x->dev, "added keypad block\n"); |
| 310 | } |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 311 | |
Sundar Iyer | 09c730a | 2010-12-21 15:53:31 +0530 | [diff] [blame] | 312 | return ret; |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 313 | } |
| 314 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 315 | static int tc3589x_of_probe(struct device_node *np, |
| 316 | struct tc3589x_platform_data *pdata) |
| 317 | { |
| 318 | struct device_node *child; |
| 319 | |
| 320 | for_each_child_of_node(np, child) { |
| 321 | if (!strcmp(child->name, "tc3589x_gpio")) { |
| 322 | pdata->block |= TC3589x_BLOCK_GPIO; |
| 323 | } |
| 324 | if (!strcmp(child->name, "tc3589x_keypad")) { |
| 325 | pdata->block |= TC3589x_BLOCK_KEYPAD; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 332 | static int tc3589x_probe(struct i2c_client *i2c, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 333 | const struct i2c_device_id *id) |
| 334 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 335 | struct tc3589x_platform_data *pdata = i2c->dev.platform_data; |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 336 | struct device_node *np = i2c->dev.of_node; |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 337 | struct tc3589x *tc3589x; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 338 | int ret; |
| 339 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 340 | if (!pdata) { |
| 341 | if (np) { |
| 342 | pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); |
| 343 | if (!pdata) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | ret = tc3589x_of_probe(np, pdata); |
| 347 | if (ret) |
| 348 | return ret; |
| 349 | } |
| 350 | else { |
| 351 | dev_err(&i2c->dev, "No platform data or DT found\n"); |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | } |
| 355 | |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 356 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
| 357 | | I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 358 | return -EIO; |
| 359 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 360 | tc3589x = kzalloc(sizeof(struct tc3589x), GFP_KERNEL); |
| 361 | if (!tc3589x) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 362 | return -ENOMEM; |
| 363 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 364 | mutex_init(&tc3589x->lock); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 365 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 366 | tc3589x->dev = &i2c->dev; |
| 367 | tc3589x->i2c = i2c; |
| 368 | tc3589x->pdata = pdata; |
| 369 | tc3589x->irq_base = pdata->irq_base; |
| 370 | tc3589x->num_gpio = id->driver_data; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 371 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 372 | i2c_set_clientdata(i2c, tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 373 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 374 | ret = tc3589x_chip_init(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 375 | if (ret) |
| 376 | goto out_free; |
| 377 | |
Lee Jones | a435ae1 | 2012-09-07 12:14:57 +0100 | [diff] [blame] | 378 | ret = tc3589x_irq_init(tc3589x, np); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 379 | if (ret) |
| 380 | goto out_free; |
| 381 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 382 | ret = request_threaded_irq(tc3589x->i2c->irq, NULL, tc3589x_irq, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 383 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 384 | "tc3589x", tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 385 | if (ret) { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 386 | dev_err(tc3589x->dev, "failed to request IRQ: %d\n", ret); |
Lee Jones | 15e27b1 | 2012-09-07 12:14:56 +0100 | [diff] [blame] | 387 | goto out_free; |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 390 | ret = tc3589x_device_init(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 391 | if (ret) { |
Sundar Iyer | 611b759 | 2010-12-13 09:33:15 +0530 | [diff] [blame] | 392 | dev_err(tc3589x->dev, "failed to add child devices\n"); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 393 | goto out_freeirq; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | |
| 398 | out_freeirq: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 399 | free_irq(tc3589x->i2c->irq, tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 400 | out_free: |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 401 | kfree(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 402 | return ret; |
| 403 | } |
| 404 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 405 | static int tc3589x_remove(struct i2c_client *client) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 406 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 407 | struct tc3589x *tc3589x = i2c_get_clientdata(client); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 408 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 409 | mfd_remove_devices(tc3589x->dev); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 410 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 411 | free_irq(tc3589x->i2c->irq, tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 412 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 413 | kfree(tc3589x); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Axel Lin | 930bf02 | 2012-07-02 17:19:52 +0800 | [diff] [blame] | 418 | #ifdef CONFIG_PM_SLEEP |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 419 | static int tc3589x_suspend(struct device *dev) |
| 420 | { |
| 421 | struct tc3589x *tc3589x = dev_get_drvdata(dev); |
| 422 | struct i2c_client *client = tc3589x->i2c; |
| 423 | int ret = 0; |
| 424 | |
| 425 | /* put the system to sleep mode */ |
| 426 | if (!device_may_wakeup(&client->dev)) |
| 427 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, |
| 428 | TC3589x_CLKMODE_MODCTL_SLEEP); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | static int tc3589x_resume(struct device *dev) |
| 434 | { |
| 435 | struct tc3589x *tc3589x = dev_get_drvdata(dev); |
| 436 | struct i2c_client *client = tc3589x->i2c; |
| 437 | int ret = 0; |
| 438 | |
| 439 | /* enable the system into operation */ |
| 440 | if (!device_may_wakeup(&client->dev)) |
| 441 | ret = tc3589x_reg_write(tc3589x, TC3589x_CLKMODE, |
| 442 | TC3589x_CLKMODE_MODCTL_OPERATION); |
| 443 | |
| 444 | return ret; |
| 445 | } |
Linus Walleij | 54d8e2c | 2011-08-09 20:37:17 +0200 | [diff] [blame] | 446 | #endif |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 447 | |
Axel Lin | 930bf02 | 2012-07-02 17:19:52 +0800 | [diff] [blame] | 448 | static SIMPLE_DEV_PM_OPS(tc3589x_dev_pm_ops, tc3589x_suspend, tc3589x_resume); |
| 449 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 450 | static const struct i2c_device_id tc3589x_id[] = { |
| 451 | { "tc3589x", 24 }, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 452 | { } |
| 453 | }; |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 454 | MODULE_DEVICE_TABLE(i2c, tc3589x_id); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 455 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 456 | static struct i2c_driver tc3589x_driver = { |
| 457 | .driver.name = "tc3589x", |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 458 | .driver.owner = THIS_MODULE, |
Sundar Iyer | 593e9d7 | 2010-12-13 09:33:18 +0530 | [diff] [blame] | 459 | .driver.pm = &tc3589x_dev_pm_ops, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 460 | .probe = tc3589x_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 461 | .remove = tc3589x_remove, |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 462 | .id_table = tc3589x_id, |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 463 | }; |
| 464 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 465 | static int __init tc3589x_init(void) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 466 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 467 | return i2c_add_driver(&tc3589x_driver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 468 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 469 | subsys_initcall(tc3589x_init); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 470 | |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 471 | static void __exit tc3589x_exit(void) |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 472 | { |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 473 | i2c_del_driver(&tc3589x_driver); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 474 | } |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 475 | module_exit(tc3589x_exit); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 476 | |
| 477 | MODULE_LICENSE("GPL v2"); |
Sundar Iyer | 20406eb | 2010-12-13 09:33:14 +0530 | [diff] [blame] | 478 | MODULE_DESCRIPTION("TC3589x MFD core driver"); |
Rabin Vincent | b4ecd32 | 2010-05-10 23:39:47 +0200 | [diff] [blame] | 479 | MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent"); |