Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 1 | /* |
Linus Walleij | 2071db4 | 2010-01-19 11:41:52 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2010 ST-Ericsson |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 3 | * License terms: GNU General Public License (GPL) version 2 |
| 4 | * Low-level core for exclusive access to the AB3100 IC on the I2C bus |
| 5 | * and some basic chip-configuration. |
| 6 | * Author: Linus Walleij <linus.walleij@stericsson.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/i2c.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/notifier.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 14 | #include <linux/err.h> |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/interrupt.h> |
Linus Walleij | 2071db4 | 2010-01-19 11:41:52 +0100 | [diff] [blame] | 19 | #include <linux/random.h> |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | #include <linux/uaccess.h> |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 23 | #include <linux/mfd/core.h> |
Marcus Cooper | 0fd0013 | 2012-08-10 10:32:35 +0200 | [diff] [blame] | 24 | #include <linux/mfd/ab3100.h> |
Linus Walleij | 812f9e9 | 2010-05-01 18:26:07 +0200 | [diff] [blame] | 25 | #include <linux/mfd/abx500.h> |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 26 | |
| 27 | /* These are the only registers inside AB3100 used in this main file */ |
| 28 | |
| 29 | /* Interrupt event registers */ |
| 30 | #define AB3100_EVENTA1 0x21 |
| 31 | #define AB3100_EVENTA2 0x22 |
| 32 | #define AB3100_EVENTA3 0x23 |
| 33 | |
| 34 | /* AB3100 DAC converter registers */ |
| 35 | #define AB3100_DIS 0x00 |
| 36 | #define AB3100_D0C 0x01 |
| 37 | #define AB3100_D1C 0x02 |
| 38 | #define AB3100_D2C 0x03 |
| 39 | #define AB3100_D3C 0x04 |
| 40 | |
| 41 | /* Chip ID register */ |
| 42 | #define AB3100_CID 0x20 |
| 43 | |
| 44 | /* AB3100 interrupt registers */ |
| 45 | #define AB3100_IMRA1 0x24 |
| 46 | #define AB3100_IMRA2 0x25 |
| 47 | #define AB3100_IMRA3 0x26 |
| 48 | #define AB3100_IMRB1 0x2B |
| 49 | #define AB3100_IMRB2 0x2C |
| 50 | #define AB3100_IMRB3 0x2D |
| 51 | |
| 52 | /* System Power Monitoring and control registers */ |
| 53 | #define AB3100_MCA 0x2E |
| 54 | #define AB3100_MCB 0x2F |
| 55 | |
| 56 | /* SIM power up */ |
| 57 | #define AB3100_SUP 0x50 |
| 58 | |
| 59 | /* |
| 60 | * I2C communication |
| 61 | * |
| 62 | * The AB3100 is usually assigned address 0x48 (7-bit) |
| 63 | * The chip is defined in the platform i2c_board_data section. |
| 64 | */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 65 | static int ab3100_get_chip_id(struct device *dev) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 66 | { |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 67 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 68 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 69 | return (int)ab3100->chip_id; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 70 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 71 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 72 | static int ab3100_set_register_interruptible(struct ab3100 *ab3100, |
| 73 | u8 reg, u8 regval) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 74 | { |
| 75 | u8 regandval[2] = {reg, regval}; |
| 76 | int err; |
| 77 | |
| 78 | err = mutex_lock_interruptible(&ab3100->access_mutex); |
| 79 | if (err) |
| 80 | return err; |
| 81 | |
| 82 | /* |
| 83 | * A two-byte write message with the first byte containing the register |
| 84 | * number and the second byte containing the value to be written |
| 85 | * effectively sets a register in the AB3100. |
| 86 | */ |
| 87 | err = i2c_master_send(ab3100->i2c_client, regandval, 2); |
| 88 | if (err < 0) { |
| 89 | dev_err(ab3100->dev, |
| 90 | "write error (write register): %d\n", |
| 91 | err); |
| 92 | } else if (err != 2) { |
| 93 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 94 | "write error (write register)\n" |
| 95 | " %d bytes transferred (expected 2)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 96 | err); |
| 97 | err = -EIO; |
| 98 | } else { |
| 99 | /* All is well */ |
| 100 | err = 0; |
| 101 | } |
| 102 | mutex_unlock(&ab3100->access_mutex); |
Linus Walleij | 7cdc2b9 | 2009-08-13 11:49:38 +0200 | [diff] [blame] | 103 | return err; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 104 | } |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 105 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 106 | static int set_register_interruptible(struct device *dev, |
| 107 | u8 bank, u8 reg, u8 value) |
| 108 | { |
| 109 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
| 110 | |
| 111 | return ab3100_set_register_interruptible(ab3100, reg, value); |
| 112 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * The test registers exist at an I2C bus address up one |
| 116 | * from the ordinary base. They are not supposed to be used |
| 117 | * in production code, but sometimes you have to do that |
| 118 | * anyway. It's currently only used from this file so declare |
| 119 | * it static and do not export. |
| 120 | */ |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 121 | static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 122 | u8 reg, u8 regval) |
| 123 | { |
| 124 | u8 regandval[2] = {reg, regval}; |
| 125 | int err; |
| 126 | |
| 127 | err = mutex_lock_interruptible(&ab3100->access_mutex); |
| 128 | if (err) |
| 129 | return err; |
| 130 | |
| 131 | err = i2c_master_send(ab3100->testreg_client, regandval, 2); |
| 132 | if (err < 0) { |
| 133 | dev_err(ab3100->dev, |
| 134 | "write error (write test register): %d\n", |
| 135 | err); |
| 136 | } else if (err != 2) { |
| 137 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 138 | "write error (write test register)\n" |
| 139 | " %d bytes transferred (expected 2)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 140 | err); |
| 141 | err = -EIO; |
| 142 | } else { |
| 143 | /* All is well */ |
| 144 | err = 0; |
| 145 | } |
| 146 | mutex_unlock(&ab3100->access_mutex); |
| 147 | |
| 148 | return err; |
| 149 | } |
| 150 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 151 | static int ab3100_get_register_interruptible(struct ab3100 *ab3100, |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 152 | u8 reg, u8 *regval) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 153 | { |
| 154 | int err; |
| 155 | |
| 156 | err = mutex_lock_interruptible(&ab3100->access_mutex); |
| 157 | if (err) |
| 158 | return err; |
| 159 | |
| 160 | /* |
| 161 | * AB3100 require an I2C "stop" command between each message, else |
| 162 | * it will not work. The only way of achieveing this with the |
| 163 | * message transport layer is to send the read and write messages |
| 164 | * separately. |
| 165 | */ |
| 166 | err = i2c_master_send(ab3100->i2c_client, ®, 1); |
| 167 | if (err < 0) { |
| 168 | dev_err(ab3100->dev, |
| 169 | "write error (send register address): %d\n", |
| 170 | err); |
| 171 | goto get_reg_out_unlock; |
| 172 | } else if (err != 1) { |
| 173 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 174 | "write error (send register address)\n" |
| 175 | " %d bytes transferred (expected 1)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 176 | err); |
| 177 | err = -EIO; |
| 178 | goto get_reg_out_unlock; |
| 179 | } else { |
| 180 | /* All is well */ |
| 181 | err = 0; |
| 182 | } |
| 183 | |
| 184 | err = i2c_master_recv(ab3100->i2c_client, regval, 1); |
| 185 | if (err < 0) { |
| 186 | dev_err(ab3100->dev, |
| 187 | "write error (read register): %d\n", |
| 188 | err); |
| 189 | goto get_reg_out_unlock; |
| 190 | } else if (err != 1) { |
| 191 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 192 | "write error (read register)\n" |
| 193 | " %d bytes transferred (expected 1)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 194 | err); |
| 195 | err = -EIO; |
| 196 | goto get_reg_out_unlock; |
| 197 | } else { |
| 198 | /* All is well */ |
| 199 | err = 0; |
| 200 | } |
| 201 | |
| 202 | get_reg_out_unlock: |
| 203 | mutex_unlock(&ab3100->access_mutex); |
| 204 | return err; |
| 205 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 206 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 207 | static int get_register_interruptible(struct device *dev, u8 bank, u8 reg, |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 208 | u8 *value) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 209 | { |
| 210 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 211 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 212 | return ab3100_get_register_interruptible(ab3100, reg, value); |
| 213 | } |
| 214 | |
| 215 | static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 216 | u8 first_reg, u8 *regvals, u8 numregs) |
| 217 | { |
| 218 | int err; |
| 219 | |
| 220 | if (ab3100->chip_id == 0xa0 || |
| 221 | ab3100->chip_id == 0xa1) |
| 222 | /* These don't support paged reads */ |
| 223 | return -EIO; |
| 224 | |
| 225 | err = mutex_lock_interruptible(&ab3100->access_mutex); |
| 226 | if (err) |
| 227 | return err; |
| 228 | |
| 229 | /* |
| 230 | * Paged read also require an I2C "stop" command. |
| 231 | */ |
| 232 | err = i2c_master_send(ab3100->i2c_client, &first_reg, 1); |
| 233 | if (err < 0) { |
| 234 | dev_err(ab3100->dev, |
| 235 | "write error (send first register address): %d\n", |
| 236 | err); |
| 237 | goto get_reg_page_out_unlock; |
| 238 | } else if (err != 1) { |
| 239 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 240 | "write error (send first register address)\n" |
| 241 | " %d bytes transferred (expected 1)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 242 | err); |
| 243 | err = -EIO; |
| 244 | goto get_reg_page_out_unlock; |
| 245 | } |
| 246 | |
| 247 | err = i2c_master_recv(ab3100->i2c_client, regvals, numregs); |
| 248 | if (err < 0) { |
| 249 | dev_err(ab3100->dev, |
| 250 | "write error (read register page): %d\n", |
| 251 | err); |
| 252 | goto get_reg_page_out_unlock; |
| 253 | } else if (err != numregs) { |
| 254 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 255 | "write error (read register page)\n" |
| 256 | " %d bytes transferred (expected %d)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 257 | err, numregs); |
| 258 | err = -EIO; |
| 259 | goto get_reg_page_out_unlock; |
| 260 | } |
| 261 | |
| 262 | /* All is well */ |
| 263 | err = 0; |
| 264 | |
| 265 | get_reg_page_out_unlock: |
| 266 | mutex_unlock(&ab3100->access_mutex); |
| 267 | return err; |
| 268 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 269 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 270 | static int get_register_page_interruptible(struct device *dev, u8 bank, |
| 271 | u8 first_reg, u8 *regvals, u8 numregs) |
| 272 | { |
| 273 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 274 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 275 | return ab3100_get_register_page_interruptible(ab3100, |
| 276 | first_reg, regvals, numregs); |
| 277 | } |
| 278 | |
| 279 | static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 280 | u8 reg, u8 andmask, u8 ormask) |
| 281 | { |
| 282 | u8 regandval[2] = {reg, 0}; |
| 283 | int err; |
| 284 | |
| 285 | err = mutex_lock_interruptible(&ab3100->access_mutex); |
| 286 | if (err) |
| 287 | return err; |
| 288 | |
| 289 | /* First read out the target register */ |
| 290 | err = i2c_master_send(ab3100->i2c_client, ®, 1); |
| 291 | if (err < 0) { |
| 292 | dev_err(ab3100->dev, |
| 293 | "write error (maskset send address): %d\n", |
| 294 | err); |
| 295 | goto get_maskset_unlock; |
| 296 | } else if (err != 1) { |
| 297 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 298 | "write error (maskset send address)\n" |
| 299 | " %d bytes transferred (expected 1)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 300 | err); |
| 301 | err = -EIO; |
| 302 | goto get_maskset_unlock; |
| 303 | } |
| 304 | |
| 305 | err = i2c_master_recv(ab3100->i2c_client, ®andval[1], 1); |
| 306 | if (err < 0) { |
| 307 | dev_err(ab3100->dev, |
| 308 | "write error (maskset read register): %d\n", |
| 309 | err); |
| 310 | goto get_maskset_unlock; |
| 311 | } else if (err != 1) { |
| 312 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 313 | "write error (maskset read register)\n" |
| 314 | " %d bytes transferred (expected 1)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 315 | err); |
| 316 | err = -EIO; |
| 317 | goto get_maskset_unlock; |
| 318 | } |
| 319 | |
| 320 | /* Modify the register */ |
| 321 | regandval[1] &= andmask; |
| 322 | regandval[1] |= ormask; |
| 323 | |
| 324 | /* Write the register */ |
| 325 | err = i2c_master_send(ab3100->i2c_client, regandval, 2); |
| 326 | if (err < 0) { |
| 327 | dev_err(ab3100->dev, |
| 328 | "write error (write register): %d\n", |
| 329 | err); |
| 330 | goto get_maskset_unlock; |
| 331 | } else if (err != 2) { |
| 332 | dev_err(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 333 | "write error (write register)\n" |
| 334 | " %d bytes transferred (expected 2)\n", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 335 | err); |
| 336 | err = -EIO; |
| 337 | goto get_maskset_unlock; |
| 338 | } |
| 339 | |
| 340 | /* All is well */ |
| 341 | err = 0; |
| 342 | |
| 343 | get_maskset_unlock: |
| 344 | mutex_unlock(&ab3100->access_mutex); |
| 345 | return err; |
| 346 | } |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 347 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 348 | static int mask_and_set_register_interruptible(struct device *dev, u8 bank, |
| 349 | u8 reg, u8 bitmask, u8 bitvalues) |
| 350 | { |
| 351 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
| 352 | |
| 353 | return ab3100_mask_and_set_register_interruptible(ab3100, |
| 354 | reg, bitmask, (bitmask & bitvalues)); |
| 355 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Register a simple callback for handling any AB3100 events. |
| 359 | */ |
| 360 | int ab3100_event_register(struct ab3100 *ab3100, |
| 361 | struct notifier_block *nb) |
| 362 | { |
| 363 | return blocking_notifier_chain_register(&ab3100->event_subscribers, |
| 364 | nb); |
| 365 | } |
| 366 | EXPORT_SYMBOL(ab3100_event_register); |
| 367 | |
| 368 | /* |
| 369 | * Remove a previously registered callback. |
| 370 | */ |
| 371 | int ab3100_event_unregister(struct ab3100 *ab3100, |
| 372 | struct notifier_block *nb) |
| 373 | { |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 374 | return blocking_notifier_chain_unregister(&ab3100->event_subscribers, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 375 | nb); |
| 376 | } |
| 377 | EXPORT_SYMBOL(ab3100_event_unregister); |
| 378 | |
| 379 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 380 | static int ab3100_event_registers_startup_state_get(struct device *dev, |
| 381 | u8 *event) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 382 | { |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 383 | struct ab3100 *ab3100 = dev_get_drvdata(dev->parent); |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 384 | |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 385 | if (!ab3100->startup_events_read) |
| 386 | return -EAGAIN; /* Try again later */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 387 | memcpy(event, ab3100->startup_events, 3); |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 388 | |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 389 | return 0; |
| 390 | } |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 391 | |
| 392 | static struct abx500_ops ab3100_ops = { |
| 393 | .get_chip_id = ab3100_get_chip_id, |
| 394 | .set_register = set_register_interruptible, |
| 395 | .get_register = get_register_interruptible, |
| 396 | .get_register_page = get_register_page_interruptible, |
| 397 | .set_register_page = NULL, |
| 398 | .mask_and_set_register = mask_and_set_register_interruptible, |
| 399 | .event_registers_startup_state_get = |
| 400 | ab3100_event_registers_startup_state_get, |
| 401 | .startup_irq_enabled = NULL, |
| 402 | }; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 403 | |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 404 | /* |
| 405 | * This is a threaded interrupt handler so we can make some |
| 406 | * I2C calls etc. |
| 407 | */ |
| 408 | static irqreturn_t ab3100_irq_handler(int irq, void *data) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 409 | { |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 410 | struct ab3100 *ab3100 = data; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 411 | u8 event_regs[3]; |
| 412 | u32 fatevent; |
| 413 | int err; |
| 414 | |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 415 | err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 416 | event_regs, 3); |
| 417 | if (err) |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 418 | goto err_event; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 419 | |
| 420 | fatevent = (event_regs[0] << 16) | |
| 421 | (event_regs[1] << 8) | |
| 422 | event_regs[2]; |
| 423 | |
| 424 | if (!ab3100->startup_events_read) { |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 425 | ab3100->startup_events[0] = event_regs[0]; |
| 426 | ab3100->startup_events[1] = event_regs[1]; |
| 427 | ab3100->startup_events[2] = event_regs[2]; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 428 | ab3100->startup_events_read = true; |
| 429 | } |
| 430 | /* |
| 431 | * The notified parties will have to mask out the events |
| 432 | * they're interested in and react to them. They will be |
| 433 | * notified on all events, then they use the fatevent value |
| 434 | * to determine if they're interested. |
| 435 | */ |
| 436 | blocking_notifier_call_chain(&ab3100->event_subscribers, |
| 437 | fatevent, NULL); |
| 438 | |
| 439 | dev_dbg(ab3100->dev, |
| 440 | "IRQ Event: 0x%08x\n", fatevent); |
| 441 | |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 442 | return IRQ_HANDLED; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 443 | |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 444 | err_event: |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 445 | dev_dbg(ab3100->dev, |
Linus Walleij | 0df883d | 2010-01-08 10:44:16 +0100 | [diff] [blame] | 446 | "error reading event status\n"); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 447 | return IRQ_HANDLED; |
| 448 | } |
| 449 | |
| 450 | #ifdef CONFIG_DEBUG_FS |
| 451 | /* |
| 452 | * Some debugfs entries only exposed if we're using debug |
| 453 | */ |
| 454 | static int ab3100_registers_print(struct seq_file *s, void *p) |
| 455 | { |
| 456 | struct ab3100 *ab3100 = s->private; |
| 457 | u8 value; |
| 458 | u8 reg; |
| 459 | |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 460 | seq_puts(s, "AB3100 registers:\n"); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 461 | |
| 462 | for (reg = 0; reg < 0xff; reg++) { |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 463 | ab3100_get_register_interruptible(ab3100, reg, &value); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 464 | seq_printf(s, "[0x%x]: 0x%x\n", reg, value); |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int ab3100_registers_open(struct inode *inode, struct file *file) |
| 470 | { |
| 471 | return single_open(file, ab3100_registers_print, inode->i_private); |
| 472 | } |
| 473 | |
| 474 | static const struct file_operations ab3100_registers_fops = { |
| 475 | .open = ab3100_registers_open, |
| 476 | .read = seq_read, |
| 477 | .llseek = seq_lseek, |
| 478 | .release = single_release, |
| 479 | .owner = THIS_MODULE, |
| 480 | }; |
| 481 | |
| 482 | struct ab3100_get_set_reg_priv { |
| 483 | struct ab3100 *ab3100; |
| 484 | bool mode; |
| 485 | }; |
| 486 | |
Samuel Ortiz | dfc3aa7 | 2009-06-23 10:48:36 +0200 | [diff] [blame] | 487 | static ssize_t ab3100_get_set_reg(struct file *file, |
| 488 | const char __user *user_buf, |
| 489 | size_t count, loff_t *ppos) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 490 | { |
| 491 | struct ab3100_get_set_reg_priv *priv = file->private_data; |
| 492 | struct ab3100 *ab3100 = priv->ab3100; |
| 493 | char buf[32]; |
Samuel Ortiz | dfc3aa7 | 2009-06-23 10:48:36 +0200 | [diff] [blame] | 494 | ssize_t buf_size; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 495 | int regp; |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 496 | u8 user_reg; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 497 | int err; |
| 498 | int i = 0; |
| 499 | |
| 500 | /* Get userspace string and assure termination */ |
| 501 | buf_size = min(count, (sizeof(buf)-1)); |
| 502 | if (copy_from_user(buf, user_buf, buf_size)) |
| 503 | return -EFAULT; |
| 504 | buf[buf_size] = 0; |
| 505 | |
| 506 | /* |
| 507 | * The idea is here to parse a string which is either |
| 508 | * "0xnn" for reading a register, or "0xaa 0xbb" for |
| 509 | * writing 0xbb to the register 0xaa. First move past |
| 510 | * whitespace and then begin to parse the register. |
| 511 | */ |
| 512 | while ((i < buf_size) && (buf[i] == ' ')) |
| 513 | i++; |
| 514 | regp = i; |
| 515 | |
| 516 | /* |
| 517 | * Advance pointer to end of string then terminate |
| 518 | * the register string. This is needed to satisfy |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 519 | * the kstrtou8() function. |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 520 | */ |
| 521 | while ((i < buf_size) && (buf[i] != ' ')) |
| 522 | i++; |
| 523 | buf[i] = '\0'; |
| 524 | |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 525 | err = kstrtou8(&buf[regp], 16, &user_reg); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 526 | if (err) |
| 527 | return err; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 528 | |
| 529 | /* Either we read or we write a register here */ |
| 530 | if (!priv->mode) { |
| 531 | /* Reading */ |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 532 | u8 regvalue; |
| 533 | |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 534 | ab3100_get_register_interruptible(ab3100, user_reg, ®value); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 535 | |
| 536 | dev_info(ab3100->dev, |
| 537 | "debug read AB3100 reg[0x%02x]: 0x%02x\n", |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 538 | user_reg, regvalue); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 539 | } else { |
| 540 | int valp; |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 541 | u8 user_value; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 542 | u8 regvalue; |
| 543 | |
| 544 | /* |
| 545 | * Writing, we need some value to write to |
| 546 | * the register so keep parsing the string |
| 547 | * from userspace. |
| 548 | */ |
| 549 | i++; |
| 550 | while ((i < buf_size) && (buf[i] == ' ')) |
| 551 | i++; |
| 552 | valp = i; |
| 553 | while ((i < buf_size) && (buf[i] != ' ')) |
| 554 | i++; |
| 555 | buf[i] = '\0'; |
| 556 | |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 557 | err = kstrtou8(&buf[valp], 16, &user_value); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 558 | if (err) |
| 559 | return err; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 560 | |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 561 | ab3100_set_register_interruptible(ab3100, user_reg, user_value); |
| 562 | ab3100_get_register_interruptible(ab3100, user_reg, ®value); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 563 | |
| 564 | dev_info(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 565 | "debug write reg[0x%02x]\n" |
| 566 | " with 0x%02x, after readback: 0x%02x\n", |
Jingoo Han | 8420a24 | 2013-06-04 13:11:50 +0900 | [diff] [blame] | 567 | user_reg, user_value, regvalue); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 568 | } |
| 569 | return buf_size; |
| 570 | } |
| 571 | |
| 572 | static const struct file_operations ab3100_get_set_reg_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 573 | .open = simple_open, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 574 | .write = ab3100_get_set_reg, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 575 | .llseek = noop_llseek, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | static struct dentry *ab3100_dir; |
| 579 | static struct dentry *ab3100_reg_file; |
| 580 | static struct ab3100_get_set_reg_priv ab3100_get_priv; |
| 581 | static struct dentry *ab3100_get_reg_file; |
| 582 | static struct ab3100_get_set_reg_priv ab3100_set_priv; |
| 583 | static struct dentry *ab3100_set_reg_file; |
| 584 | |
| 585 | static void ab3100_setup_debugfs(struct ab3100 *ab3100) |
| 586 | { |
| 587 | int err; |
| 588 | |
| 589 | ab3100_dir = debugfs_create_dir("ab3100", NULL); |
| 590 | if (!ab3100_dir) |
| 591 | goto exit_no_debugfs; |
| 592 | |
| 593 | ab3100_reg_file = debugfs_create_file("registers", |
| 594 | S_IRUGO, ab3100_dir, ab3100, |
| 595 | &ab3100_registers_fops); |
| 596 | if (!ab3100_reg_file) { |
| 597 | err = -ENOMEM; |
| 598 | goto exit_destroy_dir; |
| 599 | } |
| 600 | |
| 601 | ab3100_get_priv.ab3100 = ab3100; |
| 602 | ab3100_get_priv.mode = false; |
| 603 | ab3100_get_reg_file = debugfs_create_file("get_reg", |
Vasiliy Kulikov | f8a0697 | 2011-02-04 15:23:36 +0300 | [diff] [blame] | 604 | S_IWUSR, ab3100_dir, &ab3100_get_priv, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 605 | &ab3100_get_set_reg_fops); |
| 606 | if (!ab3100_get_reg_file) { |
| 607 | err = -ENOMEM; |
| 608 | goto exit_destroy_reg; |
| 609 | } |
| 610 | |
| 611 | ab3100_set_priv.ab3100 = ab3100; |
| 612 | ab3100_set_priv.mode = true; |
| 613 | ab3100_set_reg_file = debugfs_create_file("set_reg", |
Vasiliy Kulikov | f8a0697 | 2011-02-04 15:23:36 +0300 | [diff] [blame] | 614 | S_IWUSR, ab3100_dir, &ab3100_set_priv, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 615 | &ab3100_get_set_reg_fops); |
| 616 | if (!ab3100_set_reg_file) { |
| 617 | err = -ENOMEM; |
| 618 | goto exit_destroy_get_reg; |
| 619 | } |
| 620 | return; |
| 621 | |
| 622 | exit_destroy_get_reg: |
| 623 | debugfs_remove(ab3100_get_reg_file); |
| 624 | exit_destroy_reg: |
| 625 | debugfs_remove(ab3100_reg_file); |
| 626 | exit_destroy_dir: |
| 627 | debugfs_remove(ab3100_dir); |
| 628 | exit_no_debugfs: |
| 629 | return; |
| 630 | } |
| 631 | static inline void ab3100_remove_debugfs(void) |
| 632 | { |
| 633 | debugfs_remove(ab3100_set_reg_file); |
| 634 | debugfs_remove(ab3100_get_reg_file); |
| 635 | debugfs_remove(ab3100_reg_file); |
| 636 | debugfs_remove(ab3100_dir); |
| 637 | } |
| 638 | #else |
| 639 | static inline void ab3100_setup_debugfs(struct ab3100 *ab3100) |
| 640 | { |
| 641 | } |
| 642 | static inline void ab3100_remove_debugfs(void) |
| 643 | { |
| 644 | } |
| 645 | #endif |
| 646 | |
| 647 | /* |
| 648 | * Basic set-up, datastructure creation/destruction and I2C interface. |
| 649 | * This sets up a default config in the AB3100 chip so that it |
| 650 | * will work as expected. |
| 651 | */ |
| 652 | |
| 653 | struct ab3100_init_setting { |
| 654 | u8 abreg; |
| 655 | u8 setting; |
| 656 | }; |
| 657 | |
Bill Pemberton | a73e5df | 2012-11-19 13:25:09 -0500 | [diff] [blame] | 658 | static const struct ab3100_init_setting ab3100_init_settings[] = { |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 659 | { |
| 660 | .abreg = AB3100_MCA, |
| 661 | .setting = 0x01 |
| 662 | }, { |
| 663 | .abreg = AB3100_MCB, |
| 664 | .setting = 0x30 |
| 665 | }, { |
| 666 | .abreg = AB3100_IMRA1, |
| 667 | .setting = 0x00 |
| 668 | }, { |
| 669 | .abreg = AB3100_IMRA2, |
| 670 | .setting = 0xFF |
| 671 | }, { |
| 672 | .abreg = AB3100_IMRA3, |
| 673 | .setting = 0x01 |
| 674 | }, { |
| 675 | .abreg = AB3100_IMRB1, |
Linus Walleij | ce290b0 | 2009-08-13 11:49:49 +0200 | [diff] [blame] | 676 | .setting = 0xBF |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 677 | }, { |
| 678 | .abreg = AB3100_IMRB2, |
| 679 | .setting = 0xFF |
| 680 | }, { |
| 681 | .abreg = AB3100_IMRB3, |
| 682 | .setting = 0xFF |
| 683 | }, { |
| 684 | .abreg = AB3100_SUP, |
| 685 | .setting = 0x00 |
| 686 | }, { |
| 687 | .abreg = AB3100_DIS, |
| 688 | .setting = 0xF0 |
| 689 | }, { |
| 690 | .abreg = AB3100_D0C, |
| 691 | .setting = 0x00 |
| 692 | }, { |
| 693 | .abreg = AB3100_D1C, |
| 694 | .setting = 0x00 |
| 695 | }, { |
| 696 | .abreg = AB3100_D2C, |
| 697 | .setting = 0x00 |
| 698 | }, { |
| 699 | .abreg = AB3100_D3C, |
| 700 | .setting = 0x00 |
| 701 | }, |
| 702 | }; |
| 703 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 704 | static int ab3100_setup(struct ab3100 *ab3100) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 705 | { |
| 706 | int err = 0; |
| 707 | int i; |
| 708 | |
| 709 | for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) { |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 710 | err = ab3100_set_register_interruptible(ab3100, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 711 | ab3100_init_settings[i].abreg, |
| 712 | ab3100_init_settings[i].setting); |
| 713 | if (err) |
| 714 | goto exit_no_setup; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Special trick to make the AB3100 use the 32kHz clock (RTC) |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 719 | * bit 3 in test register 0x02 is a special, undocumented test |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 720 | * register bit that only exist in AB3100 P1E |
| 721 | */ |
| 722 | if (ab3100->chip_id == 0xc4) { |
| 723 | dev_warn(ab3100->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 724 | "AB3100 P1E variant detected forcing chip to 32KHz\n"); |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 725 | err = ab3100_set_test_register_interruptible(ab3100, |
| 726 | 0x02, 0x08); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | exit_no_setup: |
| 730 | return err; |
| 731 | } |
| 732 | |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 733 | /* The subdevices of the AB3100 */ |
| 734 | static struct mfd_cell ab3100_devs[] = { |
| 735 | { |
| 736 | .name = "ab3100-dac", |
| 737 | .id = -1, |
| 738 | }, |
| 739 | { |
| 740 | .name = "ab3100-leds", |
| 741 | .id = -1, |
| 742 | }, |
| 743 | { |
| 744 | .name = "ab3100-power", |
| 745 | .id = -1, |
| 746 | }, |
| 747 | { |
| 748 | .name = "ab3100-regulators", |
Linus Walleij | 8735bc2 | 2013-04-22 11:57:25 +0200 | [diff] [blame] | 749 | .of_compatible = "stericsson,ab3100-regulators", |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 750 | .id = -1, |
| 751 | }, |
| 752 | { |
| 753 | .name = "ab3100-sim", |
| 754 | .id = -1, |
| 755 | }, |
| 756 | { |
| 757 | .name = "ab3100-uart", |
| 758 | .id = -1, |
| 759 | }, |
| 760 | { |
| 761 | .name = "ab3100-rtc", |
| 762 | .id = -1, |
| 763 | }, |
| 764 | { |
| 765 | .name = "ab3100-charger", |
| 766 | .id = -1, |
| 767 | }, |
| 768 | { |
| 769 | .name = "ab3100-boost", |
| 770 | .id = -1, |
| 771 | }, |
| 772 | { |
| 773 | .name = "ab3100-adc", |
| 774 | .id = -1, |
| 775 | }, |
| 776 | { |
| 777 | .name = "ab3100-fuelgauge", |
| 778 | .id = -1, |
| 779 | }, |
| 780 | { |
| 781 | .name = "ab3100-vibrator", |
| 782 | .id = -1, |
| 783 | }, |
| 784 | { |
| 785 | .name = "ab3100-otp", |
| 786 | .id = -1, |
| 787 | }, |
| 788 | { |
| 789 | .name = "ab3100-codec", |
| 790 | .id = -1, |
| 791 | }, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | struct ab_family_id { |
| 795 | u8 id; |
| 796 | char *name; |
| 797 | }; |
| 798 | |
Bill Pemberton | a73e5df | 2012-11-19 13:25:09 -0500 | [diff] [blame] | 799 | static const struct ab_family_id ids[] = { |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 800 | /* AB3100 */ |
| 801 | { |
| 802 | .id = 0xc0, |
| 803 | .name = "P1A" |
| 804 | }, { |
| 805 | .id = 0xc1, |
| 806 | .name = "P1B" |
| 807 | }, { |
| 808 | .id = 0xc2, |
| 809 | .name = "P1C" |
| 810 | }, { |
| 811 | .id = 0xc3, |
| 812 | .name = "P1D" |
| 813 | }, { |
| 814 | .id = 0xc4, |
| 815 | .name = "P1E" |
| 816 | }, { |
| 817 | .id = 0xc5, |
| 818 | .name = "P1F/R1A" |
| 819 | }, { |
| 820 | .id = 0xc6, |
| 821 | .name = "P1G/R1A" |
| 822 | }, { |
| 823 | .id = 0xc7, |
| 824 | .name = "P2A/R2A" |
| 825 | }, { |
| 826 | .id = 0xc8, |
| 827 | .name = "P2B/R2B" |
| 828 | }, |
| 829 | /* AB3000 variants, not supported */ |
| 830 | { |
| 831 | .id = 0xa0 |
| 832 | }, { |
| 833 | .id = 0xa1 |
| 834 | }, { |
| 835 | .id = 0xa2 |
| 836 | }, { |
| 837 | .id = 0xa3 |
| 838 | }, { |
| 839 | .id = 0xa4 |
| 840 | }, { |
| 841 | .id = 0xa5 |
| 842 | }, { |
| 843 | .id = 0xa6 |
| 844 | }, { |
| 845 | .id = 0xa7 |
| 846 | }, |
| 847 | /* Terminator */ |
| 848 | { |
| 849 | .id = 0x00, |
| 850 | }, |
| 851 | }; |
| 852 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 853 | static int ab3100_probe(struct i2c_client *client, |
Linus Walleij | 2524468 | 2010-08-20 10:26:56 +0200 | [diff] [blame] | 854 | const struct i2c_device_id *id) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 855 | { |
| 856 | struct ab3100 *ab3100; |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 857 | struct ab3100_platform_data *ab3100_plf_data = |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 858 | dev_get_platdata(&client->dev); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 859 | int err; |
| 860 | int i; |
| 861 | |
Linus Walleij | 7f0f07c | 2012-06-12 20:26:58 +0200 | [diff] [blame] | 862 | ab3100 = devm_kzalloc(&client->dev, sizeof(struct ab3100), GFP_KERNEL); |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 863 | if (!ab3100) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 864 | return -ENOMEM; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 865 | |
| 866 | /* Initialize data structure */ |
| 867 | mutex_init(&ab3100->access_mutex); |
| 868 | BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers); |
| 869 | |
| 870 | ab3100->i2c_client = client; |
| 871 | ab3100->dev = &ab3100->i2c_client->dev; |
| 872 | |
| 873 | i2c_set_clientdata(client, ab3100); |
| 874 | |
| 875 | /* Read chip ID register */ |
Linus Walleij | 956f25a | 2009-08-13 11:49:23 +0200 | [diff] [blame] | 876 | err = ab3100_get_register_interruptible(ab3100, AB3100_CID, |
| 877 | &ab3100->chip_id); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 878 | if (err) { |
| 879 | dev_err(&client->dev, |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 880 | "failed to communicate with AB3100 chip\n"); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 881 | goto exit_no_detect; |
| 882 | } |
| 883 | |
| 884 | for (i = 0; ids[i].id != 0x0; i++) { |
| 885 | if (ids[i].id == ab3100->chip_id) { |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 886 | if (ids[i].name) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 887 | break; |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 888 | |
| 889 | dev_err(&client->dev, "AB3000 is not supported\n"); |
| 890 | goto exit_no_detect; |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Lee Jones | 15544ca | 2015-10-27 16:19:31 +0000 | [diff] [blame] | 894 | snprintf(&ab3100->chip_name[0], |
| 895 | sizeof(ab3100->chip_name) - 1, "AB3100 %s", ids[i].name); |
| 896 | |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 897 | if (ids[i].id == 0x0) { |
| 898 | dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n", |
| 899 | ab3100->chip_id); |
Lee Jones | cd63a89 | 2014-05-08 13:47:15 +0100 | [diff] [blame] | 900 | dev_err(&client->dev, |
| 901 | "accepting it anyway. Please update the driver.\n"); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 902 | goto exit_no_detect; |
| 903 | } |
| 904 | |
| 905 | dev_info(&client->dev, "Detected chip: %s\n", |
| 906 | &ab3100->chip_name[0]); |
| 907 | |
| 908 | /* Attach a second dummy i2c_client to the test register address */ |
| 909 | ab3100->testreg_client = i2c_new_dummy(client->adapter, |
Linus Walleij | 7f0f07c | 2012-06-12 20:26:58 +0200 | [diff] [blame] | 910 | client->addr + 1); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 911 | if (!ab3100->testreg_client) { |
| 912 | err = -ENOMEM; |
| 913 | goto exit_no_testreg_client; |
| 914 | } |
| 915 | |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 916 | err = ab3100_setup(ab3100); |
| 917 | if (err) |
| 918 | goto exit_no_setup; |
| 919 | |
Linus Walleij | 7f0f07c | 2012-06-12 20:26:58 +0200 | [diff] [blame] | 920 | err = devm_request_threaded_irq(&client->dev, |
| 921 | client->irq, NULL, ab3100_irq_handler, |
| 922 | IRQF_ONESHOT, "ab3100-core", ab3100); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 923 | if (err) |
| 924 | goto exit_no_irq; |
| 925 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 926 | err = abx500_register_ops(&client->dev, &ab3100_ops); |
| 927 | if (err) |
| 928 | goto exit_no_ops; |
| 929 | |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 930 | /* Set up and register the platform devices. */ |
Samuel Ortiz | a771e36 | 2011-04-06 00:41:43 +0200 | [diff] [blame] | 931 | for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) { |
| 932 | ab3100_devs[i].platform_data = ab3100_plf_data; |
| 933 | ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data); |
| 934 | } |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 935 | |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 936 | err = mfd_add_devices(&client->dev, 0, ab3100_devs, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 937 | ARRAY_SIZE(ab3100_devs), NULL, 0, NULL); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 938 | |
| 939 | ab3100_setup_debugfs(ab3100); |
| 940 | |
| 941 | return 0; |
| 942 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 943 | exit_no_ops: |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 944 | exit_no_irq: |
| 945 | exit_no_setup: |
| 946 | i2c_unregister_device(ab3100->testreg_client); |
| 947 | exit_no_testreg_client: |
| 948 | exit_no_detect: |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 949 | return err; |
| 950 | } |
| 951 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 952 | static int ab3100_remove(struct i2c_client *client) |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 953 | { |
| 954 | struct ab3100 *ab3100 = i2c_get_clientdata(client); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 955 | |
| 956 | /* Unregister subdevices */ |
Linus Walleij | 8c96aef | 2010-08-20 10:27:12 +0200 | [diff] [blame] | 957 | mfd_remove_devices(&client->dev); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 958 | ab3100_remove_debugfs(); |
| 959 | i2c_unregister_device(ab3100->testreg_client); |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static const struct i2c_device_id ab3100_id[] = { |
Jean Delvare | dcffa12 | 2009-10-04 22:53:44 +0200 | [diff] [blame] | 964 | { "ab3100", 0 }, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 965 | { } |
| 966 | }; |
| 967 | MODULE_DEVICE_TABLE(i2c, ab3100_id); |
| 968 | |
| 969 | static struct i2c_driver ab3100_driver = { |
| 970 | .driver = { |
| 971 | .name = "ab3100", |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 972 | }, |
| 973 | .id_table = ab3100_id, |
| 974 | .probe = ab3100_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 975 | .remove = ab3100_remove, |
Linus Walleij | 14fa569 | 2009-05-21 23:17:06 +0200 | [diff] [blame] | 976 | }; |
| 977 | |
| 978 | static int __init ab3100_i2c_init(void) |
| 979 | { |
| 980 | return i2c_add_driver(&ab3100_driver); |
| 981 | } |
| 982 | |
| 983 | static void __exit ab3100_i2c_exit(void) |
| 984 | { |
| 985 | i2c_del_driver(&ab3100_driver); |
| 986 | } |
| 987 | |
| 988 | subsys_initcall(ab3100_i2c_init); |
| 989 | module_exit(ab3100_i2c_exit); |
| 990 | |
| 991 | MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); |
| 992 | MODULE_DESCRIPTION("AB3100 core driver"); |
| 993 | MODULE_LICENSE("GPL"); |