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