David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/bitrev.h> |
| 16 | #include <linux/crc-ccitt.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/bif/consumer.h> |
| 28 | #include <linux/bif/driver.h> |
| 29 | |
| 30 | /** |
| 31 | * struct bif_ctrl_dev - holds controller device specific information |
| 32 | * @list: Doubly-linked list parameter linking to other |
| 33 | * BIF controllers registered in the system |
| 34 | * @desc: Description structure for this BIF controller |
| 35 | * @mutex: Mutex lock that is used to ensure mutual |
| 36 | * exclusion between transactions performed on the |
| 37 | * BIF bus for this controller |
| 38 | * @ctrl_dev: Device pointer to the BIF controller device |
| 39 | * @driver_data: Private data used by the BIF controller |
| 40 | * @selected_sdev: Slave device that is currently selected on |
| 41 | * the BIF bus of this controller |
| 42 | * @bus_change_notifier: Head of a notifier list containing notifier |
| 43 | * blocks that are notified when the battery |
| 44 | * presence changes |
| 45 | * @enter_irq_mode_work: Work task that is scheduled after a transaction |
| 46 | * completes when there are consumers that are |
| 47 | * actively monitoring BIF slave interrupts |
| 48 | * @irq_count: This is a count of the total number of BIF slave |
| 49 | * interrupts that are currently being monitored |
| 50 | * for the BIF slaves connected to this BIF |
| 51 | * controller |
| 52 | * @irq_mode_delay_jiffies: Number of jiffies to wait before scheduling the |
| 53 | * enter IRQ mode task. Using a larger value |
| 54 | * helps to improve the performance of BIF |
| 55 | * consumers that perform many BIF transactions. |
| 56 | * Using a smaller value reduces the latency of |
| 57 | * BIF slave interrupts. |
| 58 | * @battery_present: Cached value of the battery presence. This is |
| 59 | * used to filter out spurious presence update |
| 60 | * calls when the battery presence state has not |
| 61 | * changed. |
| 62 | */ |
| 63 | struct bif_ctrl_dev { |
| 64 | struct list_head list; |
| 65 | struct bif_ctrl_desc *desc; |
| 66 | struct mutex mutex; |
| 67 | struct device *ctrl_dev; |
| 68 | void *driver_data; |
| 69 | struct bif_slave_dev *selected_sdev; |
| 70 | struct blocking_notifier_head bus_change_notifier; |
| 71 | struct delayed_work enter_irq_mode_work; |
| 72 | int irq_count; |
| 73 | int irq_mode_delay_jiffies; |
| 74 | bool battery_present; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * struct bif_ctrl - handle used by BIF consumers for bus oriented BIF |
| 79 | * operations |
| 80 | * @bdev: Pointer to BIF controller device |
| 81 | * @exclusive_lock: Flag which indicates that the BIF consumer responsible |
| 82 | * for this handle has locked the BIF bus of this |
| 83 | * controller. BIF transactions from other consumers are |
| 84 | * blocked until the bus is unlocked. |
| 85 | */ |
| 86 | struct bif_ctrl { |
| 87 | struct bif_ctrl_dev *bdev; |
| 88 | bool exclusive_lock; |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * struct bif_slave_dev - holds BIF slave device information |
| 93 | * @list: Doubly-linked list parameter linking to other |
| 94 | * BIF slaves that have been enumerated |
| 95 | * @bdev: Pointer to the BIF controller device that this |
| 96 | * slave is physically connected to |
| 97 | * @slave_addr: 8-bit BIF DEV_ADR assigned to this slave |
| 98 | * @unique_id: 80-bit BIF unique ID of the slave |
| 99 | * @unique_id_bits_known: Number of bits of the UID that are currently |
| 100 | * known. This number starts is incremented during |
| 101 | * a UID search and must end at 80 if the slave |
| 102 | * responds to the search properly. |
| 103 | * @present: Boolean value showing if this slave is |
| 104 | * physically present in the system at a given |
| 105 | * point in time. The value is set to false if the |
| 106 | * battery pack containing the slave is |
| 107 | * disconnected. |
| 108 | * @l1_data: BIF DDB L1 data of the slave as read from the |
| 109 | * slave's memory |
| 110 | * @function_directory: Pointer to the BIF DDB L2 function directory |
| 111 | * list as read from the slave's memory |
| 112 | * @protocol_function: Pointer to constant protocol function data as |
| 113 | * well as software state information if the slave |
| 114 | * has a protocol function |
| 115 | * @slave_ctrl_function: Pointer to constant slave control function data |
| 116 | * as well as software state information if the |
| 117 | * slave has a slave control function |
| 118 | * @nvm_function: Pointer to constant non-volatile memory function |
| 119 | * data as well as software state information if |
| 120 | * the slave has a non-volatile memory function |
| 121 | * |
| 122 | * bif_slave_dev objects are stored indefinitely after enumeration in order to |
| 123 | * speed up battery reinsertion. Only a UID check is needed after inserting a |
| 124 | * battery assuming it has been enumerated before. |
| 125 | * |
| 126 | * unique_id bytes are stored such that unique_id[0] = MSB and |
| 127 | * unique_id[BIF_UNIQUE_ID_BYTE_LENGTH - 1] = LSB |
| 128 | */ |
| 129 | struct bif_slave_dev { |
| 130 | struct list_head list; |
| 131 | struct bif_ctrl_dev *bdev; |
| 132 | u8 slave_addr; |
| 133 | u8 unique_id[BIF_UNIQUE_ID_BYTE_LENGTH]; |
| 134 | int unique_id_bits_known; |
| 135 | bool present; |
| 136 | struct bif_ddb_l1_data l1_data; |
| 137 | struct bif_ddb_l2_data *function_directory; |
| 138 | struct bif_protocol_function *protocol_function; |
| 139 | struct bif_slave_control_function *slave_ctrl_function; |
| 140 | struct bif_nvm_function *nvm_function; |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * struct bif_slave - handle used by BIF consumers for slave oriented BIF |
| 145 | * operations |
| 146 | * @ctrl: Consumer BIF controller handle data |
| 147 | * @sdev: Pointer to BIF slave device |
| 148 | */ |
| 149 | struct bif_slave { |
| 150 | struct bif_ctrl ctrl; |
| 151 | struct bif_slave_dev *sdev; |
| 152 | }; |
| 153 | |
| 154 | /* Number of times to retry a full BIF transaction before returning an error. */ |
| 155 | #define BIF_TRANSACTION_RETRY_COUNT 5 |
| 156 | |
| 157 | static DEFINE_MUTEX(bif_ctrl_list_mutex); |
| 158 | static LIST_HEAD(bif_ctrl_list); |
| 159 | static DEFINE_MUTEX(bif_sdev_list_mutex); |
| 160 | static LIST_HEAD(bif_sdev_list); |
| 161 | |
| 162 | static u8 next_dev_addr = 0x02; |
| 163 | |
| 164 | #define DEBUG_PRINT_BUFFER_SIZE 256 |
| 165 | static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len) |
| 166 | { |
| 167 | int pos = 0; |
| 168 | int i; |
| 169 | |
| 170 | for (i = 0; i < buf_len; i++) { |
| 171 | pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]); |
| 172 | if (i < buf_len - 1) |
| 173 | pos += scnprintf(str + pos, str_len - pos, ", "); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void bif_print_slave_data(struct bif_slave_dev *sdev) |
| 178 | { |
| 179 | char str[DEBUG_PRINT_BUFFER_SIZE]; |
| 180 | u8 *uid; |
| 181 | int i, j; |
| 182 | struct bif_object *object; |
| 183 | |
| 184 | if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH) |
| 185 | return; |
| 186 | |
| 187 | uid = sdev->unique_id; |
| 188 | pr_debug("BIF slave: 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n", |
| 189 | uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], |
| 190 | uid[7], uid[8], uid[9]); |
| 191 | pr_debug(" present=%d, dev_adr=0x%02X\n", sdev->present, |
| 192 | sdev->slave_addr); |
| 193 | pr_debug(" revision=0x%02X, level=0x%02X, device class=0x%04X\n", |
| 194 | sdev->l1_data.revision, sdev->l1_data.level, |
| 195 | sdev->l1_data.device_class); |
| 196 | pr_debug(" manufacturer ID=0x%04X, product ID=0x%04X\n", |
| 197 | sdev->l1_data.manufacturer_id, sdev->l1_data.product_id); |
| 198 | pr_debug(" function directory length=%d\n", sdev->l1_data.length); |
| 199 | |
| 200 | for (i = 0; i < sdev->l1_data.length / 4; i++) { |
| 201 | pr_debug(" Function %d: type=0x%02X, version=0x%02X, pointer=0x%04X\n", |
| 202 | i, sdev->function_directory[i].function_type, |
| 203 | sdev->function_directory[i].function_version, |
| 204 | sdev->function_directory[i].function_pointer); |
| 205 | } |
| 206 | |
| 207 | if (sdev->nvm_function) { |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 208 | pr_debug(" NVM function: pointer=0x%04X, task=%d, wr_buf_size=%d, nvm_base=0x%04X, nvm_size=%d, nvm_lock_offset=%d\n", |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 209 | sdev->nvm_function->nvm_pointer, |
| 210 | sdev->nvm_function->slave_control_channel, |
| 211 | (sdev->nvm_function->write_buffer_size |
| 212 | ? sdev->nvm_function->write_buffer_size : 0), |
| 213 | sdev->nvm_function->nvm_base_address, |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 214 | sdev->nvm_function->nvm_size, |
| 215 | sdev->nvm_function->nvm_lock_offset); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 216 | if (sdev->nvm_function->object_count) |
| 217 | pr_debug(" NVM objects:\n"); |
| 218 | i = 0; |
| 219 | list_for_each_entry(object, &sdev->nvm_function->object_list, |
| 220 | list) { |
| 221 | pr_debug(" Object %d - addr=0x%04X, data len=%d, type=0x%02X, version=0x%02X, manufacturer ID=0x%04X, crc=0x%04X\n", |
| 222 | i, object->addr, object->length - 8, |
| 223 | object->type, object->version, |
| 224 | object->manufacturer_id, object->crc); |
| 225 | for (j = 0; j < DIV_ROUND_UP(object->length - 8, 16); |
| 226 | j++) { |
| 227 | fill_string(str, DEBUG_PRINT_BUFFER_SIZE, |
| 228 | object->data + j * 16, |
| 229 | min(16, object->length - 8 - (j * 16))); |
| 230 | pr_debug(" data(0x%04X): %s\n", j * 16, |
| 231 | str); |
| 232 | } |
| 233 | i++; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void bif_print_slaves(void) |
| 239 | { |
| 240 | struct bif_slave_dev *sdev; |
| 241 | |
| 242 | mutex_lock(&bif_sdev_list_mutex); |
| 243 | |
| 244 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 245 | /* Skip slaves without fully known UIDs. */ |
| 246 | if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH) |
| 247 | continue; |
| 248 | bif_print_slave_data(sdev); |
| 249 | } |
| 250 | |
| 251 | mutex_unlock(&bif_sdev_list_mutex); |
| 252 | } |
| 253 | |
| 254 | static struct bif_slave_dev *bif_add_slave(struct bif_ctrl_dev *bdev) |
| 255 | { |
| 256 | struct bif_slave_dev *sdev; |
| 257 | |
| 258 | sdev = kzalloc(sizeof(struct bif_slave_dev), GFP_KERNEL); |
| 259 | if (sdev == NULL) { |
| 260 | pr_err("Memory allocation failed for bif_slave_dev\n"); |
| 261 | return ERR_PTR(-ENOMEM); |
| 262 | } |
| 263 | |
| 264 | sdev->bdev = bdev; |
| 265 | INIT_LIST_HEAD(&sdev->list); |
| 266 | list_add_tail(&sdev->list, &bif_sdev_list); |
| 267 | |
| 268 | return sdev; |
| 269 | } |
| 270 | |
| 271 | static void bif_remove_slave(struct bif_slave_dev *sdev) |
| 272 | { |
| 273 | list_del(&sdev->list); |
| 274 | if (sdev->bdev->selected_sdev == sdev) |
| 275 | sdev->bdev->selected_sdev = NULL; |
| 276 | |
| 277 | if (sdev->slave_ctrl_function) |
| 278 | kfree(sdev->slave_ctrl_function->irq_notifier_list); |
| 279 | kfree(sdev->slave_ctrl_function); |
| 280 | kfree(sdev->protocol_function); |
| 281 | kfree(sdev->function_directory); |
| 282 | |
| 283 | kfree(sdev); |
| 284 | } |
| 285 | |
| 286 | /* This function assumes that the uid array is all 0 to start with. */ |
| 287 | static void set_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH], unsigned int bit, |
| 288 | unsigned int value) |
| 289 | { |
| 290 | u8 mask; |
| 291 | |
| 292 | if (bit >= BIF_UNIQUE_ID_BIT_LENGTH) |
| 293 | return; |
| 294 | |
| 295 | mask = 1 << (7 - (bit % 8)); |
| 296 | |
| 297 | uid[bit / 8] &= ~mask; |
| 298 | uid[bit / 8] |= value << (7 - (bit % 8)); |
| 299 | } |
| 300 | |
| 301 | static unsigned int get_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH], |
| 302 | unsigned int bit) |
| 303 | { |
| 304 | if (bit >= BIF_UNIQUE_ID_BIT_LENGTH) |
| 305 | return 0; |
| 306 | |
| 307 | return (uid[bit / 8] & (1 << (7 - (bit % 8)))) ? 1 : 0; |
| 308 | } |
| 309 | |
| 310 | static void bif_enter_irq_mode_work(struct work_struct *work) |
| 311 | { |
| 312 | struct delayed_work *dwork = to_delayed_work(work); |
| 313 | struct bif_ctrl_dev *bdev |
| 314 | = container_of(dwork, struct bif_ctrl_dev, enter_irq_mode_work); |
| 315 | int rc, i; |
| 316 | |
| 317 | mutex_lock(&bdev->mutex); |
| 318 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 319 | rc = bdev->desc->ops->set_bus_state(bdev, |
| 320 | BIF_BUS_STATE_INTERRUPT); |
| 321 | if (rc == 0) |
| 322 | break; |
| 323 | } |
| 324 | mutex_unlock(&bdev->mutex); |
| 325 | |
| 326 | /* Reschedule the task if the transaction failed. */ |
| 327 | if (rc) { |
| 328 | pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc); |
| 329 | schedule_delayed_work(&bdev->enter_irq_mode_work, |
| 330 | bdev->irq_mode_delay_jiffies); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void bif_cancel_irq_mode_work(struct bif_ctrl_dev *bdev) |
| 335 | { |
| 336 | cancel_delayed_work(&bdev->enter_irq_mode_work); |
| 337 | } |
| 338 | |
| 339 | static void bif_schedule_irq_mode_work(struct bif_ctrl_dev *bdev) |
| 340 | { |
| 341 | if (bdev->irq_count > 0 && |
| 342 | bdev->desc->ops->get_bus_state(bdev) != BIF_BUS_STATE_INTERRUPT) |
| 343 | schedule_delayed_work(&bdev->enter_irq_mode_work, |
| 344 | bdev->irq_mode_delay_jiffies); |
| 345 | } |
| 346 | |
| 347 | static int _bif_select_slave_no_retry(struct bif_slave_dev *sdev) |
| 348 | { |
| 349 | struct bif_ctrl_dev *bdev = sdev->bdev; |
| 350 | int rc = 0; |
| 351 | int i; |
| 352 | |
| 353 | /* Check if the slave is already selected. */ |
| 354 | if (sdev->bdev->selected_sdev == sdev) |
| 355 | return 0; |
| 356 | |
| 357 | if (sdev->slave_addr) { |
| 358 | /* Select using DEV_ADR. */ |
| 359 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA, |
| 360 | sdev->slave_addr); |
| 361 | if (!rc) |
| 362 | sdev->bdev->selected_sdev = sdev; |
| 363 | } else if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH) { |
| 364 | /* Select using full UID. */ |
| 365 | for (i = 0; i < BIF_UNIQUE_ID_BYTE_LENGTH - 1; i++) { |
| 366 | rc = bdev->desc->ops->bus_transaction(bdev, |
| 367 | BIF_TRANS_EDA, sdev->unique_id[i]); |
| 368 | if (rc) |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA, |
| 373 | sdev->unique_id[BIF_UNIQUE_ID_BYTE_LENGTH - 1]); |
| 374 | if (rc) |
| 375 | goto out; |
| 376 | } else { |
| 377 | pr_err("Cannot select slave because it has neither UID nor DEV_ADR.\n"); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | sdev->bdev->selected_sdev = sdev; |
| 382 | |
| 383 | return 0; |
| 384 | out: |
| 385 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 386 | return rc; |
| 387 | } |
| 388 | |
| 389 | static int bif_select_slave(struct bif_slave_dev *sdev) |
| 390 | { |
| 391 | int rc = -EPERM; |
| 392 | int i; |
| 393 | |
| 394 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 395 | rc = _bif_select_slave_no_retry(sdev); |
| 396 | if (rc == 0) |
| 397 | break; |
| 398 | /* Force slave reselection. */ |
| 399 | sdev->bdev->selected_sdev = NULL; |
| 400 | } |
| 401 | |
| 402 | return rc; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Returns 1 if slave is selected, 0 if slave is not selected, or errno if |
| 407 | * error. |
| 408 | */ |
| 409 | static int bif_is_slave_selected(struct bif_ctrl_dev *bdev) |
| 410 | { |
| 411 | int rc = -EPERM; |
| 412 | int tack, i; |
| 413 | |
| 414 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 415 | /* Attempt a transaction query. */ |
| 416 | rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_BC, |
| 417 | BIF_CMD_TQ, &tack); |
| 418 | if (rc == 0 || rc == -ETIMEDOUT) |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (rc == 0) |
| 423 | rc = 1; |
| 424 | else if (rc == -ETIMEDOUT) |
| 425 | rc = 0; |
| 426 | else |
| 427 | pr_err("BIF bus_transaction_read failed, rc=%d\n", rc); |
| 428 | |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | /* Read from a specified number of consecutive registers. */ |
| 433 | static int _bif_slave_read_no_retry(struct bif_slave_dev *sdev, u16 addr, |
| 434 | u8 *buf, int len) |
| 435 | { |
| 436 | struct bif_ctrl_dev *bdev = sdev->bdev; |
| 437 | int rc = 0; |
| 438 | int i, response; |
| 439 | |
| 440 | rc = bif_select_slave(sdev); |
| 441 | if (rc) |
| 442 | return rc; |
| 443 | |
| 444 | if (bdev->desc->ops->read_slave_registers) { |
| 445 | /* |
| 446 | * Use low level slave register read implementation in order to |
| 447 | * receive the benefits of BIF burst reads. |
| 448 | */ |
| 449 | rc = bdev->desc->ops->read_slave_registers(bdev, addr, buf, |
| 450 | len); |
| 451 | if (rc) |
David Collins | a984d9e | 2013-05-03 16:16:31 -0700 | [diff] [blame] | 452 | pr_debug("read_slave_registers failed, rc=%d\n", rc); |
| 453 | else |
| 454 | return rc; |
| 455 | /* |
| 456 | * Fall back on individual transactions if high level register |
| 457 | * read failed. |
| 458 | */ |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | for (i = 0; i < len; i++) { |
| 462 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA, |
| 463 | addr >> 8); |
| 464 | if (rc) { |
| 465 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_RRA, |
| 470 | addr & 0xFF, &response); |
| 471 | if (rc) { |
| 472 | pr_err("bus_transaction_read failed, rc=%d\n", rc); |
| 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | if (!(response & BIF_SLAVE_RD_ACK)) { |
| 477 | pr_err("BIF register read error=0x%02X\n", |
| 478 | response & BIF_SLAVE_RD_ERR); |
| 479 | return -EIO; |
| 480 | } |
| 481 | |
| 482 | buf[i] = response & BIF_SLAVE_RD_DATA; |
| 483 | addr++; |
| 484 | } |
| 485 | |
| 486 | return rc; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Read from a specified number of consecutive registers. Retry the transaction |
| 491 | * several times in case of communcation failures. |
| 492 | */ |
| 493 | static int _bif_slave_read(struct bif_slave_dev *sdev, u16 addr, u8 *buf, |
| 494 | int len) |
| 495 | { |
| 496 | int rc = -EPERM; |
| 497 | int i; |
| 498 | |
| 499 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 500 | rc = _bif_slave_read_no_retry(sdev, addr, buf, len); |
| 501 | if (rc == 0) |
| 502 | break; |
| 503 | /* Force slave reselection. */ |
| 504 | sdev->bdev->selected_sdev = NULL; |
| 505 | } |
| 506 | |
| 507 | return rc; |
| 508 | } |
| 509 | |
| 510 | /* Write to a specified number of consecutive registers. */ |
| 511 | static int _bif_slave_write_no_retry(struct bif_slave_dev *sdev, u16 addr, |
| 512 | u8 *buf, int len) |
| 513 | { |
| 514 | struct bif_ctrl_dev *bdev = sdev->bdev; |
| 515 | int rc = 0; |
| 516 | int i; |
| 517 | |
| 518 | rc = bif_select_slave(sdev); |
| 519 | if (rc) |
| 520 | return rc; |
| 521 | |
| 522 | if (bdev->desc->ops->write_slave_registers) { |
| 523 | /* |
| 524 | * Use low level slave register write implementation in order to |
| 525 | * receive the benefits of BIF burst writes. |
| 526 | */ |
| 527 | rc = bdev->desc->ops->write_slave_registers(bdev, addr, buf, |
| 528 | len); |
| 529 | if (rc) |
David Collins | a984d9e | 2013-05-03 16:16:31 -0700 | [diff] [blame] | 530 | pr_debug("write_slave_registers failed, rc=%d\n", rc); |
| 531 | else |
| 532 | return rc; |
| 533 | /* |
| 534 | * Fall back on individual transactions if high level register |
| 535 | * write failed. |
| 536 | */ |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA, addr >> 8); |
| 540 | if (rc) |
| 541 | goto out; |
| 542 | |
| 543 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WRA, addr & 0xFF); |
| 544 | if (rc) |
| 545 | goto out; |
| 546 | |
| 547 | for (i = 0; i < len; i++) { |
| 548 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WD, |
| 549 | buf[i]); |
| 550 | if (rc) |
| 551 | goto out; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | out: |
| 556 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 557 | return rc; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Write to a specified number of consecutive registers. Retry the transaction |
| 562 | * several times in case of communcation failures. |
| 563 | */ |
| 564 | static int _bif_slave_write(struct bif_slave_dev *sdev, u16 addr, u8 *buf, |
| 565 | int len) |
| 566 | { |
| 567 | int rc = -EPERM; |
| 568 | int i; |
| 569 | |
| 570 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 571 | rc = _bif_slave_write_no_retry(sdev, addr, buf, len); |
| 572 | if (rc == 0) |
| 573 | break; |
| 574 | /* Force slave reselection. */ |
| 575 | sdev->bdev->selected_sdev = NULL; |
| 576 | } |
| 577 | |
| 578 | return rc; |
| 579 | } |
| 580 | |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 581 | /* Perform a read-modify-write sequence on a single BIF slave register. */ |
| 582 | static int _bif_slave_masked_write(struct bif_slave_dev *sdev, u16 addr, u8 val, |
| 583 | u8 mask) |
| 584 | { |
| 585 | int rc; |
| 586 | u8 reg; |
| 587 | |
| 588 | rc = _bif_slave_read(sdev, addr, ®, 1); |
| 589 | if (rc) |
| 590 | return rc; |
| 591 | |
| 592 | reg = (reg & ~mask) | (val & mask); |
| 593 | |
| 594 | return _bif_slave_write(sdev, addr, ®, 1); |
| 595 | } |
| 596 | |
| 597 | static int _bif_check_task(struct bif_slave_dev *sdev, unsigned int task) |
| 598 | { |
| 599 | if (IS_ERR_OR_NULL(sdev)) { |
| 600 | pr_err("Invalid slave device handle=%ld\n", PTR_ERR(sdev)); |
| 601 | return -EINVAL; |
| 602 | } else if (!sdev->bdev) { |
| 603 | pr_err("BIF controller has been removed\n"); |
| 604 | return -ENXIO; |
| 605 | } else if (!sdev->slave_ctrl_function |
| 606 | || sdev->slave_ctrl_function->task_count == 0) { |
| 607 | pr_err("BIF slave does not support slave control\n"); |
| 608 | return -ENODEV; |
| 609 | } else if (task >= sdev->slave_ctrl_function->task_count) { |
| 610 | pr_err("Requested task: %u greater than max: %u for this slave\n", |
| 611 | task, sdev->slave_ctrl_function->task_count); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int _bif_task_is_busy(struct bif_slave_dev *sdev, unsigned int task) |
| 619 | { |
| 620 | int rc; |
| 621 | u16 addr; |
| 622 | u8 reg = 0; |
| 623 | |
| 624 | rc = _bif_check_task(sdev, task); |
| 625 | if (rc) { |
| 626 | pr_err("Invalid slave device or task, rc=%d\n", rc); |
| 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | /* Check the task busy state. */ |
| 631 | addr = SLAVE_CTRL_FUNC_TASK_BUSY_ADDR( |
| 632 | sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 633 | rc = _bif_slave_read(sdev, addr, ®, 1); |
| 634 | if (rc) { |
| 635 | pr_err("BIF slave register read failed, rc=%d\n", rc); |
| 636 | return rc; |
| 637 | } |
| 638 | |
| 639 | return (reg & BIT(task % SLAVE_CTRL_TASKS_PER_SET)) ? 1 : 0; |
| 640 | } |
| 641 | |
| 642 | static int _bif_enable_auto_task(struct bif_slave_dev *sdev, unsigned int task) |
| 643 | { |
| 644 | int rc; |
| 645 | u16 addr; |
| 646 | u8 mask; |
| 647 | |
| 648 | rc = _bif_check_task(sdev, task); |
| 649 | if (rc) { |
| 650 | pr_err("Invalid slave device or task, rc=%d\n", rc); |
| 651 | return rc; |
| 652 | } |
| 653 | |
| 654 | /* Enable the auto task within the slave */ |
| 655 | mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET); |
| 656 | addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR( |
| 657 | sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 658 | if (task / SLAVE_CTRL_TASKS_PER_SET == 0) { |
| 659 | /* Set global auto task enable. */ |
| 660 | mask |= BIT(0); |
| 661 | } |
| 662 | rc = _bif_slave_masked_write(sdev, addr, 0xFF, mask); |
| 663 | if (rc) { |
| 664 | pr_err("BIF slave register masked write failed, rc=%d\n", rc); |
| 665 | return rc; |
| 666 | } |
| 667 | |
| 668 | /* Set global auto task enable if task not in set 0. */ |
| 669 | if (task / SLAVE_CTRL_TASKS_PER_SET != 0) { |
| 670 | addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR( |
| 671 | sdev->slave_ctrl_function->slave_ctrl_pointer, 0); |
| 672 | rc = _bif_slave_masked_write(sdev, addr, 0xFF, BIT(0)); |
| 673 | if (rc) { |
| 674 | pr_err("BIF slave register masked write failed, rc=%d\n", |
| 675 | rc); |
| 676 | return rc; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return rc; |
| 681 | } |
| 682 | |
| 683 | static int _bif_disable_auto_task(struct bif_slave_dev *sdev, unsigned int task) |
| 684 | { |
| 685 | int rc; |
| 686 | u16 addr; |
| 687 | u8 mask; |
| 688 | |
| 689 | rc = _bif_check_task(sdev, task); |
| 690 | if (rc) { |
| 691 | pr_err("Invalid slave or task, rc=%d\n", rc); |
| 692 | return rc; |
| 693 | } |
| 694 | |
| 695 | /* Disable the auto task within the slave */ |
| 696 | mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET); |
| 697 | addr = SLAVE_CTRL_FUNC_TASK_AUTO_TRIGGER_ADDR( |
| 698 | sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 699 | rc = _bif_slave_masked_write(sdev, addr, 0x00, mask); |
| 700 | if (rc) { |
| 701 | pr_err("BIF slave register masked write failed, rc=%d\n", rc); |
| 702 | return rc; |
| 703 | } |
| 704 | |
| 705 | return rc; |
| 706 | } |
| 707 | |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 708 | /* |
| 709 | * The MIPI-BIF spec does not define a maximum time in which an NVM write must |
| 710 | * complete. The following delay and recheck count therefore represent |
| 711 | * arbitrary but reasonable values. |
| 712 | */ |
| 713 | #define NVM_WRITE_POLL_DELAY_MS 20 |
| 714 | #define NVM_WRITE_MAX_POLL_COUNT 50 |
| 715 | |
| 716 | static int _bif_slave_nvm_raw_write(struct bif_slave_dev *sdev, u16 offset, |
| 717 | u8 *buf, int len) |
| 718 | { |
| 719 | int rc = 0; |
| 720 | int write_len, poll_count, rc2; |
| 721 | u8 write_buf[3]; |
| 722 | |
| 723 | if (!sdev->nvm_function) { |
| 724 | pr_err("BIF slave has no NVM function\n"); |
| 725 | return -ENODEV; |
| 726 | } else if (offset + len > sdev->nvm_function->nvm_size) { |
| 727 | pr_err("write offset + len = %d > NVM size = %d\n", |
| 728 | offset + len, sdev->nvm_function->nvm_size); |
| 729 | return -EINVAL; |
| 730 | } else if (offset < sdev->nvm_function->nvm_lock_offset) { |
| 731 | pr_err("write offset = %d < first writable offset = %d\n", |
| 732 | offset, sdev->nvm_function->nvm_lock_offset); |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | |
| 736 | rc = _bif_enable_auto_task(sdev, |
| 737 | sdev->nvm_function->slave_control_channel); |
| 738 | if (rc) { |
| 739 | pr_err("Failed to enable NVM auto task, rc=%d\n", rc); |
| 740 | return rc; |
| 741 | } |
| 742 | |
| 743 | while (len > 0) { |
| 744 | write_len = sdev->nvm_function->write_buffer_size; |
| 745 | if (write_len == 0) |
| 746 | write_len = 256; |
| 747 | write_len = min(write_len, len); |
| 748 | |
| 749 | write_buf[0] = offset >> 8; |
| 750 | write_buf[1] = offset; |
| 751 | write_buf[2] = (write_len == 256) ? 0 : write_len; |
| 752 | |
| 753 | /* Write offset and size registers. */ |
| 754 | rc = _bif_slave_write(sdev, sdev->nvm_function->nvm_pointer + 6, |
| 755 | write_buf, 3); |
| 756 | if (rc) { |
| 757 | pr_err("BIF slave write failed, rc=%d\n", rc); |
| 758 | goto done; |
| 759 | } |
| 760 | |
| 761 | /* Write to NVM write buffer registers. */ |
| 762 | rc = _bif_slave_write(sdev, sdev->nvm_function->nvm_pointer + 9, |
| 763 | buf, write_len); |
| 764 | if (rc) { |
| 765 | pr_err("BIF slave write failed, rc=%d\n", rc); |
| 766 | goto done; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Wait for completion of the NVM write which was auto-triggered |
| 771 | * by the register write of the last byte in the NVM write |
| 772 | * buffer. |
| 773 | */ |
| 774 | poll_count = NVM_WRITE_MAX_POLL_COUNT; |
| 775 | do { |
| 776 | msleep(NVM_WRITE_POLL_DELAY_MS); |
| 777 | rc = _bif_task_is_busy(sdev, |
| 778 | sdev->nvm_function->slave_control_channel); |
| 779 | poll_count--; |
| 780 | } while (rc > 0 && poll_count > 0); |
| 781 | |
| 782 | if (rc < 0) { |
| 783 | pr_err("Failed to check task state, rc=%d", rc); |
| 784 | goto done; |
| 785 | } else if (rc > 0) { |
| 786 | pr_err("BIF slave NVM write not completed after %d ms\n", |
| 787 | NVM_WRITE_POLL_DELAY_MS * NVM_WRITE_MAX_POLL_COUNT); |
| 788 | rc = -ETIMEDOUT; |
| 789 | goto done; |
| 790 | } |
| 791 | |
| 792 | len -= write_len; |
| 793 | offset += write_len; |
| 794 | buf += write_len; |
| 795 | } |
| 796 | |
| 797 | done: |
| 798 | rc2 = _bif_disable_auto_task(sdev, |
| 799 | sdev->nvm_function->slave_control_channel); |
| 800 | if (rc2) { |
| 801 | pr_err("Failed to disable NVM auto task, rc=%d\n", rc2); |
| 802 | return rc2; |
| 803 | } |
| 804 | |
| 805 | return rc; |
| 806 | } |
| 807 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 808 | /* Takes a mutex if this consumer is not an exclusive bus user. */ |
| 809 | static void bif_ctrl_lock(struct bif_ctrl *ctrl) |
| 810 | { |
| 811 | if (!ctrl->exclusive_lock) { |
| 812 | mutex_lock(&ctrl->bdev->mutex); |
| 813 | bif_cancel_irq_mode_work(ctrl->bdev); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* Releases a mutex if this consumer is not an exclusive bus user. */ |
| 818 | static void bif_ctrl_unlock(struct bif_ctrl *ctrl) |
| 819 | { |
| 820 | if (!ctrl->exclusive_lock) { |
| 821 | bif_schedule_irq_mode_work(ctrl->bdev); |
| 822 | mutex_unlock(&ctrl->bdev->mutex); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | static void bif_slave_ctrl_lock(struct bif_slave *slave) |
| 827 | { |
| 828 | bif_ctrl_lock(&slave->ctrl); |
| 829 | } |
| 830 | |
| 831 | static void bif_slave_ctrl_unlock(struct bif_slave *slave) |
| 832 | { |
| 833 | bif_ctrl_unlock(&slave->ctrl); |
| 834 | } |
| 835 | |
David Collins | 2cf302f | 2013-09-23 17:33:13 -0700 | [diff] [blame] | 836 | /** |
| 837 | * bif_crc_ccitt() - calculate the CRC-CCITT CRC value of the data specified |
| 838 | * @buffer: Data to calculate the CRC of |
| 839 | * @len: Length of the data buffer in bytes |
| 840 | * |
| 841 | * MIPI-BIF specifies the usage of CRC-CCITT for BIF data objects. This |
| 842 | * function performs the CRC calculation while taking into account the bit |
| 843 | * ordering used by BIF. |
| 844 | */ |
| 845 | u16 bif_crc_ccitt(const u8 *buffer, unsigned int len) |
| 846 | { |
| 847 | u16 crc = 0xFFFF; |
| 848 | |
| 849 | while (len--) { |
| 850 | crc = crc_ccitt_byte(crc, bitrev8(*buffer)); |
| 851 | buffer++; |
| 852 | } |
| 853 | return bitrev16(crc); |
| 854 | } |
| 855 | EXPORT_SYMBOL(bif_crc_ccitt); |
| 856 | |
| 857 | static u16 bif_object_crc_ccitt(const struct bif_object *object) |
| 858 | { |
| 859 | u16 crc = 0xFFFF; |
| 860 | int i; |
| 861 | |
| 862 | crc = crc_ccitt_byte(crc, bitrev8(object->type)); |
| 863 | crc = crc_ccitt_byte(crc, bitrev8(object->version)); |
| 864 | crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id >> 8)); |
| 865 | crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id)); |
| 866 | crc = crc_ccitt_byte(crc, bitrev8(object->length >> 8)); |
| 867 | crc = crc_ccitt_byte(crc, bitrev8(object->length)); |
| 868 | |
| 869 | for (i = 0; i < object->length - 8; i++) |
| 870 | crc = crc_ccitt_byte(crc, bitrev8(object->data[i])); |
| 871 | |
| 872 | return bitrev16(crc); |
| 873 | } |
| 874 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 875 | static int bif_check_task(struct bif_slave *slave, unsigned int task) |
| 876 | { |
| 877 | if (IS_ERR_OR_NULL(slave)) { |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 878 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 879 | return -EINVAL; |
| 880 | } |
| 881 | |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 882 | return _bif_check_task(slave->sdev, task); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
| 886 | * bif_request_irq() - request a BIF slave IRQ by slave task number |
| 887 | * @slave: BIF slave handle |
| 888 | * @task: BIF task number of the IRQ inside of the slave. This |
| 889 | * corresponds to the slave control channel specified for a given |
| 890 | * BIF function inside of the slave. |
| 891 | * @nb: Notifier block to call when the IRQ fires |
| 892 | * |
| 893 | * This function registers a notifier block to call when the BIF slave interrupt |
| 894 | * is triggered and also enables the interrupt. The interrupt is enabled inside |
| 895 | * of the BIF slave's slave control function and also the BIF bus is put into |
| 896 | * interrupt mode. |
| 897 | * |
| 898 | * Returns 0 for success or errno if an error occurred. |
| 899 | */ |
| 900 | int bif_request_irq(struct bif_slave *slave, unsigned int task, |
| 901 | struct notifier_block *nb) |
| 902 | { |
| 903 | int rc; |
| 904 | u16 addr; |
| 905 | u8 reg, mask; |
| 906 | |
| 907 | rc = bif_check_task(slave, task); |
| 908 | if (rc) { |
| 909 | pr_err("Invalid slave or task, rc=%d\n", rc); |
| 910 | return rc; |
| 911 | } |
| 912 | |
| 913 | bif_slave_ctrl_lock(slave); |
| 914 | |
| 915 | rc = blocking_notifier_chain_register( |
| 916 | &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb); |
| 917 | if (rc) { |
| 918 | pr_err("Notifier registration failed, rc=%d\n", rc); |
| 919 | goto done; |
| 920 | } |
| 921 | |
| 922 | /* Enable the interrupt within the slave */ |
| 923 | mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET); |
| 924 | addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR( |
| 925 | slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 926 | if (task / SLAVE_CTRL_TASKS_PER_SET == 0) { |
| 927 | /* Set global interrupt enable. */ |
| 928 | mask |= BIT(0); |
| 929 | } |
| 930 | rc = _bif_slave_read(slave->sdev, addr, ®, 1); |
| 931 | if (rc) { |
| 932 | pr_err("BIF slave register read failed, rc=%d\n", rc); |
| 933 | goto notifier_unregister; |
| 934 | } |
| 935 | reg |= mask; |
| 936 | rc = _bif_slave_write(slave->sdev, addr, ®, 1); |
| 937 | if (rc) { |
| 938 | pr_err("BIF slave register write failed, rc=%d\n", rc); |
| 939 | goto notifier_unregister; |
| 940 | } |
| 941 | |
| 942 | /* Set global interrupt enable if task not in set 0. */ |
| 943 | if (task / SLAVE_CTRL_TASKS_PER_SET != 0) { |
| 944 | mask = BIT(0); |
| 945 | addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR( |
| 946 | slave->sdev->slave_ctrl_function->slave_ctrl_pointer, 0); |
| 947 | rc = _bif_slave_read(slave->sdev, addr, ®, 1); |
| 948 | if (rc) { |
| 949 | pr_err("BIF slave register read failed, rc=%d\n", rc); |
| 950 | goto notifier_unregister; |
| 951 | } |
| 952 | reg |= mask; |
| 953 | rc = _bif_slave_write(slave->sdev, addr, ®, 1); |
| 954 | if (rc) { |
| 955 | pr_err("BIF slave register write failed, rc=%d\n", rc); |
| 956 | goto notifier_unregister; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | rc = slave->sdev->bdev->desc->ops->set_bus_state(slave->sdev->bdev, |
| 961 | BIF_BUS_STATE_INTERRUPT); |
| 962 | if (rc) { |
| 963 | pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc); |
| 964 | goto notifier_unregister; |
| 965 | } |
| 966 | |
| 967 | slave->sdev->bdev->irq_count++; |
| 968 | done: |
| 969 | bif_slave_ctrl_unlock(slave); |
| 970 | |
| 971 | return rc; |
| 972 | |
| 973 | notifier_unregister: |
| 974 | blocking_notifier_chain_unregister( |
| 975 | &slave->sdev->slave_ctrl_function->irq_notifier_list[task], |
| 976 | nb); |
| 977 | bif_slave_ctrl_unlock(slave); |
| 978 | |
| 979 | return rc; |
| 980 | |
| 981 | } |
| 982 | EXPORT_SYMBOL(bif_request_irq); |
| 983 | |
| 984 | /** |
| 985 | * bif_free_irq() - free a BIF slave IRQ by slave task number |
| 986 | * @slave: BIF slave handle |
| 987 | * @task: BIF task number of the IRQ inside of the slave. This |
| 988 | * corresponds to the slave control channel specified for a given |
| 989 | * BIF function inside of the slave. |
| 990 | * @nb: Notifier block previously registered with this interrupt |
| 991 | * |
| 992 | * This function unregisters a notifier block that was previously registered |
| 993 | * with bif_request_irq(). |
| 994 | * |
| 995 | * Returns 0 for success or errno if an error occurred. |
| 996 | */ |
| 997 | int bif_free_irq(struct bif_slave *slave, unsigned int task, |
| 998 | struct notifier_block *nb) |
| 999 | { |
| 1000 | int rc; |
| 1001 | u16 addr; |
| 1002 | u8 reg; |
| 1003 | |
| 1004 | rc = bif_check_task(slave, task); |
| 1005 | if (rc) { |
| 1006 | pr_err("Invalid slave or task, rc=%d\n", rc); |
| 1007 | return rc; |
| 1008 | } |
| 1009 | |
| 1010 | bif_slave_ctrl_lock(slave); |
| 1011 | |
| 1012 | /* Disable the interrupt within the slave */ |
| 1013 | reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET); |
| 1014 | addr = SLAVE_CTRL_FUNC_IRQ_CLEAR_ADDR( |
| 1015 | slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 1016 | rc = _bif_slave_write(slave->sdev, addr, ®, 1); |
| 1017 | if (rc) { |
| 1018 | pr_err("BIF slave register write failed, rc=%d\n", rc); |
| 1019 | goto done; |
| 1020 | } |
| 1021 | |
| 1022 | rc = blocking_notifier_chain_unregister( |
| 1023 | &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb); |
| 1024 | if (rc) { |
| 1025 | pr_err("Notifier unregistration failed, rc=%d\n", rc); |
| 1026 | goto done; |
| 1027 | } |
| 1028 | |
| 1029 | slave->sdev->bdev->irq_count--; |
| 1030 | |
| 1031 | if (slave->sdev->bdev->irq_count == 0) { |
| 1032 | bif_cancel_irq_mode_work(slave->sdev->bdev); |
| 1033 | } else if (slave->sdev->bdev->irq_count < 0) { |
| 1034 | pr_err("Unbalanced IRQ free.\n"); |
| 1035 | rc = -EINVAL; |
| 1036 | slave->sdev->bdev->irq_count = 0; |
| 1037 | } |
| 1038 | done: |
| 1039 | bif_slave_ctrl_unlock(slave); |
| 1040 | |
| 1041 | return rc; |
| 1042 | } |
| 1043 | EXPORT_SYMBOL(bif_free_irq); |
| 1044 | |
| 1045 | /** |
| 1046 | * bif_trigger_task() - trigger a task within a BIF slave |
| 1047 | * @slave: BIF slave handle |
| 1048 | * @task: BIF task inside of the slave to trigger. This corresponds to |
| 1049 | * the slave control channel specified for a given BIF function |
| 1050 | * inside of the slave. |
| 1051 | * |
| 1052 | * Returns 0 for success or errno if an error occurred. |
| 1053 | */ |
| 1054 | int bif_trigger_task(struct bif_slave *slave, unsigned int task) |
| 1055 | { |
| 1056 | int rc; |
| 1057 | u16 addr; |
| 1058 | u8 reg; |
| 1059 | |
| 1060 | rc = bif_check_task(slave, task); |
| 1061 | if (rc) { |
| 1062 | pr_err("Invalid slave or task, rc=%d\n", rc); |
| 1063 | return rc; |
| 1064 | } |
| 1065 | |
| 1066 | bif_slave_ctrl_lock(slave); |
| 1067 | |
| 1068 | /* Trigger the task within the slave. */ |
| 1069 | reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET); |
| 1070 | addr = SLAVE_CTRL_FUNC_TASK_TRIGGER_ADDR( |
| 1071 | slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task); |
| 1072 | rc = _bif_slave_write(slave->sdev, addr, ®, 1); |
| 1073 | if (rc) { |
| 1074 | pr_err("BIF slave register write failed, rc=%d\n", rc); |
| 1075 | goto done; |
| 1076 | } |
| 1077 | |
| 1078 | done: |
| 1079 | bif_slave_ctrl_unlock(slave); |
| 1080 | |
| 1081 | return rc; |
| 1082 | } |
| 1083 | EXPORT_SYMBOL(bif_trigger_task); |
| 1084 | |
| 1085 | /** |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 1086 | * bif_enable_auto_task() - enable task auto triggering for the specified task |
| 1087 | * @slave: BIF slave handle |
| 1088 | * @task: BIF task inside of the slave to configure for automatic |
| 1089 | * triggering. This corresponds to the slave control channel |
| 1090 | * specified for a given BIF function inside of the slave. |
| 1091 | * |
| 1092 | * Returns 0 for success or errno if an error occurred. |
| 1093 | */ |
| 1094 | int bif_enable_auto_task(struct bif_slave *slave, unsigned int task) |
| 1095 | { |
| 1096 | int rc; |
| 1097 | |
| 1098 | if (IS_ERR_OR_NULL(slave)) { |
| 1099 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
| 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
| 1103 | bif_slave_ctrl_lock(slave); |
| 1104 | rc = _bif_enable_auto_task(slave->sdev, task); |
| 1105 | bif_slave_ctrl_unlock(slave); |
| 1106 | |
| 1107 | return rc; |
| 1108 | } |
| 1109 | EXPORT_SYMBOL(bif_enable_auto_task); |
| 1110 | |
| 1111 | /** |
| 1112 | * bif_disable_auto_task() - disable task auto triggering for the specified task |
| 1113 | * @slave: BIF slave handle |
| 1114 | * @task: BIF task inside of the slave to stop automatic triggering on. |
| 1115 | * This corresponds to the slave control channel specified for a |
| 1116 | * given BIF function inside of the slave. |
| 1117 | * |
| 1118 | * This function should be called after bif_enable_auto_task() in a paired |
| 1119 | * fashion. |
| 1120 | * |
| 1121 | * Returns 0 for success or errno if an error occurred. |
| 1122 | */ |
| 1123 | int bif_disable_auto_task(struct bif_slave *slave, unsigned int task) |
| 1124 | { |
| 1125 | int rc; |
| 1126 | |
| 1127 | if (IS_ERR_OR_NULL(slave)) { |
| 1128 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
| 1132 | bif_slave_ctrl_lock(slave); |
| 1133 | rc = _bif_disable_auto_task(slave->sdev, task); |
| 1134 | bif_slave_ctrl_unlock(slave); |
| 1135 | |
| 1136 | return rc; |
| 1137 | } |
| 1138 | EXPORT_SYMBOL(bif_disable_auto_task); |
| 1139 | |
| 1140 | /** |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1141 | * bif_task_is_busy() - checks the state of a BIF slave task |
| 1142 | * @slave: BIF slave handle |
| 1143 | * @task: BIF task inside of the slave to trigger. This corresponds to |
| 1144 | * the slave control channel specified for a given BIF function |
| 1145 | * inside of the slave. |
| 1146 | * |
| 1147 | * Returns 1 if the task is busy, 0 if it is not busy, and errno on error. |
| 1148 | */ |
| 1149 | int bif_task_is_busy(struct bif_slave *slave, unsigned int task) |
| 1150 | { |
| 1151 | int rc; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1152 | |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 1153 | if (IS_ERR_OR_NULL(slave)) { |
| 1154 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
| 1155 | return -EINVAL; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | bif_slave_ctrl_lock(slave); |
David Collins | bf6a635 | 2013-04-11 13:46:53 -0700 | [diff] [blame] | 1159 | rc = _bif_task_is_busy(slave->sdev, task); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1160 | bif_slave_ctrl_unlock(slave); |
| 1161 | |
| 1162 | return rc; |
| 1163 | } |
| 1164 | EXPORT_SYMBOL(bif_task_is_busy); |
| 1165 | |
| 1166 | static int bif_slave_notify_irqs(struct bif_slave_dev *sdev, int set, u8 val) |
| 1167 | { |
| 1168 | int rc = 0; |
| 1169 | int i, task; |
| 1170 | |
| 1171 | for (i = 0; i < SLAVE_CTRL_TASKS_PER_SET; i++) { |
| 1172 | if (val & (1 << i)) { |
| 1173 | task = set * SLAVE_CTRL_TASKS_PER_SET + i; |
| 1174 | |
| 1175 | rc = blocking_notifier_call_chain( |
| 1176 | &sdev->slave_ctrl_function->irq_notifier_list[task], |
| 1177 | task, sdev->bdev); |
| 1178 | rc = notifier_to_errno(rc); |
| 1179 | if (rc) |
| 1180 | pr_err("Notification failed for task %d\n", |
| 1181 | task); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return rc; |
| 1186 | } |
| 1187 | |
| 1188 | static int bif_slave_handle_irq(struct bif_slave_dev *sdev) |
| 1189 | { |
| 1190 | struct bif_ctrl_dev *bdev = sdev->bdev; |
| 1191 | bool resp = false; |
| 1192 | int rc = 0; |
| 1193 | int i; |
| 1194 | u16 addr; |
| 1195 | u8 reg; |
| 1196 | |
| 1197 | mutex_lock(&sdev->bdev->mutex); |
| 1198 | bif_cancel_irq_mode_work(sdev->bdev); |
| 1199 | |
| 1200 | rc = bif_select_slave(sdev); |
| 1201 | if (rc) { |
| 1202 | pr_err("Could not select slave, rc=%d\n", rc); |
| 1203 | goto done; |
| 1204 | } |
| 1205 | |
| 1206 | /* Check overall slave interrupt status. */ |
| 1207 | rc = bdev->desc->ops->bus_transaction_query(bdev, BIF_TRANS_BC, |
| 1208 | BIF_CMD_ISTS, &resp); |
| 1209 | if (rc) { |
| 1210 | pr_err("Could not query slave interrupt status, rc=%d\n", rc); |
| 1211 | goto done; |
| 1212 | } |
| 1213 | |
| 1214 | if (resp) { |
| 1215 | for (i = 0; i < sdev->slave_ctrl_function->task_count |
| 1216 | / SLAVE_CTRL_TASKS_PER_SET; i++) { |
| 1217 | addr = sdev->slave_ctrl_function->slave_ctrl_pointer |
| 1218 | + 4 * i + 1; |
| 1219 | rc = _bif_slave_read(sdev, addr, ®, 1); |
| 1220 | if (rc) { |
| 1221 | pr_err("BIF slave register read failed, rc=%d\n", |
| 1222 | rc); |
| 1223 | goto done; |
| 1224 | } |
| 1225 | |
| 1226 | /* Ensure that interrupts are pending in the set. */ |
| 1227 | if (reg != 0x00) { |
| 1228 | /* |
| 1229 | * Release mutex before notifying consumers so |
| 1230 | * that they can use the bus. |
| 1231 | */ |
| 1232 | mutex_unlock(&sdev->bdev->mutex); |
| 1233 | rc = bif_slave_notify_irqs(sdev, i, reg); |
| 1234 | if (rc) { |
| 1235 | pr_err("BIF slave irq notification failed, rc=%d\n", |
| 1236 | rc); |
| 1237 | goto notification_failed; |
| 1238 | } |
| 1239 | mutex_lock(&sdev->bdev->mutex); |
| 1240 | |
| 1241 | rc = bif_select_slave(sdev); |
| 1242 | if (rc) { |
| 1243 | pr_err("Could not select slave, rc=%d\n", |
| 1244 | rc); |
| 1245 | goto done; |
| 1246 | } |
| 1247 | |
| 1248 | /* Clear all interrupts in this set. */ |
| 1249 | rc = _bif_slave_write(sdev, addr, ®, 1); |
| 1250 | if (rc) { |
| 1251 | pr_err("BIF slave register write failed, rc=%d\n", |
| 1252 | rc); |
| 1253 | goto done; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | done: |
| 1260 | bif_schedule_irq_mode_work(sdev->bdev); |
| 1261 | mutex_unlock(&sdev->bdev->mutex); |
| 1262 | notification_failed: |
| 1263 | if (rc == 0) |
| 1264 | rc = resp; |
| 1265 | return rc; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * bif_ctrl_notify_slave_irq() - notify the BIF framework that a slave interrupt |
| 1270 | * was received by a BIF controller |
| 1271 | * @bdev: BIF controller device pointer |
| 1272 | * |
| 1273 | * This function should only be called from a BIF controller driver. |
| 1274 | * |
| 1275 | * Returns 0 for success or errno if an error occurred. |
| 1276 | */ |
| 1277 | int bif_ctrl_notify_slave_irq(struct bif_ctrl_dev *bdev) |
| 1278 | { |
| 1279 | struct bif_slave_dev *sdev; |
| 1280 | int rc = 0, handled = 0; |
| 1281 | |
| 1282 | if (IS_ERR_OR_NULL(bdev)) |
| 1283 | return -EINVAL; |
| 1284 | |
| 1285 | mutex_lock(&bif_sdev_list_mutex); |
| 1286 | |
| 1287 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 1288 | if (sdev->bdev == bdev && sdev->present) { |
| 1289 | rc = bif_slave_handle_irq(sdev); |
| 1290 | if (rc < 0) { |
| 1291 | pr_err("Could not handle BIF slave irq, rc=%d\n", |
| 1292 | rc); |
| 1293 | break; |
| 1294 | } |
| 1295 | handled += rc; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | mutex_unlock(&bif_sdev_list_mutex); |
| 1300 | |
| 1301 | if (handled == 0) |
| 1302 | pr_info("Spurious BIF slave interrupt detected.\n"); |
| 1303 | |
| 1304 | if (rc > 0) |
| 1305 | rc = 0; |
| 1306 | |
| 1307 | return rc; |
| 1308 | } |
| 1309 | EXPORT_SYMBOL(bif_ctrl_notify_slave_irq); |
| 1310 | |
| 1311 | /** |
| 1312 | * bif_ctrl_notify_battery_changed() - notify the BIF framework that a battery |
| 1313 | * pack has been inserted or removed |
| 1314 | * @bdev: BIF controller device pointer |
| 1315 | * |
| 1316 | * This function should only be called from a BIF controller driver. |
| 1317 | * |
| 1318 | * Returns 0 for success or errno if an error occurred. |
| 1319 | */ |
| 1320 | int bif_ctrl_notify_battery_changed(struct bif_ctrl_dev *bdev) |
| 1321 | { |
| 1322 | int rc = 0; |
| 1323 | int present; |
| 1324 | |
| 1325 | if (IS_ERR_OR_NULL(bdev)) |
| 1326 | return -EINVAL; |
| 1327 | |
| 1328 | if (bdev->desc->ops->get_battery_presence) { |
| 1329 | present = bdev->desc->ops->get_battery_presence(bdev); |
| 1330 | if (present < 0) { |
| 1331 | pr_err("Could not determine battery presence, rc=%d\n", |
| 1332 | rc); |
| 1333 | return rc; |
| 1334 | } |
| 1335 | |
| 1336 | if (bdev->battery_present == !!present) |
| 1337 | return 0; |
| 1338 | |
| 1339 | bdev->battery_present = present; |
| 1340 | |
| 1341 | rc = blocking_notifier_call_chain(&bdev->bus_change_notifier, |
| 1342 | present ? BIF_BUS_EVENT_BATTERY_INSERTED |
| 1343 | : BIF_BUS_EVENT_BATTERY_REMOVED, bdev); |
| 1344 | if (rc) |
| 1345 | pr_err("Call chain noification failed, rc=%d\n", rc); |
| 1346 | } |
| 1347 | |
| 1348 | return rc; |
| 1349 | } |
| 1350 | EXPORT_SYMBOL(bif_ctrl_notify_battery_changed); |
| 1351 | |
| 1352 | /** |
| 1353 | * bif_ctrl_signal_battery_changed() - notify the BIF framework that a battery |
| 1354 | * pack has been inserted or removed |
| 1355 | * @ctrl: BIF controller consumer handle |
| 1356 | * |
| 1357 | * This function should only be called by a BIF consumer driver on systems where |
| 1358 | * the BIF controller driver is unable to determine when a battery is inserted |
| 1359 | * or removed. |
| 1360 | * |
| 1361 | * Returns 0 for success or errno if an error occurred. |
| 1362 | */ |
| 1363 | int bif_ctrl_signal_battery_changed(struct bif_ctrl *ctrl) |
| 1364 | { |
| 1365 | if (IS_ERR_OR_NULL(ctrl)) |
| 1366 | return -EINVAL; |
| 1367 | |
| 1368 | return bif_ctrl_notify_battery_changed(ctrl->bdev); |
| 1369 | } |
| 1370 | EXPORT_SYMBOL(bif_ctrl_signal_battery_changed); |
| 1371 | |
| 1372 | /** |
| 1373 | * bif_ctrl_notifier_register() - register a notifier block to be called when |
| 1374 | * a battery pack is inserted or removed |
| 1375 | * @ctrl: BIF controller consumer handle |
| 1376 | * |
| 1377 | * The value passed into the notifier when it is called is one of |
| 1378 | * enum bif_bus_event. |
| 1379 | * |
| 1380 | * Returns 0 for success or errno if an error occurred. |
| 1381 | */ |
| 1382 | int bif_ctrl_notifier_register(struct bif_ctrl *ctrl, struct notifier_block *nb) |
| 1383 | { |
| 1384 | int rc; |
| 1385 | |
| 1386 | if (IS_ERR_OR_NULL(ctrl)) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | rc = blocking_notifier_chain_register(&ctrl->bdev->bus_change_notifier, |
| 1390 | nb); |
| 1391 | if (rc) |
| 1392 | pr_err("Notifier registration failed, rc=%d\n", rc); |
| 1393 | |
| 1394 | return rc; |
| 1395 | } |
| 1396 | EXPORT_SYMBOL(bif_ctrl_notifier_register); |
| 1397 | |
| 1398 | /** |
| 1399 | * bif_ctrl_notifier_unregister() - unregister a battery status change notifier |
| 1400 | * block that was previously registered |
| 1401 | * @ctrl: BIF controller consumer handle |
| 1402 | * |
| 1403 | * Returns 0 for success or errno if an error occurred. |
| 1404 | */ |
| 1405 | int bif_ctrl_notifier_unregister(struct bif_ctrl *ctrl, |
| 1406 | struct notifier_block *nb) |
| 1407 | { |
| 1408 | int rc; |
| 1409 | |
| 1410 | if (IS_ERR_OR_NULL(ctrl)) |
| 1411 | return -EINVAL; |
| 1412 | |
| 1413 | rc = |
| 1414 | blocking_notifier_chain_unregister(&ctrl->bdev->bus_change_notifier, |
| 1415 | nb); |
| 1416 | if (rc) |
| 1417 | pr_err("Notifier unregistration failed, rc=%d\n", rc); |
| 1418 | |
| 1419 | return rc; |
| 1420 | } |
| 1421 | EXPORT_SYMBOL(bif_ctrl_notifier_unregister); |
| 1422 | |
| 1423 | /** |
| 1424 | * bif_get_bus_handle() - returns the BIF controller consumer handle associated |
| 1425 | * with a BIF slave handle |
| 1426 | * @slave: BIF slave handle |
| 1427 | * |
| 1428 | * Note, bif_ctrl_put() should never be called for the pointer output by |
| 1429 | * bif_get_bus_handle(). |
| 1430 | */ |
| 1431 | struct bif_ctrl *bif_get_bus_handle(struct bif_slave *slave) |
| 1432 | { |
| 1433 | if (IS_ERR_OR_NULL(slave)) |
| 1434 | return ERR_PTR(-EINVAL); |
| 1435 | |
| 1436 | return &slave->ctrl; |
| 1437 | } |
| 1438 | EXPORT_SYMBOL(bif_get_bus_handle); |
| 1439 | |
| 1440 | /** |
| 1441 | * bif_ctrl_count() - returns the number of registered BIF controllers |
| 1442 | */ |
| 1443 | int bif_ctrl_count(void) |
| 1444 | { |
| 1445 | struct bif_ctrl_dev *bdev; |
| 1446 | int count = 0; |
| 1447 | |
| 1448 | mutex_lock(&bif_ctrl_list_mutex); |
| 1449 | |
| 1450 | list_for_each_entry(bdev, &bif_ctrl_list, list) { |
| 1451 | count++; |
| 1452 | } |
| 1453 | mutex_unlock(&bif_ctrl_list_mutex); |
| 1454 | |
| 1455 | return count; |
| 1456 | } |
| 1457 | EXPORT_SYMBOL(bif_ctrl_count); |
| 1458 | |
| 1459 | /** |
| 1460 | * bif_ctrl_get_by_id() - get a handle for the id'th BIF controller registered |
| 1461 | * in the system |
| 1462 | * @id: Arbitrary number associated with the BIF bus in the system |
| 1463 | * |
| 1464 | * id must be in the range [0, bif_ctrl_count() - 1]. This function should only |
| 1465 | * need to be called by a BIF consumer that is unable to link to a given BIF |
| 1466 | * controller via a device tree binding. |
| 1467 | * |
| 1468 | * Returns a BIF controller consumer handle if successful or an ERR_PTR if not. |
| 1469 | */ |
| 1470 | struct bif_ctrl *bif_ctrl_get_by_id(unsigned int id) |
| 1471 | { |
| 1472 | struct bif_ctrl_dev *bdev; |
| 1473 | struct bif_ctrl_dev *bdev_found = NULL; |
| 1474 | struct bif_ctrl *ctrl = ERR_PTR(-ENODEV); |
| 1475 | |
| 1476 | mutex_lock(&bif_ctrl_list_mutex); |
| 1477 | |
| 1478 | list_for_each_entry(bdev, &bif_ctrl_list, list) { |
| 1479 | if (id == 0) { |
| 1480 | bdev_found = bdev; |
| 1481 | break; |
| 1482 | } |
| 1483 | id--; |
| 1484 | } |
| 1485 | mutex_unlock(&bif_ctrl_list_mutex); |
| 1486 | |
| 1487 | if (bdev_found) { |
| 1488 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 1489 | if (!ctrl) { |
| 1490 | pr_err("Bus handle allocation failed\n"); |
| 1491 | ctrl = ERR_PTR(-ENOMEM); |
| 1492 | } else { |
| 1493 | ctrl->bdev = bdev_found; |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | return ctrl; |
| 1498 | } |
| 1499 | EXPORT_SYMBOL(bif_ctrl_get_by_id); |
| 1500 | |
| 1501 | /** |
| 1502 | * bif_ctrl_get() - get a handle for the BIF controller that is linked to the |
| 1503 | * consumer device in the device tree |
| 1504 | * @consumer_dev: Pointer to the consumer's device |
| 1505 | * |
| 1506 | * In order to use this function, the BIF consumer's device must specify the |
| 1507 | * "qcom,bif-ctrl" property in its device tree node which points to a BIF |
| 1508 | * controller device node. |
| 1509 | * |
| 1510 | * Returns a BIF controller consumer handle if successful or an ERR_PTR if not. |
| 1511 | * If the BIF controller linked to the consumer device has not yet probed, then |
| 1512 | * ERR_PTR(-EPROBE_DEFER) is returned. |
| 1513 | */ |
| 1514 | struct bif_ctrl *bif_ctrl_get(struct device *consumer_dev) |
| 1515 | { |
| 1516 | struct device_node *ctrl_node = NULL; |
| 1517 | struct bif_ctrl_dev *bdev_found = NULL; |
| 1518 | struct bif_ctrl *ctrl = ERR_PTR(-EPROBE_DEFER); |
| 1519 | struct bif_ctrl_dev *bdev = NULL; |
| 1520 | |
| 1521 | if (!consumer_dev || !consumer_dev->of_node) { |
| 1522 | pr_err("Invalid device node\n"); |
| 1523 | return ERR_PTR(-EINVAL); |
| 1524 | } |
| 1525 | |
| 1526 | ctrl_node = of_parse_phandle(consumer_dev->of_node, "qcom,bif-ctrl", 0); |
| 1527 | if (!ctrl_node) { |
| 1528 | pr_err("Could not find qcom,bif-ctrl property in %s\n", |
| 1529 | consumer_dev->of_node->full_name); |
| 1530 | return ERR_PTR(-ENXIO); |
| 1531 | } |
| 1532 | |
| 1533 | mutex_lock(&bif_ctrl_list_mutex); |
| 1534 | list_for_each_entry(bdev, &bif_ctrl_list, list) { |
| 1535 | if (bdev->ctrl_dev && bdev->ctrl_dev->of_node == ctrl_node) { |
| 1536 | bdev_found = bdev; |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | mutex_unlock(&bif_ctrl_list_mutex); |
| 1541 | |
| 1542 | if (bdev_found) { |
| 1543 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 1544 | if (!ctrl) { |
| 1545 | pr_err("Bus handle allocation failed\n"); |
| 1546 | ctrl = ERR_PTR(-ENOMEM); |
| 1547 | } else { |
| 1548 | ctrl->bdev = bdev_found; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | return ctrl; |
| 1553 | } |
| 1554 | EXPORT_SYMBOL(bif_ctrl_get); |
| 1555 | |
| 1556 | /** |
| 1557 | * bif_ctrl_put() - frees a BIF controller handle |
| 1558 | * @ctrl: BIF controller consumer handle |
| 1559 | */ |
| 1560 | void bif_ctrl_put(struct bif_ctrl *ctrl) |
| 1561 | { |
| 1562 | if (!IS_ERR_OR_NULL(ctrl) && ctrl->exclusive_lock) |
| 1563 | mutex_unlock(&ctrl->bdev->mutex); |
| 1564 | kfree(ctrl); |
| 1565 | } |
| 1566 | EXPORT_SYMBOL(bif_ctrl_put); |
| 1567 | |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1568 | static bool bif_slave_object_match(const struct bif_object *object, |
| 1569 | const struct bif_match_criteria *criteria) |
| 1570 | { |
| 1571 | return (object->type == criteria->obj_type) |
| 1572 | && (object->version == criteria->obj_version |
| 1573 | || !(criteria->match_mask & BIF_MATCH_OBJ_VERSION)) |
| 1574 | && (object->manufacturer_id == criteria->obj_manufacturer_id |
| 1575 | || !(criteria->match_mask & BIF_MATCH_OBJ_MANUFACTURER_ID)); |
| 1576 | } |
| 1577 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1578 | /* |
| 1579 | * Returns true if all parameters are matched, otherwise false. |
| 1580 | * function_type and function_version mean that their exists some function in |
| 1581 | * the slave which has the specified type and subtype. ctrl == NULL is treated |
| 1582 | * as a wildcard. |
| 1583 | */ |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1584 | static bool bif_slave_match(struct bif_ctrl *ctrl, |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1585 | struct bif_slave_dev *sdev, const struct bif_match_criteria *criteria) |
| 1586 | { |
| 1587 | int i, type, version; |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1588 | struct bif_object *object; |
| 1589 | bool function_found = false; |
| 1590 | bool object_found = false; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1591 | |
| 1592 | if (ctrl && (ctrl->bdev != sdev->bdev)) |
| 1593 | return false; |
| 1594 | |
| 1595 | if (!sdev->present |
| 1596 | && (!(criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE) |
| 1597 | || ((criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE) |
| 1598 | && !criteria->ignore_presence))) |
| 1599 | return false; |
| 1600 | |
| 1601 | if ((criteria->match_mask & BIF_MATCH_MANUFACTURER_ID) |
| 1602 | && sdev->l1_data.manufacturer_id != criteria->manufacturer_id) |
| 1603 | return false; |
| 1604 | |
| 1605 | if ((criteria->match_mask & BIF_MATCH_PRODUCT_ID) |
| 1606 | && sdev->l1_data.product_id != criteria->product_id) |
| 1607 | return false; |
| 1608 | |
| 1609 | if (criteria->match_mask & BIF_MATCH_FUNCTION_TYPE) { |
| 1610 | if (!sdev->function_directory) |
| 1611 | return false; |
| 1612 | for (i = 0; i < sdev->l1_data.length / 4; i++) { |
| 1613 | type = sdev->function_directory[i].function_type; |
| 1614 | version = sdev->function_directory[i].function_version; |
| 1615 | if (type == criteria->function_type && |
| 1616 | (version == criteria->function_version |
| 1617 | || !(criteria->match_mask |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1618 | & BIF_MATCH_FUNCTION_VERSION))) { |
| 1619 | function_found = true; |
| 1620 | break; |
| 1621 | } |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1622 | } |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1623 | if (!function_found) |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | if (criteria->match_mask & BIF_MATCH_OBJ_TYPE) { |
| 1628 | if (!sdev->nvm_function) |
| 1629 | return false; |
| 1630 | bif_ctrl_lock(ctrl); |
| 1631 | list_for_each_entry(object, &sdev->nvm_function->object_list, |
| 1632 | list) { |
| 1633 | if (bif_slave_object_match(object, criteria)) { |
| 1634 | object_found = true; |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | bif_ctrl_unlock(ctrl); |
| 1639 | if (!object_found) |
| 1640 | return false; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | return true; |
| 1644 | } |
| 1645 | |
| 1646 | /** |
| 1647 | * bif_slave_match_count() - returns the number of slaves associated with the |
| 1648 | * specified BIF controller which fit the matching |
| 1649 | * criteria |
| 1650 | * @ctrl: BIF controller consumer handle |
| 1651 | * @match_criteria: Matching criteria used to filter slaves |
| 1652 | */ |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1653 | int bif_slave_match_count(struct bif_ctrl *ctrl, |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1654 | const struct bif_match_criteria *match_criteria) |
| 1655 | { |
| 1656 | struct bif_slave_dev *sdev; |
| 1657 | int count = 0; |
| 1658 | |
| 1659 | mutex_lock(&bif_sdev_list_mutex); |
| 1660 | |
| 1661 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 1662 | if (bif_slave_match(ctrl, sdev, match_criteria)) |
| 1663 | count++; |
| 1664 | } |
| 1665 | |
| 1666 | mutex_unlock(&bif_sdev_list_mutex); |
| 1667 | |
| 1668 | return count; |
| 1669 | } |
| 1670 | EXPORT_SYMBOL(bif_slave_match_count); |
| 1671 | |
| 1672 | /** |
| 1673 | * bif_slave_match_get() - get a slave handle for the id'th slave associated |
| 1674 | * with the specified BIF controller which fits the |
| 1675 | * matching criteria |
| 1676 | * @ctrl: BIF controller consumer handle |
| 1677 | * @id: Index into the set of matching slaves |
| 1678 | * @match_criteria: Matching criteria used to filter slaves |
| 1679 | * |
| 1680 | * id must be in the range [0, bif_slave_match_count(ctrl, match_criteria) - 1]. |
| 1681 | * |
| 1682 | * Returns a BIF slave handle if successful or an ERR_PTR if not. |
| 1683 | */ |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1684 | struct bif_slave *bif_slave_match_get(struct bif_ctrl *ctrl, |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 1685 | unsigned int id, const struct bif_match_criteria *match_criteria) |
| 1686 | { |
| 1687 | struct bif_slave_dev *sdev; |
| 1688 | struct bif_slave *slave = ERR_PTR(-ENODEV); |
| 1689 | struct bif_slave_dev *sdev_found = NULL; |
| 1690 | int count = 0; |
| 1691 | |
| 1692 | mutex_lock(&bif_sdev_list_mutex); |
| 1693 | |
| 1694 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 1695 | if (bif_slave_match(ctrl, sdev, match_criteria)) |
| 1696 | count++; |
| 1697 | if (count == id + 1) { |
| 1698 | sdev_found = sdev; |
| 1699 | break; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | mutex_unlock(&bif_sdev_list_mutex); |
| 1704 | |
| 1705 | if (sdev_found) { |
| 1706 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
| 1707 | if (!slave) { |
| 1708 | pr_err("Slave allocation failed\n"); |
| 1709 | slave = ERR_PTR(-ENOMEM); |
| 1710 | } else { |
| 1711 | slave->sdev = sdev_found; |
| 1712 | slave->ctrl.bdev = sdev_found->bdev; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | return slave; |
| 1717 | } |
| 1718 | EXPORT_SYMBOL(bif_slave_match_get); |
| 1719 | |
| 1720 | /** |
| 1721 | * bif_slave_put() - frees a BIF slave handle |
| 1722 | * @slave: BIF slave handle |
| 1723 | */ |
| 1724 | void bif_slave_put(struct bif_slave *slave) |
| 1725 | { |
| 1726 | if (!IS_ERR_OR_NULL(slave) && slave->ctrl.exclusive_lock) |
| 1727 | mutex_unlock(&slave->sdev->bdev->mutex); |
| 1728 | kfree(slave); |
| 1729 | } |
| 1730 | EXPORT_SYMBOL(bif_slave_put); |
| 1731 | |
| 1732 | /** |
| 1733 | * bif_slave_find_function() - get the function pointer and version of a |
| 1734 | * BIF function if it is present on the specified slave |
| 1735 | * @slave: BIF slave handle |
| 1736 | * @function: BIF function to search for inside of the slave |
| 1737 | * @version: If the function is found, then 'version' is set to the |
| 1738 | * version value of the function |
| 1739 | * @function_pointer: If the function is found, then 'function_pointer' is set |
| 1740 | * to the BIF slave address of the function |
| 1741 | * |
| 1742 | * Returns 0 for success or errno if an error occurred. If the function is not |
| 1743 | * found in the slave, then -ENODEV is returned. |
| 1744 | */ |
| 1745 | int bif_slave_find_function(struct bif_slave *slave, u8 function, u8 *version, |
| 1746 | u16 *function_pointer) |
| 1747 | { |
| 1748 | int rc = -ENODEV; |
| 1749 | struct bif_ddb_l2_data *func; |
| 1750 | int i; |
| 1751 | |
| 1752 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(version) |
| 1753 | || IS_ERR_OR_NULL(function_pointer)) { |
| 1754 | pr_err("Invalid pointer input.\n"); |
| 1755 | return -EINVAL; |
| 1756 | } |
| 1757 | |
| 1758 | func = slave->sdev->function_directory; |
| 1759 | |
| 1760 | for (i = 0; i < slave->sdev->l1_data.length / 4; i++) { |
| 1761 | if (function == func[i].function_type) { |
| 1762 | *version = func[i].function_version; |
| 1763 | *function_pointer = func[i].function_pointer; |
| 1764 | rc = 0; |
| 1765 | break; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | return rc; |
| 1770 | } |
| 1771 | EXPORT_SYMBOL(bif_slave_find_function); |
| 1772 | |
David Collins | 387d5fb | 2013-09-23 11:51:16 -0700 | [diff] [blame] | 1773 | static bool bif_object_match(const struct bif_object *object, |
| 1774 | const struct bif_obj_match_criteria *criteria) |
| 1775 | { |
| 1776 | return (object->type == criteria->type |
| 1777 | || !(criteria->match_mask & BIF_OBJ_MATCH_TYPE)) |
| 1778 | && (object->version == criteria->version |
| 1779 | || !(criteria->match_mask & BIF_OBJ_MATCH_VERSION)) |
| 1780 | && (object->manufacturer_id == criteria->manufacturer_id |
| 1781 | || !(criteria->match_mask & BIF_OBJ_MATCH_MANUFACTURER_ID)); |
| 1782 | } |
| 1783 | |
| 1784 | /** |
| 1785 | * bif_object_match_count() - returns the number of objects associated with the |
| 1786 | * specified BIF slave which fit the matching criteria |
| 1787 | * @slave: BIF slave handle |
| 1788 | * @match_criteria: Matching criteria used to filter objects |
| 1789 | */ |
| 1790 | int bif_object_match_count(struct bif_slave *slave, |
| 1791 | const struct bif_obj_match_criteria *match_criteria) |
| 1792 | { |
| 1793 | struct bif_object *object; |
| 1794 | int count = 0; |
| 1795 | |
| 1796 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(match_criteria)) { |
| 1797 | pr_err("Invalid pointer input.\n"); |
| 1798 | return -EINVAL; |
| 1799 | } |
| 1800 | |
| 1801 | if (!slave->sdev->nvm_function) |
| 1802 | return 0; |
| 1803 | |
| 1804 | bif_slave_ctrl_lock(slave); |
| 1805 | list_for_each_entry(object, &slave->sdev->nvm_function->object_list, |
| 1806 | list) { |
| 1807 | if (bif_object_match(object, match_criteria)) |
| 1808 | count++; |
| 1809 | } |
| 1810 | bif_slave_ctrl_unlock(slave); |
| 1811 | |
| 1812 | return count; |
| 1813 | } |
| 1814 | EXPORT_SYMBOL(bif_object_match_count); |
| 1815 | |
| 1816 | /** |
| 1817 | * bif_object_match_get() - get a BIF object handle for the id'th object found |
| 1818 | * in the non-volatile memory of the specified BIF slave |
| 1819 | * which fits the matching criteria |
| 1820 | * @slave: BIF slave handle |
| 1821 | * @id: Index into the set of matching objects |
| 1822 | * @match_criteria: Matching criteria used to filter objects |
| 1823 | * |
| 1824 | * id must be in range [0, bif_object_match_count(slave, match_criteria) - 1]. |
| 1825 | * |
| 1826 | * Returns a BIF object handle if successful or an ERR_PTR if not. This handle |
| 1827 | * must be freed using bif_object_put() when it is no longer needed. |
| 1828 | */ |
| 1829 | struct bif_object *bif_object_match_get(struct bif_slave *slave, |
| 1830 | unsigned int id, const struct bif_obj_match_criteria *match_criteria) |
| 1831 | { |
| 1832 | struct bif_object *object; |
| 1833 | struct bif_object *object_found = NULL; |
| 1834 | struct bif_object *object_consumer = ERR_PTR(-ENODEV); |
| 1835 | int count = 0; |
| 1836 | |
| 1837 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(match_criteria)) { |
| 1838 | pr_err("Invalid pointer input.\n"); |
| 1839 | return ERR_PTR(-EINVAL); |
| 1840 | } |
| 1841 | |
| 1842 | if (!slave->sdev->nvm_function) |
| 1843 | return object_consumer; |
| 1844 | |
| 1845 | bif_slave_ctrl_lock(slave); |
| 1846 | list_for_each_entry(object, &slave->sdev->nvm_function->object_list, |
| 1847 | list) { |
| 1848 | if (bif_object_match(object, match_criteria)) |
| 1849 | count++; |
| 1850 | if (count == id + 1) { |
| 1851 | object_found = object; |
| 1852 | break; |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | if (object_found) { |
| 1857 | object_consumer = kmemdup(object_found, |
| 1858 | sizeof(*object_consumer), GFP_KERNEL); |
| 1859 | if (!object_consumer) { |
| 1860 | pr_err("out of memory\n"); |
| 1861 | object_consumer = ERR_PTR(-ENOMEM); |
| 1862 | goto done; |
| 1863 | } |
| 1864 | |
| 1865 | object_consumer->data = kmemdup(object_found->data, |
| 1866 | object_found->length - 8, GFP_KERNEL); |
| 1867 | if (!object_consumer->data) { |
| 1868 | pr_err("out of memory\n"); |
| 1869 | kfree(object_consumer); |
| 1870 | object_consumer = ERR_PTR(-ENOMEM); |
| 1871 | goto done; |
| 1872 | } |
| 1873 | |
| 1874 | /* |
| 1875 | * Use prev pointer in consumer struct to point to original |
| 1876 | * struct in the internal linked list. |
| 1877 | */ |
| 1878 | object_consumer->list.prev = &object_found->list; |
| 1879 | } |
| 1880 | |
| 1881 | done: |
| 1882 | bif_slave_ctrl_unlock(slave); |
| 1883 | |
| 1884 | return object_consumer; |
| 1885 | |
| 1886 | } |
| 1887 | EXPORT_SYMBOL(bif_object_match_get); |
| 1888 | |
| 1889 | /** |
| 1890 | * bif_object_put() - frees the memory allocated for a BIF object pointer |
| 1891 | * returned by bif_object_match_get() |
| 1892 | * @object: BIF object to free |
| 1893 | */ |
| 1894 | void bif_object_put(struct bif_object *object) |
| 1895 | { |
| 1896 | if (object) |
| 1897 | kfree(object->data); |
| 1898 | kfree(object); |
| 1899 | } |
| 1900 | EXPORT_SYMBOL(bif_object_put); |
| 1901 | |
David Collins | 2cf302f | 2013-09-23 17:33:13 -0700 | [diff] [blame] | 1902 | /* Copies the contents of object into buf following MIPI-BIF formatting. */ |
| 1903 | static void bif_object_flatten(u8 *buf, const struct bif_object *object) |
| 1904 | { |
| 1905 | buf[0] = object->type; |
| 1906 | buf[1] = object->version; |
| 1907 | buf[2] = object->manufacturer_id >> 8; |
| 1908 | buf[3] = object->manufacturer_id; |
| 1909 | buf[4] = object->length >> 8; |
| 1910 | buf[5] = object->length; |
| 1911 | memcpy(&buf[6], object->data, object->length - 8); |
| 1912 | buf[object->length - 2] = object->crc >> 8; |
| 1913 | buf[object->length - 1] = object->crc; |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * bif_object_write() - writes a new BIF object at the end of the object list in |
| 1918 | * the non-volatile memory of a slave |
| 1919 | * @slave: BIF slave handle |
| 1920 | * @type: Type of the object |
| 1921 | * @version: Version of the object |
| 1922 | * @manufacturer_id: Manufacturer ID number allocated by MIPI |
| 1923 | * @data: Data contained in the object |
| 1924 | * @data_len: Length of the data |
| 1925 | * |
| 1926 | * Returns 0 on success or errno on failure. This function will fail if the NVM |
| 1927 | * lock points to an offset after the BIF object list terminator (0x00). |
| 1928 | */ |
| 1929 | int bif_object_write(struct bif_slave *slave, u8 type, u8 version, |
| 1930 | u16 manufacturer_id, const u8 *data, int data_len) |
| 1931 | { |
| 1932 | struct bif_object *object; |
| 1933 | struct bif_object *tail_object; |
| 1934 | struct bif_nvm_function *nvm; |
| 1935 | int rc; |
| 1936 | int add_null = 0; |
| 1937 | u16 offset = 0; |
| 1938 | u8 *buf; |
| 1939 | |
| 1940 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(data)) { |
| 1941 | pr_err("Invalid input pointer\n"); |
| 1942 | return -EINVAL; |
| 1943 | } |
| 1944 | |
| 1945 | nvm = slave->sdev->nvm_function; |
| 1946 | if (!nvm) { |
| 1947 | pr_err("BIF slave has no NVM function\n"); |
| 1948 | return -ENODEV; |
| 1949 | } |
| 1950 | |
| 1951 | bif_slave_ctrl_lock(slave); |
| 1952 | if (nvm->object_count > 0) { |
| 1953 | tail_object = list_entry(nvm->object_list.prev, |
| 1954 | struct bif_object, list); |
| 1955 | offset = tail_object->addr - nvm->nvm_base_address |
| 1956 | + tail_object->length; |
| 1957 | } |
| 1958 | |
| 1959 | if (offset < nvm->nvm_lock_offset) { |
| 1960 | pr_err("Cannot write BIF object to NVM because the end of the object list is locked (end=%d < lock=%d)\n", |
| 1961 | offset, nvm->nvm_lock_offset); |
| 1962 | rc = -EPERM; |
| 1963 | goto error_unlock; |
| 1964 | } else if (offset + data_len + 8 > nvm->nvm_size) { |
| 1965 | pr_err("Cannot write BIF object to NVM because there is not enough remaining space (size=%d > remaining=%d)\n", |
| 1966 | data_len + 8, nvm->nvm_size - offset); |
| 1967 | rc = -EINVAL; |
| 1968 | goto error_unlock; |
| 1969 | } |
| 1970 | |
| 1971 | if (offset + data_len + 8 < nvm->nvm_size) |
| 1972 | add_null = 1; |
| 1973 | object = kzalloc(sizeof(*object), GFP_KERNEL); |
| 1974 | if (!object) { |
| 1975 | pr_err("kzalloc failed\n"); |
| 1976 | rc = -ENOMEM; |
| 1977 | goto error_unlock; |
| 1978 | } |
| 1979 | |
| 1980 | object->data = kzalloc(data_len, GFP_KERNEL); |
| 1981 | if (!object->data) { |
| 1982 | pr_err("kzalloc failed\n"); |
| 1983 | rc = -ENOMEM; |
| 1984 | goto free_object; |
| 1985 | } |
| 1986 | |
| 1987 | buf = kzalloc(data_len + 8 + add_null, GFP_KERNEL); |
| 1988 | if (!buf) { |
| 1989 | pr_err("kzalloc failed\n"); |
| 1990 | rc = -ENOMEM; |
| 1991 | goto free_data; |
| 1992 | } |
| 1993 | |
| 1994 | object->type = type; |
| 1995 | object->version = version; |
| 1996 | object->manufacturer_id = manufacturer_id; |
| 1997 | object->length = data_len + 8; |
| 1998 | memcpy(object->data, data, data_len); |
| 1999 | object->crc = bif_object_crc_ccitt(object); |
| 2000 | object->addr = offset + nvm->nvm_base_address; |
| 2001 | |
| 2002 | bif_object_flatten(buf, object); |
| 2003 | if (add_null) |
| 2004 | buf[object->length] = BIF_OBJ_END_OF_LIST; |
| 2005 | |
| 2006 | rc = _bif_slave_nvm_raw_write(slave->sdev, offset, buf, |
| 2007 | object->length + add_null); |
| 2008 | if (rc < 0) { |
| 2009 | pr_err("NVM write failed, rc=%d\n", rc); |
| 2010 | kfree(buf); |
| 2011 | goto free_data; |
| 2012 | } |
| 2013 | kfree(buf); |
| 2014 | |
| 2015 | list_add_tail(&object->list, &nvm->object_list); |
| 2016 | nvm->object_count++; |
| 2017 | |
| 2018 | bif_slave_ctrl_unlock(slave); |
| 2019 | |
| 2020 | return rc; |
| 2021 | |
| 2022 | free_data: |
| 2023 | kfree(object->data); |
| 2024 | free_object: |
| 2025 | kfree(object); |
| 2026 | error_unlock: |
| 2027 | bif_slave_ctrl_unlock(slave); |
| 2028 | |
| 2029 | return rc; |
| 2030 | |
| 2031 | } |
| 2032 | EXPORT_SYMBOL(bif_object_write); |
| 2033 | |
| 2034 | /* |
| 2035 | * Returns a pointer to the internal object referenced by a consumer object |
| 2036 | * if it exists. Returns NULL if the internal object cannot be found. |
| 2037 | */ |
| 2038 | static struct bif_object *bif_object_consumer_search( |
| 2039 | struct bif_nvm_function *nvm, const struct bif_object *consumer_object) |
| 2040 | { |
| 2041 | struct bif_object *object = NULL; |
| 2042 | struct bif_object *search_object; |
| 2043 | |
| 2044 | /* |
| 2045 | * Internal struct in object linked list is pointed to by consumer |
| 2046 | * object list.prev. |
| 2047 | */ |
| 2048 | search_object = list_entry(consumer_object->list.prev, |
| 2049 | struct bif_object, list); |
| 2050 | |
| 2051 | list_for_each_entry(object, &nvm->object_list, list) { |
| 2052 | if (object == search_object) |
| 2053 | break; |
| 2054 | } |
| 2055 | |
| 2056 | if (object != search_object) |
| 2057 | return NULL; |
| 2058 | |
| 2059 | return object; |
| 2060 | } |
| 2061 | |
| 2062 | /** |
| 2063 | * bif_object_overwrite() - overwrites an existing BIF object found in the |
| 2064 | * non-volatile memory of a slave |
| 2065 | * @slave: BIF slave handle |
| 2066 | * @object: Existing object in the slave to overwrite |
| 2067 | * @type: Type of the object |
| 2068 | * @version: Version of the object |
| 2069 | * @manufacturer_id: Manufacturer ID number allocated by MIPI |
| 2070 | * @data: Data contained in the object |
| 2071 | * @data_len: Length of the data |
| 2072 | * |
| 2073 | * Returns 0 on success or errno on failure. The data stored within 'object' |
| 2074 | * is updated to the new values upon success. The new data written to the |
| 2075 | * object must have exactly the same length as the old data (i.e. |
| 2076 | * data_len == object->length - 8). |
| 2077 | * |
| 2078 | * This function will fail if the NVM lock points to an offset after the |
| 2079 | * beginning of the existing BIF object. |
| 2080 | */ |
| 2081 | int bif_object_overwrite(struct bif_slave *slave, |
| 2082 | struct bif_object *object, u8 type, u8 version, |
| 2083 | u16 manufacturer_id, const u8 *data, int data_len) |
| 2084 | { |
| 2085 | struct bif_object *edit_object = NULL; |
| 2086 | struct bif_nvm_function *nvm; |
| 2087 | int rc; |
| 2088 | u16 crc; |
| 2089 | u8 *buf; |
| 2090 | |
| 2091 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(object) |
| 2092 | || IS_ERR_OR_NULL(data)) { |
| 2093 | pr_err("Invalid input pointer\n"); |
| 2094 | return -EINVAL; |
| 2095 | } |
| 2096 | |
| 2097 | nvm = slave->sdev->nvm_function; |
| 2098 | if (!nvm) { |
| 2099 | pr_err("BIF slave has no NVM function\n"); |
| 2100 | return -ENODEV; |
| 2101 | } |
| 2102 | |
| 2103 | if (data_len + 8 != object->length) { |
| 2104 | pr_err("New data length=%d is different from existing length=%d\n", |
| 2105 | data_len, object->length - 8); |
| 2106 | return -EINVAL; |
| 2107 | } |
| 2108 | |
| 2109 | bif_slave_ctrl_lock(slave); |
| 2110 | |
| 2111 | edit_object = bif_object_consumer_search(nvm, object); |
| 2112 | if (!edit_object) { |
| 2113 | pr_err("BIF object not found within slave\n"); |
| 2114 | rc = -EINVAL; |
| 2115 | goto error_unlock; |
| 2116 | } |
| 2117 | |
| 2118 | if (edit_object->addr - nvm->nvm_base_address < nvm->nvm_lock_offset) { |
| 2119 | pr_err("Cannot overwrite BIF object in NVM because some portion of it is locked\n"); |
| 2120 | rc = -EPERM; |
| 2121 | goto error_unlock; |
| 2122 | } |
| 2123 | |
| 2124 | buf = kzalloc(data_len + 8, GFP_KERNEL); |
| 2125 | if (!buf) { |
| 2126 | pr_err("kzalloc failed\n"); |
| 2127 | rc = -ENOMEM; |
| 2128 | goto error_unlock; |
| 2129 | } |
| 2130 | |
| 2131 | buf[0] = type; |
| 2132 | buf[1] = version; |
| 2133 | buf[2] = manufacturer_id >> 8; |
| 2134 | buf[3] = manufacturer_id; |
| 2135 | buf[4] = (data_len + 8) >> 8; |
| 2136 | buf[5] = data_len + 8; |
| 2137 | memcpy(&buf[6], data, data_len); |
| 2138 | crc = bif_crc_ccitt(buf, data_len + 6); |
| 2139 | buf[data_len + 6] = crc >> 8; |
| 2140 | buf[data_len + 7] = crc; |
| 2141 | |
| 2142 | rc = _bif_slave_nvm_raw_write(slave->sdev, |
| 2143 | object->addr - nvm->nvm_base_address, buf, data_len + 8); |
| 2144 | if (rc < 0) { |
| 2145 | pr_err("NVM write failed, rc=%d\n", rc); |
| 2146 | kfree(buf); |
| 2147 | goto error_unlock; |
| 2148 | } |
| 2149 | kfree(buf); |
| 2150 | |
| 2151 | /* Update internal object struct. */ |
| 2152 | edit_object->type = type; |
| 2153 | edit_object->version = version; |
| 2154 | edit_object->manufacturer_id = manufacturer_id; |
| 2155 | edit_object->length = data_len + 8; |
| 2156 | memcpy(edit_object->data, data, data_len); |
| 2157 | edit_object->crc = crc; |
| 2158 | |
| 2159 | /* Update consumer object struct. */ |
| 2160 | object->type = type; |
| 2161 | object->version = version; |
| 2162 | object->manufacturer_id = manufacturer_id; |
| 2163 | object->length = data_len + 8; |
| 2164 | memcpy(object->data, data, data_len); |
| 2165 | object->crc = crc; |
| 2166 | |
| 2167 | error_unlock: |
| 2168 | bif_slave_ctrl_unlock(slave); |
| 2169 | |
| 2170 | return rc; |
| 2171 | } |
| 2172 | EXPORT_SYMBOL(bif_object_overwrite); |
| 2173 | |
| 2174 | /** |
| 2175 | * bif_object_delete() - deletes an existing BIF object found in the |
| 2176 | * non-volatile memory of a slave. Objects found in the |
| 2177 | * object list in the NVM of the slave are shifted forward |
| 2178 | * in order to fill the hole left by the deleted object |
| 2179 | * @slave: BIF slave handle |
| 2180 | * @object: Existing object in the slave to delete |
| 2181 | * |
| 2182 | * Returns 0 on success or errno on failure. bif_object_put() must still be |
| 2183 | * called after this function in order to free the memory in the consumer |
| 2184 | * 'object' struct pointer. |
| 2185 | * |
| 2186 | * This function will fail if the NVM lock points to an offset after the |
| 2187 | * beginning of the existing BIF object. |
| 2188 | */ |
| 2189 | int bif_object_delete(struct bif_slave *slave, const struct bif_object *object) |
| 2190 | { |
| 2191 | struct bif_object *del_object = NULL; |
| 2192 | struct bif_object *tail_object; |
| 2193 | struct bif_nvm_function *nvm; |
| 2194 | bool found = false; |
| 2195 | int pos = 0; |
| 2196 | int rc; |
| 2197 | u8 *buf; |
| 2198 | |
| 2199 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(object)) { |
| 2200 | pr_err("Invalid input pointer\n"); |
| 2201 | return -EINVAL; |
| 2202 | } |
| 2203 | |
| 2204 | nvm = slave->sdev->nvm_function; |
| 2205 | if (!nvm) { |
| 2206 | pr_err("BIF slave has no NVM function\n"); |
| 2207 | return -ENODEV; |
| 2208 | } |
| 2209 | |
| 2210 | bif_slave_ctrl_lock(slave); |
| 2211 | |
| 2212 | del_object = bif_object_consumer_search(nvm, object); |
| 2213 | if (!del_object) { |
| 2214 | pr_err("BIF object not found within slave\n"); |
| 2215 | rc = -EINVAL; |
| 2216 | goto error_unlock; |
| 2217 | } |
| 2218 | |
| 2219 | if (del_object->addr - nvm->nvm_base_address < nvm->nvm_lock_offset) { |
| 2220 | pr_err("Cannot delete BIF object in NVM because some portion of it is locked\n"); |
| 2221 | rc = -EPERM; |
| 2222 | goto error_unlock; |
| 2223 | } |
| 2224 | |
| 2225 | buf = kmalloc(nvm->nvm_size, GFP_KERNEL); |
| 2226 | if (!buf) { |
| 2227 | pr_err("kzalloc failed\n"); |
| 2228 | rc = -ENOMEM; |
| 2229 | goto error_unlock; |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * Copy the contents of objects after the one to be deleted into a flat |
| 2234 | * array. |
| 2235 | */ |
| 2236 | list_for_each_entry(tail_object, &nvm->object_list, list) { |
| 2237 | if (found) { |
| 2238 | bif_object_flatten(&buf[pos], tail_object); |
| 2239 | pos += tail_object->length; |
| 2240 | } else if (tail_object == del_object) { |
| 2241 | found = true; |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | /* Add the list terminator. */ |
| 2246 | buf[pos++] = BIF_OBJ_END_OF_LIST; |
| 2247 | |
| 2248 | rc = _bif_slave_nvm_raw_write(slave->sdev, |
| 2249 | del_object->addr - nvm->nvm_base_address, buf, pos); |
| 2250 | if (rc < 0) { |
| 2251 | pr_err("NVM write failed, rc=%d\n", rc); |
| 2252 | kfree(buf); |
| 2253 | goto error_unlock; |
| 2254 | } |
| 2255 | kfree(buf); |
| 2256 | |
| 2257 | /* Update the addresses of the objects after the one to be deleted. */ |
| 2258 | found = false; |
| 2259 | list_for_each_entry(tail_object, &nvm->object_list, list) { |
| 2260 | if (found) |
| 2261 | tail_object->addr -= del_object->length; |
| 2262 | else if (tail_object == del_object) |
| 2263 | found = true; |
| 2264 | } |
| 2265 | |
| 2266 | list_del(&del_object->list); |
| 2267 | kfree(del_object->data); |
| 2268 | kfree(del_object); |
| 2269 | nvm->object_count--; |
| 2270 | |
| 2271 | error_unlock: |
| 2272 | bif_slave_ctrl_unlock(slave); |
| 2273 | |
| 2274 | return rc; |
| 2275 | } |
| 2276 | EXPORT_SYMBOL(bif_object_delete); |
| 2277 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 2278 | /** |
| 2279 | * bif_slave_read() - read contiguous memory values from a BIF slave |
| 2280 | * @slave: BIF slave handle |
| 2281 | * @addr: BIF slave address to begin reading at |
| 2282 | * @buf: Buffer to fill with memory values |
| 2283 | * @len: Number of byte to read |
| 2284 | * |
| 2285 | * Returns 0 for success or errno if an error occurred. |
| 2286 | */ |
| 2287 | int bif_slave_read(struct bif_slave *slave, u16 addr, u8 *buf, int len) |
| 2288 | { |
| 2289 | int rc; |
| 2290 | |
| 2291 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) { |
| 2292 | pr_err("Invalid pointer input.\n"); |
| 2293 | return -EINVAL; |
| 2294 | } |
| 2295 | |
| 2296 | bif_slave_ctrl_lock(slave); |
| 2297 | |
| 2298 | rc = _bif_slave_read(slave->sdev, addr, buf, len); |
| 2299 | if (rc) |
| 2300 | pr_err("BIF slave read failed, rc=%d\n", rc); |
| 2301 | |
| 2302 | bif_slave_ctrl_unlock(slave); |
| 2303 | |
| 2304 | return rc; |
| 2305 | } |
| 2306 | EXPORT_SYMBOL(bif_slave_read); |
| 2307 | |
| 2308 | /** |
| 2309 | * bif_slave_write() - write contiguous memory values to a BIF slave |
| 2310 | * @slave: BIF slave handle |
| 2311 | * @addr: BIF slave address to begin writing at |
| 2312 | * @buf: Buffer containing values to write |
| 2313 | * @len: Number of byte to write |
| 2314 | * |
| 2315 | * Returns 0 for success or errno if an error occurred. |
| 2316 | */ |
| 2317 | int bif_slave_write(struct bif_slave *slave, u16 addr, u8 *buf, int len) |
| 2318 | { |
| 2319 | int rc; |
| 2320 | |
| 2321 | if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) { |
| 2322 | pr_err("Invalid pointer input.\n"); |
| 2323 | return -EINVAL; |
| 2324 | } |
| 2325 | |
| 2326 | bif_slave_ctrl_lock(slave); |
| 2327 | |
| 2328 | rc = _bif_slave_write(slave->sdev, addr, buf, len); |
| 2329 | if (rc) |
| 2330 | pr_err("BIF slave write failed, rc=%d\n", rc); |
| 2331 | |
| 2332 | bif_slave_ctrl_unlock(slave); |
| 2333 | |
| 2334 | return rc; |
| 2335 | } |
| 2336 | EXPORT_SYMBOL(bif_slave_write); |
| 2337 | |
| 2338 | /** |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 2339 | * bif_slave_nvm_raw_read() - read contiguous memory values from a BIF slave's |
| 2340 | * non-volatile memory (NVM) |
| 2341 | * @slave: BIF slave handle |
| 2342 | * @offset: Offset from the beginning of BIF slave NVM to begin reading at |
| 2343 | * @buf: Buffer to fill with memory values |
| 2344 | * @len: Number of byte to read |
| 2345 | * |
| 2346 | * Returns 0 for success or errno if an error occurred. |
| 2347 | */ |
| 2348 | int bif_slave_nvm_raw_read(struct bif_slave *slave, u16 offset, u8 *buf, |
| 2349 | int len) |
| 2350 | { |
| 2351 | if (IS_ERR_OR_NULL(slave)) { |
| 2352 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
| 2353 | return -EINVAL; |
| 2354 | } else if (IS_ERR_OR_NULL(buf)) { |
| 2355 | pr_err("Invalid buffer pointer=%ld\n", PTR_ERR(buf)); |
| 2356 | return -EINVAL; |
| 2357 | } else if (!slave->sdev->nvm_function) { |
| 2358 | pr_err("BIF slave has no NVM function\n"); |
| 2359 | return -ENODEV; |
| 2360 | } else if (offset + len > slave->sdev->nvm_function->nvm_size) { |
| 2361 | pr_err("read offset + len = %d > NVM size = %d\n", |
| 2362 | offset + len, slave->sdev->nvm_function->nvm_size); |
| 2363 | return -EINVAL; |
| 2364 | } |
| 2365 | |
| 2366 | return bif_slave_read(slave, |
| 2367 | slave->sdev->nvm_function->nvm_base_address + offset, buf, len); |
| 2368 | } |
| 2369 | EXPORT_SYMBOL(bif_slave_nvm_raw_read); |
| 2370 | |
| 2371 | /** |
| 2372 | * bif_slave_nvm_raw_write() - write contiguous memory values to a BIF slave's |
| 2373 | * non-volatile memory (NVM) |
| 2374 | * @slave: BIF slave handle |
| 2375 | * @offset: Offset from the beginning of BIF slave NVM to begin writing at |
| 2376 | * @buf: Buffer containing values to write |
| 2377 | * @len: Number of byte to write |
| 2378 | * |
| 2379 | * Note that this function does *not* respect the MIPI-BIF object data |
| 2380 | * formatting specification. It can cause corruption of the object data list |
| 2381 | * stored in NVM if used improperly. |
| 2382 | * |
| 2383 | * Returns 0 for success or errno if an error occurred. |
| 2384 | */ |
| 2385 | int bif_slave_nvm_raw_write(struct bif_slave *slave, u16 offset, u8 *buf, |
| 2386 | int len) |
| 2387 | { |
| 2388 | int rc; |
| 2389 | |
| 2390 | if (IS_ERR_OR_NULL(slave)) { |
| 2391 | pr_err("Invalid slave pointer=%ld\n", PTR_ERR(slave)); |
| 2392 | return -EINVAL; |
| 2393 | } else if (IS_ERR_OR_NULL(buf)) { |
| 2394 | pr_err("Invalid buffer pointer=%ld\n", PTR_ERR(buf)); |
| 2395 | return -EINVAL; |
| 2396 | } |
| 2397 | |
| 2398 | bif_slave_ctrl_lock(slave); |
| 2399 | rc = _bif_slave_nvm_raw_write(slave->sdev, offset, buf, len); |
| 2400 | bif_slave_ctrl_unlock(slave); |
| 2401 | |
| 2402 | return rc; |
| 2403 | } |
| 2404 | EXPORT_SYMBOL(bif_slave_nvm_raw_write); |
| 2405 | |
| 2406 | /** |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 2407 | * bif_slave_is_present() - check if a slave is currently physically present |
| 2408 | * in the system |
| 2409 | * @slave: BIF slave handle |
| 2410 | * |
| 2411 | * Returns 1 if the slave is present, 0 if the slave is not present, or errno |
| 2412 | * if an error occurred. |
| 2413 | * |
| 2414 | * This function can be used by BIF consumer drivers to check if their slave |
| 2415 | * handles are still meaningful after battery reinsertion. |
| 2416 | */ |
| 2417 | int bif_slave_is_present(struct bif_slave *slave) |
| 2418 | { |
| 2419 | if (IS_ERR_OR_NULL(slave)) { |
| 2420 | pr_err("Invalid pointer input.\n"); |
| 2421 | return -EINVAL; |
| 2422 | } |
| 2423 | |
| 2424 | return slave->sdev->present; |
| 2425 | } |
| 2426 | EXPORT_SYMBOL(bif_slave_is_present); |
| 2427 | |
| 2428 | /** |
| 2429 | * bif_slave_is_selected() - check if a slave is currently selected on the BIF |
| 2430 | * bus |
| 2431 | * @slave: BIF slave handle |
| 2432 | * |
| 2433 | * Returns 1 if the slave is selected, 0 if the slave is not selected, or errno |
| 2434 | * if an error occurred. |
| 2435 | * |
| 2436 | * This function should not be required under normal circumstances since the |
| 2437 | * bif-core framework ensures that slaves are always selected when needed. |
| 2438 | * It would be most useful when used as a helper in conjunction with |
| 2439 | * bif_ctrl_bus_lock() and the raw transaction functions. |
| 2440 | */ |
| 2441 | int bif_slave_is_selected(struct bif_slave *slave) |
| 2442 | { |
| 2443 | int rc; |
| 2444 | |
| 2445 | if (IS_ERR_OR_NULL(slave)) { |
| 2446 | pr_err("Invalid pointer input.\n"); |
| 2447 | return -EINVAL; |
| 2448 | } |
| 2449 | |
| 2450 | if (slave->sdev->bdev->selected_sdev != slave->sdev) |
| 2451 | return false; |
| 2452 | |
| 2453 | bif_slave_ctrl_lock(slave); |
| 2454 | rc = bif_is_slave_selected(slave->sdev->bdev); |
| 2455 | bif_slave_ctrl_unlock(slave); |
| 2456 | |
| 2457 | return rc; |
| 2458 | } |
| 2459 | EXPORT_SYMBOL(bif_slave_is_selected); |
| 2460 | |
| 2461 | /** |
| 2462 | * bif_slave_select() - select a slave on the BIF bus |
| 2463 | * @slave: BIF slave handle |
| 2464 | * |
| 2465 | * Returns 0 on success or errno if an error occurred. |
| 2466 | * |
| 2467 | * This function should not be required under normal circumstances since the |
| 2468 | * bif-core framework ensures that slaves are always selected when needed. |
| 2469 | * It would be most useful when used as a helper in conjunction with |
| 2470 | * bif_ctrl_bus_lock() and the raw transaction functions. |
| 2471 | */ |
| 2472 | int bif_slave_select(struct bif_slave *slave) |
| 2473 | { |
| 2474 | int rc; |
| 2475 | |
| 2476 | if (IS_ERR_OR_NULL(slave)) { |
| 2477 | pr_err("Invalid pointer input.\n"); |
| 2478 | return -EINVAL; |
| 2479 | } |
| 2480 | |
| 2481 | bif_slave_ctrl_lock(slave); |
| 2482 | slave->sdev->bdev->selected_sdev = NULL; |
| 2483 | rc = bif_select_slave(slave->sdev); |
| 2484 | bif_slave_ctrl_unlock(slave); |
| 2485 | |
| 2486 | return rc; |
| 2487 | } |
| 2488 | EXPORT_SYMBOL(bif_slave_select); |
| 2489 | |
| 2490 | /** |
| 2491 | * bif_ctrl_raw_transaction() - perform a raw BIF transaction on the bus which |
| 2492 | * expects no slave response |
| 2493 | * @ctrl: BIF controller consumer handle |
| 2494 | * @transaction: BIF transaction to carry out. This should be one of the |
| 2495 | * values in enum bif_transaction. |
| 2496 | * @data: 8-bit data to use in the transaction. The meaning of |
| 2497 | * this data depends upon the transaction that is to be |
| 2498 | * performed. |
| 2499 | * |
| 2500 | * When performing a bus command (BC) transaction, values in enum |
| 2501 | * bif_bus_command may be used for the data parameter. Additional manufacturer |
| 2502 | * specific values may also be used in a BC transaction. |
| 2503 | * |
| 2504 | * Returns 0 on success or errno if an error occurred. |
| 2505 | * |
| 2506 | * This function should only need to be used when BIF transactions are required |
| 2507 | * that are not handled by the bif-core directly. |
| 2508 | */ |
| 2509 | int bif_ctrl_raw_transaction(struct bif_ctrl *ctrl, int transaction, u8 data) |
| 2510 | { |
| 2511 | int rc; |
| 2512 | |
| 2513 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2514 | pr_err("Invalid pointer input.\n"); |
| 2515 | return -EINVAL; |
| 2516 | } |
| 2517 | |
| 2518 | bif_ctrl_lock(ctrl); |
| 2519 | |
| 2520 | rc = ctrl->bdev->desc->ops->bus_transaction(ctrl->bdev, transaction, |
| 2521 | data); |
| 2522 | if (rc) |
| 2523 | pr_err("BIF bus transaction failed, rc=%d\n", rc); |
| 2524 | |
| 2525 | bif_ctrl_unlock(ctrl); |
| 2526 | |
| 2527 | return rc; |
| 2528 | } |
| 2529 | EXPORT_SYMBOL(bif_ctrl_raw_transaction); |
| 2530 | |
| 2531 | /** |
| 2532 | * bif_ctrl_raw_transaction_read() - perform a raw BIF transaction on the bus |
| 2533 | * which expects an RD or TACK slave response word |
| 2534 | * @ctrl: BIF controller consumer handle |
| 2535 | * @transaction: BIF transaction to carry out. This should be one of the |
| 2536 | * values in enum bif_transaction. |
| 2537 | * @data: 8-bit data to use in the transaction. The meaning of |
| 2538 | * this data depends upon the transaction that is to be |
| 2539 | * performed. |
| 2540 | * @response: Pointer to an integer which is filled with the 11-bit |
| 2541 | * slave response word upon success. The 11-bit format is |
| 2542 | * (MSB to LSB) BCF, ACK, EOT, D7-D0. |
| 2543 | * |
| 2544 | * When performing a bus command (BC) transaction, values in enum |
| 2545 | * bif_bus_command may be used for the data parameter. Additional manufacturer |
| 2546 | * specific values may also be used in a BC transaction. |
| 2547 | * |
| 2548 | * Returns 0 on success or errno if an error occurred. |
| 2549 | * |
| 2550 | * This function should only need to be used when BIF transactions are required |
| 2551 | * that are not handled by the bif-core directly. |
| 2552 | */ |
| 2553 | int bif_ctrl_raw_transaction_read(struct bif_ctrl *ctrl, int transaction, |
| 2554 | u8 data, int *response) |
| 2555 | { |
| 2556 | int rc; |
| 2557 | |
| 2558 | if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(response)) { |
| 2559 | pr_err("Invalid pointer input.\n"); |
| 2560 | return -EINVAL; |
| 2561 | } |
| 2562 | |
| 2563 | bif_ctrl_lock(ctrl); |
| 2564 | |
| 2565 | rc = ctrl->bdev->desc->ops->bus_transaction_read(ctrl->bdev, |
| 2566 | transaction, data, response); |
| 2567 | if (rc) |
| 2568 | pr_err("BIF bus transaction failed, rc=%d\n", rc); |
| 2569 | |
| 2570 | bif_ctrl_unlock(ctrl); |
| 2571 | |
| 2572 | return rc; |
| 2573 | } |
| 2574 | EXPORT_SYMBOL(bif_ctrl_raw_transaction_read); |
| 2575 | |
| 2576 | /** |
| 2577 | * bif_ctrl_raw_transaction_query() - perform a raw BIF transaction on the bus |
| 2578 | * which expects a BQ slave response |
| 2579 | * @ctrl: BIF controller consumer handle |
| 2580 | * @transaction: BIF transaction to carry out. This should be one of the |
| 2581 | * values in enum bif_transaction. |
| 2582 | * @data: 8-bit data to use in the transaction. The meaning of |
| 2583 | * this data depends upon the transaction that is to be |
| 2584 | * performed. |
| 2585 | * @query_response: Pointer to boolean which is set to true if a BQ pulse |
| 2586 | * is receieved, or false if no BQ pulse is received before |
| 2587 | * timing out. |
| 2588 | * |
| 2589 | * When performing a bus command (BC) transaction, values in enum |
| 2590 | * bif_bus_command may be used for the data parameter. Additional manufacturer |
| 2591 | * specific values may also be used in a BC transaction. |
| 2592 | * |
| 2593 | * Returns 0 on success or errno if an error occurred. |
| 2594 | * |
| 2595 | * This function should only need to be used when BIF transactions are required |
| 2596 | * that are not handled by the bif-core directly. |
| 2597 | */ |
| 2598 | int bif_ctrl_raw_transaction_query(struct bif_ctrl *ctrl, int transaction, |
| 2599 | u8 data, bool *query_response) |
| 2600 | { |
| 2601 | int rc; |
| 2602 | |
| 2603 | if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(query_response)) { |
| 2604 | pr_err("Invalid pointer input.\n"); |
| 2605 | return -EINVAL; |
| 2606 | } |
| 2607 | |
| 2608 | bif_ctrl_lock(ctrl); |
| 2609 | |
| 2610 | rc = ctrl->bdev->desc->ops->bus_transaction_query(ctrl->bdev, |
| 2611 | transaction, data, query_response); |
| 2612 | if (rc) |
| 2613 | pr_err("BIF bus transaction failed, rc=%d\n", rc); |
| 2614 | |
| 2615 | bif_ctrl_unlock(ctrl); |
| 2616 | |
| 2617 | return rc; |
| 2618 | } |
| 2619 | EXPORT_SYMBOL(bif_ctrl_raw_transaction_query); |
| 2620 | |
| 2621 | /** |
| 2622 | * bif_ctrl_bus_lock() - lock the BIF bus of a controller for exclusive access |
| 2623 | * @ctrl: BIF controller consumer handle |
| 2624 | * |
| 2625 | * This function should only need to be called in circumstances where a BIF |
| 2626 | * consumer is issuing special BIF bus commands that have strict ordering |
| 2627 | * requirements. |
| 2628 | */ |
| 2629 | void bif_ctrl_bus_lock(struct bif_ctrl *ctrl) |
| 2630 | { |
| 2631 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2632 | pr_err("Invalid controller handle.\n"); |
| 2633 | return; |
| 2634 | } |
| 2635 | |
| 2636 | if (ctrl->exclusive_lock) { |
| 2637 | pr_err("BIF bus exclusive lock already held\n"); |
| 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | mutex_lock(&ctrl->bdev->mutex); |
| 2642 | ctrl->exclusive_lock = true; |
| 2643 | bif_cancel_irq_mode_work(ctrl->bdev); |
| 2644 | } |
| 2645 | EXPORT_SYMBOL(bif_ctrl_bus_lock); |
| 2646 | |
| 2647 | /** |
| 2648 | * bif_ctrl_bus_unlock() - lock the BIF bus of a controller that was previously |
| 2649 | * locked for exclusive access |
| 2650 | * @ctrl: BIF controller consumer handle |
| 2651 | * |
| 2652 | * This function must only be called after first calling bif_ctrl_bus_lock(). |
| 2653 | */ |
| 2654 | void bif_ctrl_bus_unlock(struct bif_ctrl *ctrl) |
| 2655 | { |
| 2656 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2657 | pr_err("Invalid controller handle.\n"); |
| 2658 | return; |
| 2659 | } |
| 2660 | |
| 2661 | if (!ctrl->exclusive_lock) { |
| 2662 | pr_err("BIF bus exclusive lock not already held\n"); |
| 2663 | return; |
| 2664 | } |
| 2665 | |
| 2666 | ctrl->exclusive_lock = false; |
| 2667 | bif_schedule_irq_mode_work(ctrl->bdev); |
| 2668 | mutex_unlock(&ctrl->bdev->mutex); |
| 2669 | } |
| 2670 | EXPORT_SYMBOL(bif_ctrl_bus_unlock); |
| 2671 | |
| 2672 | /** |
| 2673 | * bif_ctrl_measure_rid() - measure the battery pack Rid pull-down resistance |
| 2674 | * in ohms |
| 2675 | * @ctrl: BIF controller consumer handle |
| 2676 | * |
| 2677 | * Returns the resistance of the Rid resistor in ohms if successful or errno |
| 2678 | * if an error occurred. |
| 2679 | */ |
| 2680 | int bif_ctrl_measure_rid(struct bif_ctrl *ctrl) |
| 2681 | { |
| 2682 | int rc; |
| 2683 | |
| 2684 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2685 | pr_err("Invalid controller handle.\n"); |
| 2686 | return -ENODEV; |
| 2687 | } |
| 2688 | |
| 2689 | if (!ctrl->bdev->desc->ops->get_battery_rid) { |
| 2690 | pr_err("Cannot measure Rid.\n"); |
| 2691 | return -ENXIO; |
| 2692 | } |
| 2693 | |
| 2694 | bif_ctrl_lock(ctrl); |
| 2695 | |
| 2696 | rc = ctrl->bdev->desc->ops->get_battery_rid(ctrl->bdev); |
| 2697 | if (rc < 0) |
| 2698 | pr_err("Error during Rid measurement, rc=%d\n", rc); |
| 2699 | |
| 2700 | bif_ctrl_unlock(ctrl); |
| 2701 | |
| 2702 | return rc; |
| 2703 | } |
| 2704 | EXPORT_SYMBOL(bif_ctrl_measure_rid); |
| 2705 | |
| 2706 | /** |
| 2707 | * bif_ctrl_get_bus_period() - get the BIF bus period (tau_bif) in nanoseconds |
| 2708 | * @ctrl: BIF controller consumer handle |
| 2709 | * |
| 2710 | * Returns the currently configured bus period in nanoseconds if successful or |
| 2711 | * errno if an error occurred. |
| 2712 | */ |
| 2713 | int bif_ctrl_get_bus_period(struct bif_ctrl *ctrl) |
| 2714 | { |
| 2715 | int rc; |
| 2716 | |
| 2717 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2718 | pr_err("Invalid controller handle.\n"); |
| 2719 | return -ENODEV; |
| 2720 | } |
| 2721 | |
| 2722 | if (!ctrl->bdev->desc->ops->get_bus_period) { |
| 2723 | pr_err("Cannot get the BIF bus period.\n"); |
| 2724 | return -ENXIO; |
| 2725 | } |
| 2726 | |
| 2727 | rc = ctrl->bdev->desc->ops->get_bus_period(ctrl->bdev); |
| 2728 | if (rc < 0) |
| 2729 | pr_err("Error during bus period retrieval, rc=%d\n", rc); |
| 2730 | |
| 2731 | return rc; |
| 2732 | } |
| 2733 | EXPORT_SYMBOL(bif_ctrl_get_bus_period); |
| 2734 | |
| 2735 | /** |
| 2736 | * bif_ctrl_set_bus_period() - set the BIF bus period (tau_bif) in nanoseconds |
| 2737 | * @ctrl: BIF controller consumer handle |
| 2738 | * @period_ns: BIF bus period in nanoseconds to use |
| 2739 | * |
| 2740 | * If the exact period is not supported by the BIF controller hardware, then the |
| 2741 | * next larger supported period will be used. |
| 2742 | * |
| 2743 | * Returns 0 on success or errno if an error occurred. |
| 2744 | */ |
| 2745 | int bif_ctrl_set_bus_period(struct bif_ctrl *ctrl, int period_ns) |
| 2746 | { |
| 2747 | int rc; |
| 2748 | |
| 2749 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2750 | pr_err("Invalid controller handle.\n"); |
| 2751 | return -ENODEV; |
| 2752 | } |
| 2753 | |
| 2754 | if (!ctrl->bdev->desc->ops->set_bus_period) { |
| 2755 | pr_err("Cannot set the BIF bus period.\n"); |
| 2756 | return -ENXIO; |
| 2757 | } |
| 2758 | |
| 2759 | bif_ctrl_lock(ctrl); |
| 2760 | rc = ctrl->bdev->desc->ops->set_bus_period(ctrl->bdev, period_ns); |
| 2761 | if (rc) |
| 2762 | pr_err("Error during bus period configuration, rc=%d\n", rc); |
| 2763 | bif_ctrl_unlock(ctrl); |
| 2764 | |
| 2765 | return rc; |
| 2766 | } |
| 2767 | EXPORT_SYMBOL(bif_ctrl_set_bus_period); |
| 2768 | |
| 2769 | /** |
| 2770 | * bif_ctrl_get_bus_state() - get the current state of the BIF bus |
| 2771 | * @ctrl: BIF controller consumer handle |
| 2772 | * |
| 2773 | * Returns a bus state from enum bif_bus_state if successful or errno if an |
| 2774 | * error occurred. |
| 2775 | */ |
| 2776 | int bif_ctrl_get_bus_state(struct bif_ctrl *ctrl) |
| 2777 | { |
| 2778 | int rc; |
| 2779 | |
| 2780 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2781 | pr_err("Invalid controller handle.\n"); |
| 2782 | return -ENODEV; |
| 2783 | } |
| 2784 | |
| 2785 | rc = ctrl->bdev->desc->ops->get_bus_state(ctrl->bdev); |
| 2786 | if (rc < 0) |
| 2787 | pr_err("Error during bus state retrieval, rc=%d\n", rc); |
| 2788 | |
| 2789 | return rc; |
| 2790 | } |
| 2791 | EXPORT_SYMBOL(bif_ctrl_get_bus_state); |
| 2792 | |
| 2793 | /** |
| 2794 | * bif_ctrl_set_bus_state() - set the state of the BIF bus |
| 2795 | * @ctrl: BIF controller consumer handle |
| 2796 | * @state: State for the BIF bus to enter |
| 2797 | * |
| 2798 | * Returns 0 on success or errno if an error occurred. |
| 2799 | */ |
| 2800 | int bif_ctrl_set_bus_state(struct bif_ctrl *ctrl, enum bif_bus_state state) |
| 2801 | { |
| 2802 | int rc; |
| 2803 | |
| 2804 | if (IS_ERR_OR_NULL(ctrl)) { |
| 2805 | pr_err("Invalid controller handle.\n"); |
| 2806 | return -ENODEV; |
| 2807 | } |
| 2808 | |
| 2809 | bif_ctrl_lock(ctrl); |
| 2810 | |
| 2811 | rc = ctrl->bdev->desc->ops->set_bus_state(ctrl->bdev, state); |
| 2812 | if (rc < 0) |
| 2813 | pr_err("Error during bus state configuration, rc=%d\n", rc); |
| 2814 | |
| 2815 | /* |
| 2816 | * Uncache the selected slave if the new bus state results in the slave |
| 2817 | * becoming unselected. |
| 2818 | */ |
| 2819 | if (state == BIF_BUS_STATE_MASTER_DISABLED |
| 2820 | || state == BIF_BUS_STATE_POWER_DOWN |
| 2821 | || state == BIF_BUS_STATE_STANDBY) |
| 2822 | ctrl->bdev->selected_sdev = NULL; |
| 2823 | |
| 2824 | bif_ctrl_unlock(ctrl); |
| 2825 | |
| 2826 | return rc; |
| 2827 | } |
| 2828 | EXPORT_SYMBOL(bif_ctrl_set_bus_state); |
| 2829 | |
| 2830 | /* |
| 2831 | * Check if the specified function is a protocol function and if it is, then |
| 2832 | * instantiate protocol function data for the slave. |
| 2833 | */ |
| 2834 | static int bif_initialize_protocol_function(struct bif_slave_dev *sdev, |
| 2835 | struct bif_ddb_l2_data *func) |
| 2836 | { |
| 2837 | int rc = 0; |
| 2838 | u8 buf[4]; |
| 2839 | |
| 2840 | /* Ensure that this is a protocol function. */ |
| 2841 | if (func->function_type != BIF_FUNC_PROTOCOL) |
| 2842 | return 0; |
| 2843 | |
| 2844 | if (sdev->protocol_function) { |
| 2845 | pr_err("Duplicate protocol function found for BIF slave; DEV_ADR=0x%02X\n", |
| 2846 | sdev->slave_addr); |
| 2847 | return -EPERM; |
| 2848 | } |
| 2849 | |
| 2850 | sdev->protocol_function = kzalloc(sizeof(struct bif_protocol_function), |
| 2851 | GFP_KERNEL); |
| 2852 | if (!sdev->protocol_function) { |
| 2853 | pr_err("out of memory\n"); |
| 2854 | return -ENOMEM; |
| 2855 | } |
| 2856 | |
| 2857 | rc = _bif_slave_read(sdev, func->function_pointer, buf, 4); |
| 2858 | if (rc) { |
| 2859 | pr_err("Protocol function data read failed, rc=%d\n", rc); |
| 2860 | return rc; |
| 2861 | } |
| 2862 | |
| 2863 | sdev->protocol_function->protocol_pointer = buf[0] << 8 | buf[1]; |
| 2864 | sdev->protocol_function->device_id_pointer = buf[2] << 8 | buf[3]; |
| 2865 | sdev->protocol_function->l2_entry = func; |
| 2866 | |
| 2867 | rc = _bif_slave_read(sdev, sdev->protocol_function->device_id_pointer, |
| 2868 | sdev->protocol_function->device_id, BIF_DEVICE_ID_BYTE_LENGTH); |
| 2869 | if (rc) { |
| 2870 | pr_err("Device ID read failed, rc=%d\n", rc); |
| 2871 | return rc; |
| 2872 | } |
| 2873 | |
| 2874 | /* Check if this slave does not have a UID value stored. */ |
| 2875 | if (sdev->unique_id_bits_known == 0) { |
| 2876 | sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH; |
| 2877 | /* Fill in UID using manufacturer ID and device ID. */ |
| 2878 | sdev->unique_id[0] = sdev->l1_data.manufacturer_id >> 8; |
| 2879 | sdev->unique_id[1] = sdev->l1_data.manufacturer_id; |
| 2880 | memcpy(&sdev->unique_id[2], |
| 2881 | sdev->protocol_function->device_id, |
| 2882 | BIF_DEVICE_ID_BYTE_LENGTH); |
| 2883 | } |
| 2884 | |
| 2885 | return rc; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * Check if the specified function is a slave control function and if it is, |
| 2890 | * then instantiate slave control function data for the slave. |
| 2891 | */ |
| 2892 | static int bif_initialize_slave_control_function(struct bif_slave_dev *sdev, |
| 2893 | struct bif_ddb_l2_data *func) |
| 2894 | { |
| 2895 | int rc = 0; |
| 2896 | int i; |
| 2897 | u8 buf[3]; |
| 2898 | |
| 2899 | /* Ensure that this is a slave control function. */ |
| 2900 | if (func->function_type != BIF_FUNC_SLAVE_CONTROL) |
| 2901 | return 0; |
| 2902 | |
| 2903 | if (sdev->slave_ctrl_function) { |
| 2904 | pr_err("Duplicate slave control function found for BIF slave; DEV_ADR=0x%02X\n", |
| 2905 | sdev->slave_addr); |
| 2906 | return -EPERM; |
| 2907 | } |
| 2908 | |
| 2909 | sdev->slave_ctrl_function |
| 2910 | = kzalloc(sizeof(struct bif_protocol_function), GFP_KERNEL); |
| 2911 | if (!sdev->slave_ctrl_function) { |
| 2912 | pr_err("out of memory\n"); |
| 2913 | return -ENOMEM; |
| 2914 | } |
| 2915 | |
| 2916 | rc = _bif_slave_read(sdev, func->function_pointer, buf, 3); |
| 2917 | if (rc) { |
| 2918 | pr_err("Slave control function data read failed, rc=%d\n", rc); |
| 2919 | return rc; |
| 2920 | } |
| 2921 | |
| 2922 | sdev->slave_ctrl_function->slave_ctrl_pointer = buf[0] << 8 | buf[1]; |
| 2923 | sdev->slave_ctrl_function->task_count |
| 2924 | = buf[2] * SLAVE_CTRL_TASKS_PER_SET; |
| 2925 | sdev->slave_ctrl_function->l2_entry = func; |
| 2926 | |
| 2927 | if (sdev->slave_ctrl_function->task_count > 0) { |
| 2928 | sdev->slave_ctrl_function->irq_notifier_list = |
| 2929 | kzalloc(sizeof(struct blocking_notifier_head) |
| 2930 | * sdev->slave_ctrl_function->task_count, |
| 2931 | GFP_KERNEL); |
| 2932 | if (!sdev->slave_ctrl_function->irq_notifier_list) { |
| 2933 | pr_err("out of memory\n"); |
| 2934 | kfree(sdev->slave_ctrl_function); |
| 2935 | return -ENOMEM; |
| 2936 | } |
| 2937 | |
| 2938 | for (i = 0; i < sdev->slave_ctrl_function->task_count; i++) { |
| 2939 | BLOCKING_INIT_NOTIFIER_HEAD( |
| 2940 | &sdev->slave_ctrl_function->irq_notifier_list[i]); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | return rc; |
| 2945 | } |
| 2946 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 2947 | /* |
| 2948 | * Check if the specified function is an NVM function and if it is, then |
| 2949 | * instantiate NVM function data for the slave and read all objects. |
| 2950 | */ |
| 2951 | static int bif_initialize_nvm_function(struct bif_slave_dev *sdev, |
| 2952 | struct bif_ddb_l2_data *func) |
| 2953 | { |
| 2954 | int rc = 0; |
David Collins | 38b45e6 | 2013-09-18 16:09:47 -0700 | [diff] [blame] | 2955 | int data_len, read_size; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 2956 | u8 buf[8], object_type; |
| 2957 | struct bif_object *object; |
| 2958 | struct bif_object *temp; |
| 2959 | u16 addr; |
| 2960 | u16 crc; |
| 2961 | |
| 2962 | /* Ensure that this is an NVM function. */ |
| 2963 | if (func->function_type != BIF_FUNC_NVM) |
| 2964 | return 0; |
| 2965 | |
| 2966 | if (sdev->nvm_function) { |
| 2967 | pr_err("Duplicate NVM function found for BIF slave; DEV_ADR=0x%02X\n", |
| 2968 | sdev->slave_addr); |
| 2969 | return -EPERM; |
| 2970 | } |
| 2971 | |
| 2972 | sdev->nvm_function |
| 2973 | = kzalloc(sizeof(*sdev->nvm_function), GFP_KERNEL); |
| 2974 | if (!sdev->nvm_function) { |
| 2975 | pr_err("out of memory\n"); |
| 2976 | return -ENOMEM; |
| 2977 | } |
| 2978 | |
| 2979 | rc = _bif_slave_read(sdev, func->function_pointer, buf, 8); |
| 2980 | if (rc) { |
| 2981 | pr_err("NVM function data read failed, rc=%d\n", rc); |
| 2982 | return rc; |
| 2983 | } |
| 2984 | |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 2985 | sdev->nvm_function->nvm_pointer = buf[0] << 8 | buf[1]; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 2986 | sdev->nvm_function->slave_control_channel = buf[2]; |
| 2987 | sdev->nvm_function->write_buffer_size = buf[3]; |
| 2988 | sdev->nvm_function->nvm_base_address = buf[4] << 8 | buf[5]; |
| 2989 | sdev->nvm_function->nvm_size = buf[6] << 8 | buf[7]; |
| 2990 | |
David Collins | 95738fe | 2013-09-18 17:17:08 -0700 | [diff] [blame] | 2991 | /* Read NVM lock offset */ |
| 2992 | rc = _bif_slave_read(sdev, sdev->nvm_function->nvm_pointer, buf, 2); |
| 2993 | if (rc) { |
| 2994 | pr_err("Slave memory read failed, rc=%d\n", rc); |
| 2995 | return rc; |
| 2996 | } |
| 2997 | |
| 2998 | sdev->nvm_function->nvm_lock_offset = buf[0] << 8 | buf[1]; |
| 2999 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3000 | INIT_LIST_HEAD(&sdev->nvm_function->object_list); |
| 3001 | |
| 3002 | /* Read object list */ |
| 3003 | addr = sdev->nvm_function->nvm_base_address; |
| 3004 | rc = _bif_slave_read(sdev, addr, &object_type, 1); |
| 3005 | if (rc) { |
| 3006 | pr_err("Slave memory read failed, rc=%d\n", rc); |
| 3007 | return rc; |
| 3008 | } |
| 3009 | |
David Collins | 38b45e6 | 2013-09-18 16:09:47 -0700 | [diff] [blame] | 3010 | while (object_type != BIF_OBJ_END_OF_LIST) { |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3011 | object = kzalloc(sizeof(*object), GFP_KERNEL); |
| 3012 | if (!object) { |
| 3013 | pr_err("out of memory\n"); |
| 3014 | rc = -ENOMEM; |
| 3015 | goto free_data; |
| 3016 | } |
| 3017 | list_add_tail(&object->list, &sdev->nvm_function->object_list); |
| 3018 | |
| 3019 | rc = _bif_slave_read(sdev, addr + 1, buf + 1, 5); |
| 3020 | if (rc) { |
| 3021 | pr_err("Slave memory read of object header failed; addr=0x%04X, len=%d, rc=%d\n", |
| 3022 | addr + 1, 5, rc); |
| 3023 | goto free_data; |
| 3024 | } |
| 3025 | |
| 3026 | object->addr = addr; |
| 3027 | object->type = object_type; |
| 3028 | object->version = buf[1]; |
| 3029 | object->manufacturer_id = buf[2] << 8 | buf[3]; |
| 3030 | object->length = buf[4] << 8 | buf[5]; |
| 3031 | |
| 3032 | if ((object->addr + object->length) |
| 3033 | > (sdev->nvm_function->nvm_base_address |
| 3034 | + sdev->nvm_function->nvm_size)) { |
| 3035 | pr_warn("warning: BIF slave object is not formatted correctly; NVM base=0x%04X, NVM len=%d, object addr=0x%04X, object len=%d\n", |
| 3036 | sdev->nvm_function->nvm_base_address, |
| 3037 | sdev->nvm_function->nvm_size, |
| 3038 | object->addr, |
| 3039 | object->length); |
| 3040 | /* Limit object size to remaining NVM size. */ |
| 3041 | object->length = sdev->nvm_function->nvm_size |
| 3042 | + sdev->nvm_function->nvm_base_address |
| 3043 | - object->addr; |
| 3044 | } |
| 3045 | |
| 3046 | /* Object header + CRC takes up 8 bytes. */ |
| 3047 | data_len = object->length - 8; |
| 3048 | object->data = kmalloc(data_len, GFP_KERNEL); |
| 3049 | if (!object->data) { |
| 3050 | pr_err("out of memory\n"); |
| 3051 | rc = -ENOMEM; |
| 3052 | goto free_data; |
| 3053 | } |
| 3054 | |
| 3055 | rc = _bif_slave_read(sdev, addr + 6, object->data, data_len); |
| 3056 | if (rc) { |
| 3057 | pr_err("Slave memory read of object data failed; addr=0x%04X, len=%d, rc=%d\n", |
| 3058 | addr + 6, data_len, rc); |
| 3059 | goto free_data; |
| 3060 | } |
| 3061 | |
David Collins | 38b45e6 | 2013-09-18 16:09:47 -0700 | [diff] [blame] | 3062 | if ((object->length + addr) >= (sdev->nvm_function->nvm_size |
| 3063 | + sdev->nvm_function->nvm_base_address)) |
| 3064 | read_size = 2; |
| 3065 | else |
| 3066 | read_size = 3; |
| 3067 | rc = _bif_slave_read(sdev, addr + 6 + data_len, buf, read_size); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3068 | if (rc) { |
| 3069 | pr_err("Slave memory read of object CRC failed; addr=0x%04X, len=%d, rc=%d\n", |
David Collins | 38b45e6 | 2013-09-18 16:09:47 -0700 | [diff] [blame] | 3070 | addr + 6 + data_len, read_size, rc); |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3071 | goto free_data; |
| 3072 | } |
| 3073 | |
| 3074 | object->crc = buf[0] << 8 | buf[1]; |
David Collins | 38b45e6 | 2013-09-18 16:09:47 -0700 | [diff] [blame] | 3075 | object_type = (read_size == 3) ? buf[2] : BIF_OBJ_END_OF_LIST; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3076 | sdev->nvm_function->object_count++; |
| 3077 | |
| 3078 | crc = bif_object_crc_ccitt(object); |
| 3079 | if (crc != object->crc) |
| 3080 | pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n", |
| 3081 | object->addr, crc, object->crc); |
| 3082 | |
| 3083 | addr += object->length; |
| 3084 | } |
| 3085 | |
| 3086 | return rc; |
| 3087 | |
| 3088 | free_data: |
| 3089 | list_for_each_entry_safe(object, temp, |
| 3090 | &sdev->nvm_function->object_list, list) { |
| 3091 | list_del(&object->list); |
| 3092 | kfree(object->data); |
| 3093 | kfree(object); |
| 3094 | } |
| 3095 | kfree(sdev->nvm_function); |
| 3096 | sdev->nvm_function = NULL; |
| 3097 | return rc; |
| 3098 | } |
| 3099 | |
| 3100 | static int bif_parse_slave_data(struct bif_slave_dev *sdev) |
| 3101 | { |
| 3102 | int rc = 0; |
| 3103 | u8 buf[10]; |
| 3104 | u8 *func_buf; |
| 3105 | struct bif_ddb_l2_data *func; |
| 3106 | int function_count, i; |
| 3107 | |
| 3108 | rc = _bif_slave_read(sdev, BIF_DDB_L1_BASE_ADDR, buf, 10); |
| 3109 | if (rc) { |
| 3110 | pr_err("DDB L1 data read failed, rc=%d\n", rc); |
| 3111 | return rc; |
| 3112 | } |
| 3113 | |
| 3114 | sdev->l1_data.revision = buf[0]; |
| 3115 | sdev->l1_data.level = buf[1]; |
| 3116 | sdev->l1_data.device_class = buf[2] << 8 | buf[3]; |
| 3117 | sdev->l1_data.manufacturer_id = buf[4] << 8 | buf[5]; |
| 3118 | sdev->l1_data.product_id = buf[6] << 8 | buf[7]; |
| 3119 | sdev->l1_data.length = buf[8] << 8 | buf[9]; |
| 3120 | |
| 3121 | function_count = sdev->l1_data.length / 4; |
| 3122 | if (sdev->l1_data.length % 4) { |
| 3123 | pr_err("Function directory length=%d is invalid\n", |
| 3124 | sdev->l1_data.length); |
| 3125 | return -EPROTO; |
| 3126 | } |
| 3127 | |
| 3128 | /* No DDB L2 function directory */ |
| 3129 | if (function_count == 0) |
| 3130 | return 0; |
| 3131 | |
| 3132 | func_buf = kmalloc(sdev->l1_data.length, GFP_KERNEL); |
| 3133 | if (!func_buf) { |
| 3134 | pr_err("out of memory\n"); |
| 3135 | return -ENOMEM; |
| 3136 | } |
| 3137 | |
| 3138 | sdev->function_directory = kzalloc( |
| 3139 | function_count * sizeof(struct bif_ddb_l2_data), GFP_KERNEL); |
| 3140 | if (!sdev->function_directory) { |
| 3141 | pr_err("out of memory\n"); |
| 3142 | return -ENOMEM; |
| 3143 | } |
| 3144 | |
| 3145 | rc = _bif_slave_read(sdev, BIF_DDB_L2_BASE_ADDR, func_buf, |
| 3146 | sdev->l1_data.length); |
| 3147 | if (rc) { |
| 3148 | pr_err("DDB L2 data read failed, rc=%d\n", rc); |
| 3149 | return rc; |
| 3150 | } |
| 3151 | |
| 3152 | for (i = 0; i < function_count; i++) { |
| 3153 | func = &sdev->function_directory[i]; |
| 3154 | func->function_type = func_buf[i * 4]; |
| 3155 | func->function_version = func_buf[i * 4 + 1]; |
| 3156 | func->function_pointer = func_buf[i * 4 + 2] << 8 |
| 3157 | | func_buf[i * 4 + 3]; |
| 3158 | rc = bif_initialize_protocol_function(sdev, func); |
| 3159 | if (rc) |
| 3160 | goto done; |
| 3161 | rc = bif_initialize_slave_control_function(sdev, func); |
| 3162 | if (rc) |
| 3163 | goto done; |
| 3164 | rc = bif_initialize_nvm_function(sdev, func); |
| 3165 | if (rc) |
| 3166 | goto done; |
| 3167 | } |
| 3168 | done: |
| 3169 | kfree(func_buf); |
| 3170 | return rc; |
| 3171 | } |
| 3172 | |
| 3173 | static int bif_add_secondary_slaves(struct bif_slave_dev *primary_slave) |
| 3174 | { |
| 3175 | int rc = 0; |
| 3176 | int data_len, i; |
| 3177 | u16 crc; |
| 3178 | struct bif_slave_dev *sdev; |
| 3179 | struct bif_object *object; |
| 3180 | |
| 3181 | list_for_each_entry(object, &primary_slave->nvm_function->object_list, |
| 3182 | list) { |
| 3183 | if (object->type != BIF_OBJ_SEC_SLAVE) |
| 3184 | continue; |
| 3185 | |
| 3186 | data_len = object->length - 8; |
| 3187 | if (data_len % BIF_UNIQUE_ID_BYTE_LENGTH) { |
| 3188 | pr_info("Invalid secondary slave object found, addr=0x%04X, data len=%d\n", |
| 3189 | object->addr, data_len); |
| 3190 | continue; |
| 3191 | } |
| 3192 | |
| 3193 | crc = bif_object_crc_ccitt(object); |
| 3194 | if (crc != object->crc) { |
| 3195 | pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n", |
| 3196 | object->addr, crc, object->crc); |
| 3197 | continue; |
| 3198 | } |
| 3199 | |
| 3200 | for (i = 0; i < data_len / BIF_UNIQUE_ID_BYTE_LENGTH; i++) { |
| 3201 | sdev = bif_add_slave(primary_slave->bdev); |
| 3202 | if (IS_ERR(sdev)) { |
| 3203 | rc = PTR_ERR(sdev); |
| 3204 | pr_err("bif_add_slave failed, rc=%d\n", rc); |
| 3205 | return rc; |
| 3206 | } |
| 3207 | memcpy(sdev->unique_id, |
| 3208 | &object->data[i * BIF_UNIQUE_ID_BYTE_LENGTH], |
| 3209 | BIF_UNIQUE_ID_BYTE_LENGTH); |
| 3210 | sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH; |
| 3211 | |
| 3212 | rc = bif_select_slave(sdev); |
| 3213 | if (rc) { |
| 3214 | pr_err("Could not select slave, rc=%d\n", rc); |
| 3215 | goto free_slave; |
| 3216 | } |
| 3217 | |
| 3218 | rc = bif_is_slave_selected(sdev->bdev); |
| 3219 | if (rc < 0) { |
| 3220 | pr_err("Transaction failed, rc=%d\n", rc); |
| 3221 | goto free_slave; |
| 3222 | } else if (rc == 1) { |
| 3223 | sdev->present = true; |
| 3224 | sdev->bdev->selected_sdev = sdev; |
David Collins | 5a90ae9 | 2013-05-02 12:19:40 -0700 | [diff] [blame] | 3225 | rc = bif_parse_slave_data(sdev); |
| 3226 | if (rc) { |
| 3227 | pr_err("Failed to parse secondary slave data, rc=%d\n", |
| 3228 | rc); |
| 3229 | goto free_slave; |
| 3230 | } |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3231 | } else { |
| 3232 | sdev->present = false; |
| 3233 | sdev->bdev->selected_sdev = NULL; |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | return rc; |
| 3239 | |
| 3240 | free_slave: |
| 3241 | bif_remove_slave(sdev); |
| 3242 | return rc; |
| 3243 | } |
| 3244 | |
| 3245 | /* |
| 3246 | * Performs UID search to identify all slaves attached to the bus. Assumes that |
| 3247 | * all necessary locks are held. |
| 3248 | */ |
| 3249 | static int bif_perform_uid_search(struct bif_ctrl_dev *bdev) |
| 3250 | { |
| 3251 | struct bif_slave_dev *sdev; |
| 3252 | struct bif_slave_dev *new_slave; |
| 3253 | bool resp[2], resp_dilc; |
| 3254 | int i; |
| 3255 | int rc = 0; |
| 3256 | u8 cmd_probe[2] = {BIF_CMD_DIP0, BIF_CMD_DIP1}; |
| 3257 | u8 cmd_enter[2] = {BIF_CMD_DIE0, BIF_CMD_DIE1}; |
| 3258 | |
| 3259 | /* |
| 3260 | * Iterate over all partially known UIDs adding new ones as they are |
| 3261 | * found. |
| 3262 | */ |
| 3263 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 3264 | /* Skip slaves with fully known UIDs. */ |
| 3265 | if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH |
| 3266 | || sdev->bdev != bdev) |
| 3267 | continue; |
| 3268 | |
| 3269 | /* Begin a new UID search. */ |
| 3270 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_BC, |
| 3271 | BIF_CMD_DISS); |
| 3272 | if (rc) { |
| 3273 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3274 | return rc; |
| 3275 | } |
| 3276 | |
| 3277 | /* Step through all known UID bits (MSB to LSB). */ |
| 3278 | for (i = 0; i < sdev->unique_id_bits_known; i++) { |
| 3279 | rc = bdev->desc->ops->bus_transaction(bdev, |
| 3280 | BIF_TRANS_BC, |
| 3281 | cmd_enter[get_uid_bit(sdev->unique_id, i)]); |
| 3282 | if (rc) { |
| 3283 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3284 | return rc; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | /* Step through unknown UID bits. */ |
| 3289 | for (i = sdev->unique_id_bits_known; |
| 3290 | i < BIF_UNIQUE_ID_BIT_LENGTH; i++) { |
| 3291 | rc = bdev->desc->ops->bus_transaction_query(bdev, |
| 3292 | BIF_TRANS_BC, cmd_probe[0], &resp[0]); |
| 3293 | if (rc) { |
| 3294 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3295 | return rc; |
| 3296 | } |
| 3297 | |
| 3298 | rc = bdev->desc->ops->bus_transaction_query(bdev, |
| 3299 | BIF_TRANS_BC, cmd_probe[1], &resp[1]); |
| 3300 | if (rc) { |
| 3301 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3302 | return rc; |
| 3303 | } |
| 3304 | |
| 3305 | if (resp[0] && resp[1]) { |
| 3306 | /* Create an entry for the new UID branch. */ |
| 3307 | new_slave = bif_add_slave(bdev); |
| 3308 | if (IS_ERR(new_slave)) { |
| 3309 | rc = PTR_ERR(sdev); |
| 3310 | pr_err("bif_add_slave failed, rc=%d\n", |
| 3311 | rc); |
| 3312 | return rc; |
| 3313 | } |
| 3314 | memcpy(new_slave->unique_id, sdev->unique_id, |
| 3315 | BIF_UNIQUE_ID_BYTE_LENGTH); |
| 3316 | new_slave->bdev = sdev->bdev; |
| 3317 | |
| 3318 | set_uid_bit(sdev->unique_id, i, 0); |
| 3319 | sdev->unique_id_bits_known = i + 1; |
| 3320 | |
| 3321 | set_uid_bit(new_slave->unique_id, i, 1); |
| 3322 | new_slave->unique_id_bits_known = i + 1; |
| 3323 | } else if (resp[0]) { |
| 3324 | set_uid_bit(sdev->unique_id, i, 0); |
| 3325 | sdev->unique_id_bits_known = i + 1; |
| 3326 | } else if (resp[1]) { |
| 3327 | set_uid_bit(sdev->unique_id, i, 1); |
| 3328 | sdev->unique_id_bits_known = i + 1; |
| 3329 | } else { |
| 3330 | pr_debug("no bus query response received\n"); |
| 3331 | rc = -ENXIO; |
| 3332 | return rc; |
| 3333 | } |
| 3334 | |
| 3335 | rc = bdev->desc->ops->bus_transaction(bdev, |
| 3336 | BIF_TRANS_BC, cmd_enter[resp[0] ? 0 : 1]); |
| 3337 | if (rc) { |
| 3338 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3339 | return rc; |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | rc = bdev->desc->ops->bus_transaction_query(bdev, |
| 3344 | BIF_TRANS_BC, BIF_CMD_DILC, &resp_dilc); |
| 3345 | if (rc) { |
| 3346 | pr_err("bus_transaction failed, rc=%d\n", rc); |
| 3347 | return rc; |
| 3348 | } |
| 3349 | |
| 3350 | if (resp_dilc) { |
| 3351 | sdev->present = true; |
| 3352 | sdev->bdev->selected_sdev = sdev; |
| 3353 | rc = bif_parse_slave_data(sdev); |
David Collins | 5a90ae9 | 2013-05-02 12:19:40 -0700 | [diff] [blame] | 3354 | if (rc) { |
| 3355 | pr_err("Failed to parse secondary slave data, rc=%d\n", |
| 3356 | rc); |
| 3357 | return rc; |
| 3358 | } |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3359 | } else { |
| 3360 | pr_err("Slave failed to respond to DILC bus command; its UID is thus unverified.\n"); |
| 3361 | sdev->unique_id_bits_known = 0; |
| 3362 | rc = -ENXIO; |
| 3363 | return rc; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | return rc; |
| 3368 | } |
| 3369 | |
| 3370 | /* |
| 3371 | * Removes slaves from the bif_sdev_list which have the same UID as previous |
| 3372 | * slaves in the list. |
| 3373 | */ |
| 3374 | static int bif_remove_duplicate_slaves(struct bif_ctrl_dev *bdev) |
| 3375 | { |
| 3376 | struct bif_slave_dev *sdev; |
| 3377 | struct bif_slave_dev *last_slave; |
| 3378 | struct bif_slave_dev *temp; |
| 3379 | |
| 3380 | list_for_each_entry_safe(last_slave, temp, &bif_sdev_list, list) { |
| 3381 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 3382 | if (last_slave == sdev) { |
| 3383 | break; |
| 3384 | } else if (memcmp(last_slave->unique_id, |
| 3385 | sdev->unique_id, |
| 3386 | BIF_UNIQUE_ID_BYTE_LENGTH) == 0) { |
| 3387 | bif_remove_slave(last_slave); |
| 3388 | break; |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | static int bif_add_all_slaves(struct bif_ctrl_dev *bdev) |
| 3397 | { |
| 3398 | struct bif_slave_dev *sdev; |
| 3399 | int rc = 0; |
| 3400 | int i; |
| 3401 | bool has_slave = false, is_primary_slave = false; |
| 3402 | |
| 3403 | mutex_lock(&bif_sdev_list_mutex); |
| 3404 | mutex_lock(&bdev->mutex); |
| 3405 | |
| 3406 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 3407 | if (sdev->bdev == bdev) { |
| 3408 | has_slave = true; |
| 3409 | break; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | if (!has_slave) { |
| 3414 | /* Create a single empty slave to start the search algorithm. */ |
| 3415 | sdev = bif_add_slave(bdev); |
| 3416 | if (IS_ERR(sdev)) { |
| 3417 | rc = PTR_ERR(sdev); |
| 3418 | pr_err("bif_add_slave failed, rc=%d\n", rc); |
| 3419 | goto out; |
| 3420 | } |
| 3421 | |
| 3422 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 3423 | /* Attempt to select primary slave in battery pack. */ |
| 3424 | rc = bdev->desc->ops->bus_transaction(bdev, |
| 3425 | BIF_TRANS_SDA, BIF_PRIMARY_SLAVE_DEV_ADR); |
| 3426 | if (rc == 0) |
| 3427 | break; |
| 3428 | } |
| 3429 | if (rc) { |
| 3430 | pr_err("BIF bus_transaction failed, rc=%d\n", rc); |
| 3431 | goto out; |
| 3432 | } |
| 3433 | |
| 3434 | /* Check if a slave is selected. */ |
| 3435 | rc = bif_is_slave_selected(bdev); |
| 3436 | if (rc < 0) { |
| 3437 | pr_err("BIF bus_transaction failed, rc=%d\n", rc); |
| 3438 | goto out; |
| 3439 | } else { |
| 3440 | is_primary_slave = rc; |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | if (is_primary_slave) { |
| 3445 | pr_debug("Using primary slave at DEV_ADR==0x%02X\n", |
| 3446 | BIF_PRIMARY_SLAVE_DEV_ADR); |
| 3447 | sdev->bdev->selected_sdev = sdev; |
| 3448 | sdev->present = true; |
| 3449 | sdev->slave_addr = BIF_PRIMARY_SLAVE_DEV_ADR; |
| 3450 | rc = bif_parse_slave_data(sdev); |
| 3451 | if (rc) { |
| 3452 | pr_err("Failed to parse primary slave data, rc=%d\n", |
| 3453 | rc); |
| 3454 | goto out; |
| 3455 | } |
| 3456 | rc = bif_add_secondary_slaves(sdev); |
| 3457 | if (rc) { |
| 3458 | pr_err("Failed to add secondary slaves, rc=%d\n", rc); |
| 3459 | goto out; |
| 3460 | } |
| 3461 | } else { |
| 3462 | pr_debug("Falling back on full UID search.\n"); |
| 3463 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 3464 | rc = bif_perform_uid_search(bdev); |
| 3465 | if (rc == 0) |
| 3466 | break; |
| 3467 | } |
| 3468 | if (rc) { |
| 3469 | pr_debug("BIF UID search failed, rc=%d\n", rc); |
| 3470 | goto out; |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | bif_remove_duplicate_slaves(bdev); |
| 3475 | |
| 3476 | mutex_unlock(&bdev->mutex); |
| 3477 | mutex_unlock(&bif_sdev_list_mutex); |
| 3478 | |
| 3479 | return rc; |
| 3480 | |
| 3481 | out: |
| 3482 | mutex_unlock(&bdev->mutex); |
| 3483 | mutex_unlock(&bif_sdev_list_mutex); |
| 3484 | pr_debug("BIF slave search failed, rc=%d\n", rc); |
| 3485 | return rc; |
| 3486 | } |
| 3487 | |
| 3488 | static int bif_add_known_slave(struct bif_ctrl_dev *bdev, u8 slave_addr) |
| 3489 | { |
| 3490 | struct bif_slave_dev *sdev; |
| 3491 | int rc = 0; |
| 3492 | int i; |
| 3493 | |
| 3494 | for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) { |
| 3495 | /* Attempt to select the slave. */ |
| 3496 | rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA, |
| 3497 | slave_addr); |
| 3498 | if (rc == 0) |
| 3499 | break; |
| 3500 | } |
| 3501 | if (rc) { |
| 3502 | pr_err("BIF bus_transaction failed, rc=%d\n", rc); |
| 3503 | return rc; |
| 3504 | } |
| 3505 | |
| 3506 | /* Check if a slave is selected. */ |
| 3507 | rc = bif_is_slave_selected(bdev); |
| 3508 | if (rc < 0) { |
| 3509 | pr_err("BIF bus_transaction failed, rc=%d\n", rc); |
| 3510 | return rc; |
| 3511 | } |
| 3512 | |
| 3513 | sdev = bif_add_slave(bdev); |
| 3514 | if (IS_ERR(sdev)) { |
| 3515 | rc = PTR_ERR(sdev); |
| 3516 | pr_err("bif_add_slave failed, rc=%d\n", rc); |
| 3517 | return rc; |
| 3518 | } |
| 3519 | |
| 3520 | sdev->bdev->selected_sdev = sdev; |
| 3521 | sdev->present = true; |
| 3522 | sdev->slave_addr = slave_addr; |
| 3523 | rc = bif_parse_slave_data(sdev); |
| 3524 | if (rc) { |
| 3525 | pr_err("Failed to parse slave data, addr=0x%02X, rc=%d\n", |
| 3526 | slave_addr, rc); |
| 3527 | return rc; |
| 3528 | } |
| 3529 | |
| 3530 | return rc; |
| 3531 | } |
| 3532 | |
| 3533 | static int bif_add_known_slaves_from_dt(struct bif_ctrl_dev *bdev, |
| 3534 | struct device_node *of_node) |
| 3535 | { |
| 3536 | int len = 0; |
| 3537 | int rc, i; |
| 3538 | u32 addr; |
| 3539 | const __be32 *val; |
| 3540 | |
| 3541 | mutex_lock(&bif_sdev_list_mutex); |
| 3542 | mutex_lock(&bdev->mutex); |
| 3543 | |
| 3544 | val = of_get_property(of_node, "qcom,known-device-addresses", &len); |
| 3545 | len /= sizeof(u32); |
| 3546 | if (val && len == 0) { |
| 3547 | pr_err("qcom,known-device-addresses property is invalid\n"); |
| 3548 | rc = -EINVAL; |
| 3549 | goto out; |
| 3550 | } |
| 3551 | |
| 3552 | for (i = 0; i < len; i++) { |
| 3553 | addr = be32_to_cpup(val++); |
| 3554 | if (addr == 0x00 || addr > 0xFF) { |
| 3555 | rc = -EINVAL; |
| 3556 | pr_err("qcom,known-device-addresses property contains invalid address=0x%X\n", |
| 3557 | addr); |
| 3558 | goto out; |
| 3559 | } |
| 3560 | rc = bif_add_known_slave(bdev, addr); |
| 3561 | if (rc) { |
| 3562 | pr_err("bif_add_known_slave() failed, rc=%d\n", rc); |
| 3563 | goto out; |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | out: |
| 3568 | if (len > 0) |
| 3569 | bif_remove_duplicate_slaves(bdev); |
| 3570 | |
| 3571 | mutex_unlock(&bdev->mutex); |
| 3572 | mutex_unlock(&bif_sdev_list_mutex); |
| 3573 | |
| 3574 | return rc; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * Programs a device address for the specified slave in order to simplify |
| 3579 | * slave selection in the future. |
| 3580 | */ |
| 3581 | static int bif_assign_slave_dev_addr(struct bif_slave_dev *sdev, u8 dev_addr) |
| 3582 | { |
| 3583 | int rc; |
| 3584 | u16 addr; |
| 3585 | |
| 3586 | if (!sdev->protocol_function) { |
| 3587 | pr_err("Protocol function not present; cannot set device address.\n"); |
| 3588 | return -ENODEV; |
| 3589 | } |
| 3590 | |
| 3591 | addr = PROTOCOL_FUNC_DEV_ADR_ADDR( |
| 3592 | sdev->protocol_function->protocol_pointer); |
| 3593 | |
| 3594 | rc = _bif_slave_write(sdev, addr, &dev_addr, 1); |
| 3595 | if (rc) |
| 3596 | pr_err("Failed to set slave device address.\n"); |
| 3597 | else |
| 3598 | sdev->slave_addr = dev_addr; |
| 3599 | |
| 3600 | return rc; |
| 3601 | } |
| 3602 | |
| 3603 | /* Assigns a unique device address to all slaves which do not have one. */ |
| 3604 | static int bif_assign_all_slaves_dev_addr(struct bif_ctrl_dev *bdev) |
| 3605 | { |
| 3606 | struct bif_slave_dev *sdev; |
| 3607 | struct bif_slave_dev *sibling; |
| 3608 | bool duplicate; |
| 3609 | int rc = 0; |
| 3610 | u8 dev_addr, first_dev_addr; |
| 3611 | |
| 3612 | mutex_lock(&bif_sdev_list_mutex); |
| 3613 | mutex_lock(&bdev->mutex); |
| 3614 | |
| 3615 | first_dev_addr = next_dev_addr; |
| 3616 | /* |
| 3617 | * Iterate over all partially known UIDs adding new ones as they are |
| 3618 | * found. |
| 3619 | */ |
| 3620 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 3621 | /* |
| 3622 | * Skip slaves without known UIDs, which already have a device |
| 3623 | * address or which aren't present. |
| 3624 | */ |
| 3625 | if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH |
| 3626 | || sdev->slave_addr != 0x00 || !sdev->present) |
| 3627 | continue; |
| 3628 | |
| 3629 | do { |
| 3630 | dev_addr = next_dev_addr; |
| 3631 | duplicate = false; |
| 3632 | list_for_each_entry(sibling, &bif_sdev_list, list) { |
| 3633 | if (sibling->slave_addr == dev_addr) { |
| 3634 | duplicate = true; |
| 3635 | break; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | next_dev_addr = dev_addr + 1; |
| 3640 | } while (duplicate && (next_dev_addr != first_dev_addr)); |
| 3641 | |
| 3642 | if (next_dev_addr == first_dev_addr) { |
| 3643 | pr_err("No more BIF slave device addresses available.\n"); |
| 3644 | rc = -ENODEV; |
| 3645 | goto out; |
| 3646 | } |
| 3647 | |
| 3648 | rc = bif_assign_slave_dev_addr(sdev, dev_addr); |
| 3649 | if (rc) { |
| 3650 | pr_err("Failed to set slave address.\n"); |
| 3651 | goto out; |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | mutex_unlock(&bdev->mutex); |
| 3656 | mutex_unlock(&bif_sdev_list_mutex); |
| 3657 | |
| 3658 | return rc; |
| 3659 | |
| 3660 | out: |
| 3661 | mutex_unlock(&bdev->mutex); |
| 3662 | mutex_unlock(&bif_sdev_list_mutex); |
| 3663 | pr_err("BIF slave device address setting failed, rc=%d\n", rc); |
| 3664 | return rc; |
| 3665 | } |
| 3666 | |
| 3667 | /** |
| 3668 | * bdev_get_drvdata() - get the private BIF controller driver data |
| 3669 | * @bdev: BIF controller device pointer |
| 3670 | */ |
| 3671 | void *bdev_get_drvdata(struct bif_ctrl_dev *bdev) |
| 3672 | { |
| 3673 | return bdev->driver_data; |
| 3674 | } |
| 3675 | EXPORT_SYMBOL(bdev_get_drvdata); |
| 3676 | |
| 3677 | static const char * const battery_label[] = { |
| 3678 | "unknown", |
| 3679 | "none", |
| 3680 | "special 1", |
| 3681 | "special 2", |
| 3682 | "special 3", |
| 3683 | "low cost", |
| 3684 | "smart", |
| 3685 | }; |
| 3686 | |
| 3687 | static const char *bif_get_battery_pack_type(int rid_ohm) |
| 3688 | { |
| 3689 | const char *label = battery_label[0]; |
| 3690 | |
| 3691 | if (rid_ohm > BIF_BATT_RID_SMART_MAX) |
| 3692 | label = battery_label[1]; |
| 3693 | else if (rid_ohm >= BIF_BATT_RID_SMART_MIN) |
| 3694 | label = battery_label[6]; |
| 3695 | else if (rid_ohm >= BIF_BATT_RID_LOW_COST_MIN |
| 3696 | && rid_ohm <= BIF_BATT_RID_LOW_COST_MAX) |
| 3697 | label = battery_label[5]; |
| 3698 | else if (rid_ohm >= BIF_BATT_RID_SPECIAL3_MIN |
| 3699 | && rid_ohm <= BIF_BATT_RID_SPECIAL3_MAX) |
| 3700 | label = battery_label[4]; |
| 3701 | else if (rid_ohm >= BIF_BATT_RID_SPECIAL2_MIN |
| 3702 | && rid_ohm <= BIF_BATT_RID_SPECIAL2_MAX) |
| 3703 | label = battery_label[3]; |
| 3704 | else if (rid_ohm >= BIF_BATT_RID_SPECIAL1_MIN |
| 3705 | && rid_ohm <= BIF_BATT_RID_SPECIAL1_MAX) |
| 3706 | label = battery_label[2]; |
| 3707 | |
| 3708 | return label; |
| 3709 | } |
| 3710 | |
| 3711 | /** |
| 3712 | * bif_ctrl_register() - register a BIF controller with the BIF framework |
| 3713 | * @bif_desc: Pointer to BIF controller descriptor |
| 3714 | * @dev: Device pointer of the BIF controller |
| 3715 | * @driver_data: Private driver data to associate with the BIF controller |
| 3716 | * @of_node Pointer to the device tree node of the BIF controller |
| 3717 | * |
| 3718 | * Returns a BIF controller device pointer for the controller if registration |
| 3719 | * is successful or an ERR_PTR if an error occurred. |
| 3720 | */ |
| 3721 | struct bif_ctrl_dev *bif_ctrl_register(struct bif_ctrl_desc *bif_desc, |
| 3722 | struct device *dev, void *driver_data, struct device_node *of_node) |
| 3723 | { |
| 3724 | struct bif_ctrl_dev *bdev = ERR_PTR(-EINVAL); |
| 3725 | struct bif_slave_dev *sdev; |
| 3726 | bool battery_present = false; |
David Collins | 94a763e | 2013-09-12 13:15:44 -0700 | [diff] [blame] | 3727 | bool slaves_present = false; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3728 | int rc, rid_ohm; |
| 3729 | |
| 3730 | if (!bif_desc) { |
| 3731 | pr_err("Invalid bif_desc specified\n"); |
| 3732 | return bdev; |
| 3733 | } else if (!bif_desc->name) { |
| 3734 | pr_err("BIF name missing\n"); |
| 3735 | return bdev; |
| 3736 | } else if (!bif_desc->ops) { |
| 3737 | pr_err("BIF operations missing\n"); |
| 3738 | return bdev; |
| 3739 | } else if (!bif_desc->ops->bus_transaction |
| 3740 | || !bif_desc->ops->bus_transaction_query |
| 3741 | || !bif_desc->ops->bus_transaction_read |
| 3742 | || !bif_desc->ops->get_bus_state |
| 3743 | || !bif_desc->ops->set_bus_state) { |
| 3744 | pr_err("BIF operation callback function(s) missing\n"); |
| 3745 | return bdev; |
| 3746 | } |
| 3747 | |
| 3748 | bdev = kzalloc(sizeof(struct bif_ctrl_dev), GFP_KERNEL); |
| 3749 | if (bdev == NULL) { |
| 3750 | pr_err("Memory allocation failed for bif_ctrl_dev\n"); |
| 3751 | return ERR_PTR(-ENOMEM); |
| 3752 | } |
| 3753 | |
| 3754 | mutex_init(&bdev->mutex); |
| 3755 | INIT_LIST_HEAD(&bdev->list); |
| 3756 | INIT_DELAYED_WORK(&bdev->enter_irq_mode_work, bif_enter_irq_mode_work); |
| 3757 | bdev->desc = bif_desc; |
| 3758 | bdev->ctrl_dev = dev; |
| 3759 | bdev->driver_data = driver_data; |
| 3760 | bdev->irq_mode_delay_jiffies = 2; |
| 3761 | |
| 3762 | mutex_lock(&bif_ctrl_list_mutex); |
| 3763 | list_add_tail(&bdev->list, &bif_ctrl_list); |
| 3764 | mutex_unlock(&bif_ctrl_list_mutex); |
| 3765 | |
| 3766 | rc = bif_add_all_slaves(bdev); |
| 3767 | if (rc) |
| 3768 | pr_debug("Search for all slaves failed, rc=%d\n", rc); |
| 3769 | rc = bif_add_known_slaves_from_dt(bdev, of_node); |
| 3770 | if (rc) |
| 3771 | pr_err("Adding slaves based on device tree addressed failed, rc=%d.\n", |
| 3772 | rc); |
| 3773 | rc = bif_assign_all_slaves_dev_addr(bdev); |
| 3774 | if (rc) |
| 3775 | pr_err("Failed to set slave device address, rc=%d\n", rc); |
| 3776 | |
| 3777 | bif_print_slaves(); |
| 3778 | |
| 3779 | if (bdev->desc->ops->get_battery_presence) { |
| 3780 | rc = bdev->desc->ops->get_battery_presence(bdev); |
| 3781 | if (rc < 0) { |
| 3782 | pr_err("Could not determine battery presence, rc=%d\n", |
| 3783 | rc); |
| 3784 | } else { |
| 3785 | battery_present = rc; |
| 3786 | pr_info("Battery pack present = %c\n", rc ? 'Y' : 'N'); |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | if (bdev->desc->ops->get_battery_rid) { |
| 3791 | rid_ohm = bdev->desc->ops->get_battery_rid(bdev); |
| 3792 | if (rid_ohm >= 0) |
| 3793 | pr_info("Battery pack type = %s (Rid=%d ohm)\n", |
| 3794 | bif_get_battery_pack_type(rid_ohm), rid_ohm); |
| 3795 | else |
| 3796 | pr_err("Could not read Rid, rc=%d\n", rid_ohm); |
| 3797 | } |
| 3798 | |
| 3799 | list_for_each_entry(sdev, &bif_sdev_list, list) { |
| 3800 | if (sdev->present) { |
| 3801 | battery_present = true; |
David Collins | 94a763e | 2013-09-12 13:15:44 -0700 | [diff] [blame] | 3802 | slaves_present = true; |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3803 | break; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | BLOCKING_INIT_NOTIFIER_HEAD(&bdev->bus_change_notifier); |
| 3808 | |
David Collins | 94a763e | 2013-09-12 13:15:44 -0700 | [diff] [blame] | 3809 | /* Disable the BIF bus master if no slaves are found. */ |
| 3810 | if (!slaves_present) { |
| 3811 | rc = bdev->desc->ops->set_bus_state(bdev, |
| 3812 | BIF_BUS_STATE_MASTER_DISABLED); |
| 3813 | if (rc < 0) |
| 3814 | pr_err("Could not disble BIF master, rc=%d\n", rc); |
| 3815 | } |
| 3816 | |
David Collins | ed93049 | 2013-01-23 13:57:09 -0800 | [diff] [blame] | 3817 | if (battery_present) { |
| 3818 | bdev->battery_present = true; |
| 3819 | rc = blocking_notifier_call_chain(&bdev->bus_change_notifier, |
| 3820 | BIF_BUS_EVENT_BATTERY_INSERTED, bdev); |
| 3821 | if (rc) |
| 3822 | pr_err("Call chain noification failed, rc=%d\n", rc); |
| 3823 | } |
| 3824 | |
| 3825 | return bdev; |
| 3826 | } |
| 3827 | EXPORT_SYMBOL(bif_ctrl_register); |
| 3828 | |
| 3829 | /** |
| 3830 | * bif_ctrl_unregister() - unregisters a BIF controller |
| 3831 | * @bdev: BIF controller device pointer |
| 3832 | */ |
| 3833 | void bif_ctrl_unregister(struct bif_ctrl_dev *bdev) |
| 3834 | { |
| 3835 | if (bdev) { |
| 3836 | mutex_lock(&bif_ctrl_list_mutex); |
| 3837 | list_del(&bdev->list); |
| 3838 | mutex_unlock(&bif_ctrl_list_mutex); |
| 3839 | } |
| 3840 | } |
| 3841 | EXPORT_SYMBOL(bif_ctrl_unregister); |