blob: 3030caac617f8474ebc4a0bc7ebe418487fc3e57 [file] [log] [blame]
David Collinsed930492013-01-23 13:57:09 -08001/* 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 */
63struct 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 */
86struct 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 */
129struct 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 */
149struct 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
157static DEFINE_MUTEX(bif_ctrl_list_mutex);
158static LIST_HEAD(bif_ctrl_list);
159static DEFINE_MUTEX(bif_sdev_list_mutex);
160static LIST_HEAD(bif_sdev_list);
161
162static u8 next_dev_addr = 0x02;
163
164#define DEBUG_PRINT_BUFFER_SIZE 256
165static 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
177static 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) {
208 pr_debug(" NVM function: pointer=0x%04X, task=%d, wr_buf_size=%d, nvm_base=0x%04X, nvm_size=%d\n",
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,
214 sdev->nvm_function->nvm_size);
215 if (sdev->nvm_function->object_count)
216 pr_debug(" NVM objects:\n");
217 i = 0;
218 list_for_each_entry(object, &sdev->nvm_function->object_list,
219 list) {
220 pr_debug(" Object %d - addr=0x%04X, data len=%d, type=0x%02X, version=0x%02X, manufacturer ID=0x%04X, crc=0x%04X\n",
221 i, object->addr, object->length - 8,
222 object->type, object->version,
223 object->manufacturer_id, object->crc);
224 for (j = 0; j < DIV_ROUND_UP(object->length - 8, 16);
225 j++) {
226 fill_string(str, DEBUG_PRINT_BUFFER_SIZE,
227 object->data + j * 16,
228 min(16, object->length - 8 - (j * 16)));
229 pr_debug(" data(0x%04X): %s\n", j * 16,
230 str);
231 }
232 i++;
233 }
234 }
235}
236
237static void bif_print_slaves(void)
238{
239 struct bif_slave_dev *sdev;
240
241 mutex_lock(&bif_sdev_list_mutex);
242
243 list_for_each_entry(sdev, &bif_sdev_list, list) {
244 /* Skip slaves without fully known UIDs. */
245 if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH)
246 continue;
247 bif_print_slave_data(sdev);
248 }
249
250 mutex_unlock(&bif_sdev_list_mutex);
251}
252
253static struct bif_slave_dev *bif_add_slave(struct bif_ctrl_dev *bdev)
254{
255 struct bif_slave_dev *sdev;
256
257 sdev = kzalloc(sizeof(struct bif_slave_dev), GFP_KERNEL);
258 if (sdev == NULL) {
259 pr_err("Memory allocation failed for bif_slave_dev\n");
260 return ERR_PTR(-ENOMEM);
261 }
262
263 sdev->bdev = bdev;
264 INIT_LIST_HEAD(&sdev->list);
265 list_add_tail(&sdev->list, &bif_sdev_list);
266
267 return sdev;
268}
269
270static void bif_remove_slave(struct bif_slave_dev *sdev)
271{
272 list_del(&sdev->list);
273 if (sdev->bdev->selected_sdev == sdev)
274 sdev->bdev->selected_sdev = NULL;
275
276 if (sdev->slave_ctrl_function)
277 kfree(sdev->slave_ctrl_function->irq_notifier_list);
278 kfree(sdev->slave_ctrl_function);
279 kfree(sdev->protocol_function);
280 kfree(sdev->function_directory);
281
282 kfree(sdev);
283}
284
285/* This function assumes that the uid array is all 0 to start with. */
286static void set_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH], unsigned int bit,
287 unsigned int value)
288{
289 u8 mask;
290
291 if (bit >= BIF_UNIQUE_ID_BIT_LENGTH)
292 return;
293
294 mask = 1 << (7 - (bit % 8));
295
296 uid[bit / 8] &= ~mask;
297 uid[bit / 8] |= value << (7 - (bit % 8));
298}
299
300static unsigned int get_uid_bit(u8 uid[BIF_UNIQUE_ID_BYTE_LENGTH],
301 unsigned int bit)
302{
303 if (bit >= BIF_UNIQUE_ID_BIT_LENGTH)
304 return 0;
305
306 return (uid[bit / 8] & (1 << (7 - (bit % 8)))) ? 1 : 0;
307}
308
309static void bif_enter_irq_mode_work(struct work_struct *work)
310{
311 struct delayed_work *dwork = to_delayed_work(work);
312 struct bif_ctrl_dev *bdev
313 = container_of(dwork, struct bif_ctrl_dev, enter_irq_mode_work);
314 int rc, i;
315
316 mutex_lock(&bdev->mutex);
317 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
318 rc = bdev->desc->ops->set_bus_state(bdev,
319 BIF_BUS_STATE_INTERRUPT);
320 if (rc == 0)
321 break;
322 }
323 mutex_unlock(&bdev->mutex);
324
325 /* Reschedule the task if the transaction failed. */
326 if (rc) {
327 pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc);
328 schedule_delayed_work(&bdev->enter_irq_mode_work,
329 bdev->irq_mode_delay_jiffies);
330 }
331}
332
333static void bif_cancel_irq_mode_work(struct bif_ctrl_dev *bdev)
334{
335 cancel_delayed_work(&bdev->enter_irq_mode_work);
336}
337
338static void bif_schedule_irq_mode_work(struct bif_ctrl_dev *bdev)
339{
340 if (bdev->irq_count > 0 &&
341 bdev->desc->ops->get_bus_state(bdev) != BIF_BUS_STATE_INTERRUPT)
342 schedule_delayed_work(&bdev->enter_irq_mode_work,
343 bdev->irq_mode_delay_jiffies);
344}
345
346static int _bif_select_slave_no_retry(struct bif_slave_dev *sdev)
347{
348 struct bif_ctrl_dev *bdev = sdev->bdev;
349 int rc = 0;
350 int i;
351
352 /* Check if the slave is already selected. */
353 if (sdev->bdev->selected_sdev == sdev)
354 return 0;
355
356 if (sdev->slave_addr) {
357 /* Select using DEV_ADR. */
358 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
359 sdev->slave_addr);
360 if (!rc)
361 sdev->bdev->selected_sdev = sdev;
362 } else if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH) {
363 /* Select using full UID. */
364 for (i = 0; i < BIF_UNIQUE_ID_BYTE_LENGTH - 1; i++) {
365 rc = bdev->desc->ops->bus_transaction(bdev,
366 BIF_TRANS_EDA, sdev->unique_id[i]);
367 if (rc)
368 goto out;
369 }
370
371 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
372 sdev->unique_id[BIF_UNIQUE_ID_BYTE_LENGTH - 1]);
373 if (rc)
374 goto out;
375 } else {
376 pr_err("Cannot select slave because it has neither UID nor DEV_ADR.\n");
377 return -EINVAL;
378 }
379
380 sdev->bdev->selected_sdev = sdev;
381
382 return 0;
383out:
384 pr_err("bus_transaction failed, rc=%d\n", rc);
385 return rc;
386}
387
388static int bif_select_slave(struct bif_slave_dev *sdev)
389{
390 int rc = -EPERM;
391 int i;
392
393 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
394 rc = _bif_select_slave_no_retry(sdev);
395 if (rc == 0)
396 break;
397 /* Force slave reselection. */
398 sdev->bdev->selected_sdev = NULL;
399 }
400
401 return rc;
402}
403
404/*
405 * Returns 1 if slave is selected, 0 if slave is not selected, or errno if
406 * error.
407 */
408static int bif_is_slave_selected(struct bif_ctrl_dev *bdev)
409{
410 int rc = -EPERM;
411 int tack, i;
412
413 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
414 /* Attempt a transaction query. */
415 rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_BC,
416 BIF_CMD_TQ, &tack);
417 if (rc == 0 || rc == -ETIMEDOUT)
418 break;
419 }
420
421 if (rc == 0)
422 rc = 1;
423 else if (rc == -ETIMEDOUT)
424 rc = 0;
425 else
426 pr_err("BIF bus_transaction_read failed, rc=%d\n", rc);
427
428 return rc;
429}
430
431/* Read from a specified number of consecutive registers. */
432static int _bif_slave_read_no_retry(struct bif_slave_dev *sdev, u16 addr,
433 u8 *buf, int len)
434{
435 struct bif_ctrl_dev *bdev = sdev->bdev;
436 int rc = 0;
437 int i, response;
438
439 rc = bif_select_slave(sdev);
440 if (rc)
441 return rc;
442
443 if (bdev->desc->ops->read_slave_registers) {
444 /*
445 * Use low level slave register read implementation in order to
446 * receive the benefits of BIF burst reads.
447 */
448 rc = bdev->desc->ops->read_slave_registers(bdev, addr, buf,
449 len);
450 if (rc)
451 pr_err("read_slave_registers failed, rc=%d\n", rc);
452 return rc;
453 }
454
455 for (i = 0; i < len; i++) {
456 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA,
457 addr >> 8);
458 if (rc) {
459 pr_err("bus_transaction failed, rc=%d\n", rc);
460 return rc;
461 }
462
463 rc = bdev->desc->ops->bus_transaction_read(bdev, BIF_TRANS_RRA,
464 addr & 0xFF, &response);
465 if (rc) {
466 pr_err("bus_transaction_read failed, rc=%d\n", rc);
467 return rc;
468 }
469
470 if (!(response & BIF_SLAVE_RD_ACK)) {
471 pr_err("BIF register read error=0x%02X\n",
472 response & BIF_SLAVE_RD_ERR);
473 return -EIO;
474 }
475
476 buf[i] = response & BIF_SLAVE_RD_DATA;
477 addr++;
478 }
479
480 return rc;
481}
482
483/*
484 * Read from a specified number of consecutive registers. Retry the transaction
485 * several times in case of communcation failures.
486 */
487static int _bif_slave_read(struct bif_slave_dev *sdev, u16 addr, u8 *buf,
488 int len)
489{
490 int rc = -EPERM;
491 int i;
492
493 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
494 rc = _bif_slave_read_no_retry(sdev, addr, buf, len);
495 if (rc == 0)
496 break;
497 /* Force slave reselection. */
498 sdev->bdev->selected_sdev = NULL;
499 }
500
501 return rc;
502}
503
504/* Write to a specified number of consecutive registers. */
505static int _bif_slave_write_no_retry(struct bif_slave_dev *sdev, u16 addr,
506 u8 *buf, int len)
507{
508 struct bif_ctrl_dev *bdev = sdev->bdev;
509 int rc = 0;
510 int i;
511
512 rc = bif_select_slave(sdev);
513 if (rc)
514 return rc;
515
516 if (bdev->desc->ops->write_slave_registers) {
517 /*
518 * Use low level slave register write implementation in order to
519 * receive the benefits of BIF burst writes.
520 */
521 rc = bdev->desc->ops->write_slave_registers(bdev, addr, buf,
522 len);
523 if (rc)
524 pr_err("write_slave_registers failed, rc=%d\n", rc);
525 return rc;
526 }
527
528 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_ERA, addr >> 8);
529 if (rc)
530 goto out;
531
532 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WRA, addr & 0xFF);
533 if (rc)
534 goto out;
535
536 for (i = 0; i < len; i++) {
537 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_WD,
538 buf[i]);
539 if (rc)
540 goto out;
541 }
542
543 return 0;
544out:
545 pr_err("bus_transaction failed, rc=%d\n", rc);
546 return rc;
547}
548
549/*
550 * Write to a specified number of consecutive registers. Retry the transaction
551 * several times in case of communcation failures.
552 */
553static int _bif_slave_write(struct bif_slave_dev *sdev, u16 addr, u8 *buf,
554 int len)
555{
556 int rc = -EPERM;
557 int i;
558
559 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
560 rc = _bif_slave_write_no_retry(sdev, addr, buf, len);
561 if (rc == 0)
562 break;
563 /* Force slave reselection. */
564 sdev->bdev->selected_sdev = NULL;
565 }
566
567 return rc;
568}
569
570/* Takes a mutex if this consumer is not an exclusive bus user. */
571static void bif_ctrl_lock(struct bif_ctrl *ctrl)
572{
573 if (!ctrl->exclusive_lock) {
574 mutex_lock(&ctrl->bdev->mutex);
575 bif_cancel_irq_mode_work(ctrl->bdev);
576 }
577}
578
579/* Releases a mutex if this consumer is not an exclusive bus user. */
580static void bif_ctrl_unlock(struct bif_ctrl *ctrl)
581{
582 if (!ctrl->exclusive_lock) {
583 bif_schedule_irq_mode_work(ctrl->bdev);
584 mutex_unlock(&ctrl->bdev->mutex);
585 }
586}
587
588static void bif_slave_ctrl_lock(struct bif_slave *slave)
589{
590 bif_ctrl_lock(&slave->ctrl);
591}
592
593static void bif_slave_ctrl_unlock(struct bif_slave *slave)
594{
595 bif_ctrl_unlock(&slave->ctrl);
596}
597
598static int bif_check_task(struct bif_slave *slave, unsigned int task)
599{
600 if (IS_ERR_OR_NULL(slave)) {
601 pr_err("Invalid slave handle.\n");
602 return -EINVAL;
603 } else if (!slave->sdev->bdev) {
604 pr_err("BIF controller has been removed.\n");
605 return -ENXIO;
606 } else if (!slave->sdev->slave_ctrl_function
607 || slave->sdev->slave_ctrl_function->task_count == 0) {
608 pr_err("BIF slave does not support slave control.\n");
609 return -ENODEV;
610 } else if (task >= slave->sdev->slave_ctrl_function->task_count) {
611 pr_err("Requested task: %u greater than max: %u for this slave\n",
612 task, slave->sdev->slave_ctrl_function->task_count);
613 return -EINVAL;
614 }
615
616 return 0;
617}
618
619/**
620 * bif_request_irq() - request a BIF slave IRQ by slave task number
621 * @slave: BIF slave handle
622 * @task: BIF task number of the IRQ inside of the slave. This
623 * corresponds to the slave control channel specified for a given
624 * BIF function inside of the slave.
625 * @nb: Notifier block to call when the IRQ fires
626 *
627 * This function registers a notifier block to call when the BIF slave interrupt
628 * is triggered and also enables the interrupt. The interrupt is enabled inside
629 * of the BIF slave's slave control function and also the BIF bus is put into
630 * interrupt mode.
631 *
632 * Returns 0 for success or errno if an error occurred.
633 */
634int bif_request_irq(struct bif_slave *slave, unsigned int task,
635 struct notifier_block *nb)
636{
637 int rc;
638 u16 addr;
639 u8 reg, mask;
640
641 rc = bif_check_task(slave, task);
642 if (rc) {
643 pr_err("Invalid slave or task, rc=%d\n", rc);
644 return rc;
645 }
646
647 bif_slave_ctrl_lock(slave);
648
649 rc = blocking_notifier_chain_register(
650 &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb);
651 if (rc) {
652 pr_err("Notifier registration failed, rc=%d\n", rc);
653 goto done;
654 }
655
656 /* Enable the interrupt within the slave */
657 mask = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
658 addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR(
659 slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
660 if (task / SLAVE_CTRL_TASKS_PER_SET == 0) {
661 /* Set global interrupt enable. */
662 mask |= BIT(0);
663 }
664 rc = _bif_slave_read(slave->sdev, addr, &reg, 1);
665 if (rc) {
666 pr_err("BIF slave register read failed, rc=%d\n", rc);
667 goto notifier_unregister;
668 }
669 reg |= mask;
670 rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
671 if (rc) {
672 pr_err("BIF slave register write failed, rc=%d\n", rc);
673 goto notifier_unregister;
674 }
675
676 /* Set global interrupt enable if task not in set 0. */
677 if (task / SLAVE_CTRL_TASKS_PER_SET != 0) {
678 mask = BIT(0);
679 addr = SLAVE_CTRL_FUNC_IRQ_EN_ADDR(
680 slave->sdev->slave_ctrl_function->slave_ctrl_pointer, 0);
681 rc = _bif_slave_read(slave->sdev, addr, &reg, 1);
682 if (rc) {
683 pr_err("BIF slave register read failed, rc=%d\n", rc);
684 goto notifier_unregister;
685 }
686 reg |= mask;
687 rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
688 if (rc) {
689 pr_err("BIF slave register write failed, rc=%d\n", rc);
690 goto notifier_unregister;
691 }
692 }
693
694 rc = slave->sdev->bdev->desc->ops->set_bus_state(slave->sdev->bdev,
695 BIF_BUS_STATE_INTERRUPT);
696 if (rc) {
697 pr_err("Could not set BIF bus to interrupt mode, rc=%d\n", rc);
698 goto notifier_unregister;
699 }
700
701 slave->sdev->bdev->irq_count++;
702done:
703 bif_slave_ctrl_unlock(slave);
704
705 return rc;
706
707notifier_unregister:
708 blocking_notifier_chain_unregister(
709 &slave->sdev->slave_ctrl_function->irq_notifier_list[task],
710 nb);
711 bif_slave_ctrl_unlock(slave);
712
713 return rc;
714
715}
716EXPORT_SYMBOL(bif_request_irq);
717
718/**
719 * bif_free_irq() - free a BIF slave IRQ by slave task number
720 * @slave: BIF slave handle
721 * @task: BIF task number of the IRQ inside of the slave. This
722 * corresponds to the slave control channel specified for a given
723 * BIF function inside of the slave.
724 * @nb: Notifier block previously registered with this interrupt
725 *
726 * This function unregisters a notifier block that was previously registered
727 * with bif_request_irq().
728 *
729 * Returns 0 for success or errno if an error occurred.
730 */
731int bif_free_irq(struct bif_slave *slave, unsigned int task,
732 struct notifier_block *nb)
733{
734 int rc;
735 u16 addr;
736 u8 reg;
737
738 rc = bif_check_task(slave, task);
739 if (rc) {
740 pr_err("Invalid slave or task, rc=%d\n", rc);
741 return rc;
742 }
743
744 bif_slave_ctrl_lock(slave);
745
746 /* Disable the interrupt within the slave */
747 reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
748 addr = SLAVE_CTRL_FUNC_IRQ_CLEAR_ADDR(
749 slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
750 rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
751 if (rc) {
752 pr_err("BIF slave register write failed, rc=%d\n", rc);
753 goto done;
754 }
755
756 rc = blocking_notifier_chain_unregister(
757 &slave->sdev->slave_ctrl_function->irq_notifier_list[task], nb);
758 if (rc) {
759 pr_err("Notifier unregistration failed, rc=%d\n", rc);
760 goto done;
761 }
762
763 slave->sdev->bdev->irq_count--;
764
765 if (slave->sdev->bdev->irq_count == 0) {
766 bif_cancel_irq_mode_work(slave->sdev->bdev);
767 } else if (slave->sdev->bdev->irq_count < 0) {
768 pr_err("Unbalanced IRQ free.\n");
769 rc = -EINVAL;
770 slave->sdev->bdev->irq_count = 0;
771 }
772done:
773 bif_slave_ctrl_unlock(slave);
774
775 return rc;
776}
777EXPORT_SYMBOL(bif_free_irq);
778
779/**
780 * bif_trigger_task() - trigger a task within a BIF slave
781 * @slave: BIF slave handle
782 * @task: BIF task inside of the slave to trigger. This corresponds to
783 * the slave control channel specified for a given BIF function
784 * inside of the slave.
785 *
786 * Returns 0 for success or errno if an error occurred.
787 */
788int bif_trigger_task(struct bif_slave *slave, unsigned int task)
789{
790 int rc;
791 u16 addr;
792 u8 reg;
793
794 rc = bif_check_task(slave, task);
795 if (rc) {
796 pr_err("Invalid slave or task, rc=%d\n", rc);
797 return rc;
798 }
799
800 bif_slave_ctrl_lock(slave);
801
802 /* Trigger the task within the slave. */
803 reg = BIT(task % SLAVE_CTRL_TASKS_PER_SET);
804 addr = SLAVE_CTRL_FUNC_TASK_TRIGGER_ADDR(
805 slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
806 rc = _bif_slave_write(slave->sdev, addr, &reg, 1);
807 if (rc) {
808 pr_err("BIF slave register write failed, rc=%d\n", rc);
809 goto done;
810 }
811
812done:
813 bif_slave_ctrl_unlock(slave);
814
815 return rc;
816}
817EXPORT_SYMBOL(bif_trigger_task);
818
819/**
820 * bif_task_is_busy() - checks the state of a BIF slave task
821 * @slave: BIF slave handle
822 * @task: BIF task inside of the slave to trigger. This corresponds to
823 * the slave control channel specified for a given BIF function
824 * inside of the slave.
825 *
826 * Returns 1 if the task is busy, 0 if it is not busy, and errno on error.
827 */
828int bif_task_is_busy(struct bif_slave *slave, unsigned int task)
829{
830 int rc;
831 u16 addr;
832 u8 reg;
833
834 rc = bif_check_task(slave, task);
835 if (rc) {
836 pr_err("Invalid slave or task, rc=%d\n", rc);
837 return rc;
838 }
839
840 bif_slave_ctrl_lock(slave);
841
842 /* Check the task busy state. */
843 addr = SLAVE_CTRL_FUNC_TASK_BUSY_ADDR(
844 slave->sdev->slave_ctrl_function->slave_ctrl_pointer, task);
845 rc = _bif_slave_read(slave->sdev, addr, &reg, 1);
846 if (rc) {
847 pr_err("BIF slave register read failed, rc=%d\n", rc);
848 goto done;
849 }
850
851 rc = (reg & BIT(task % SLAVE_CTRL_TASKS_PER_SET)) ? 1 : 0;
852done:
853 bif_slave_ctrl_unlock(slave);
854
855 return rc;
856}
857EXPORT_SYMBOL(bif_task_is_busy);
858
859static int bif_slave_notify_irqs(struct bif_slave_dev *sdev, int set, u8 val)
860{
861 int rc = 0;
862 int i, task;
863
864 for (i = 0; i < SLAVE_CTRL_TASKS_PER_SET; i++) {
865 if (val & (1 << i)) {
866 task = set * SLAVE_CTRL_TASKS_PER_SET + i;
867
868 rc = blocking_notifier_call_chain(
869 &sdev->slave_ctrl_function->irq_notifier_list[task],
870 task, sdev->bdev);
871 rc = notifier_to_errno(rc);
872 if (rc)
873 pr_err("Notification failed for task %d\n",
874 task);
875 }
876 }
877
878 return rc;
879}
880
881static int bif_slave_handle_irq(struct bif_slave_dev *sdev)
882{
883 struct bif_ctrl_dev *bdev = sdev->bdev;
884 bool resp = false;
885 int rc = 0;
886 int i;
887 u16 addr;
888 u8 reg;
889
890 mutex_lock(&sdev->bdev->mutex);
891 bif_cancel_irq_mode_work(sdev->bdev);
892
893 rc = bif_select_slave(sdev);
894 if (rc) {
895 pr_err("Could not select slave, rc=%d\n", rc);
896 goto done;
897 }
898
899 /* Check overall slave interrupt status. */
900 rc = bdev->desc->ops->bus_transaction_query(bdev, BIF_TRANS_BC,
901 BIF_CMD_ISTS, &resp);
902 if (rc) {
903 pr_err("Could not query slave interrupt status, rc=%d\n", rc);
904 goto done;
905 }
906
907 if (resp) {
908 for (i = 0; i < sdev->slave_ctrl_function->task_count
909 / SLAVE_CTRL_TASKS_PER_SET; i++) {
910 addr = sdev->slave_ctrl_function->slave_ctrl_pointer
911 + 4 * i + 1;
912 rc = _bif_slave_read(sdev, addr, &reg, 1);
913 if (rc) {
914 pr_err("BIF slave register read failed, rc=%d\n",
915 rc);
916 goto done;
917 }
918
919 /* Ensure that interrupts are pending in the set. */
920 if (reg != 0x00) {
921 /*
922 * Release mutex before notifying consumers so
923 * that they can use the bus.
924 */
925 mutex_unlock(&sdev->bdev->mutex);
926 rc = bif_slave_notify_irqs(sdev, i, reg);
927 if (rc) {
928 pr_err("BIF slave irq notification failed, rc=%d\n",
929 rc);
930 goto notification_failed;
931 }
932 mutex_lock(&sdev->bdev->mutex);
933
934 rc = bif_select_slave(sdev);
935 if (rc) {
936 pr_err("Could not select slave, rc=%d\n",
937 rc);
938 goto done;
939 }
940
941 /* Clear all interrupts in this set. */
942 rc = _bif_slave_write(sdev, addr, &reg, 1);
943 if (rc) {
944 pr_err("BIF slave register write failed, rc=%d\n",
945 rc);
946 goto done;
947 }
948 }
949 }
950 }
951
952done:
953 bif_schedule_irq_mode_work(sdev->bdev);
954 mutex_unlock(&sdev->bdev->mutex);
955notification_failed:
956 if (rc == 0)
957 rc = resp;
958 return rc;
959}
960
961/**
962 * bif_ctrl_notify_slave_irq() - notify the BIF framework that a slave interrupt
963 * was received by a BIF controller
964 * @bdev: BIF controller device pointer
965 *
966 * This function should only be called from a BIF controller driver.
967 *
968 * Returns 0 for success or errno if an error occurred.
969 */
970int bif_ctrl_notify_slave_irq(struct bif_ctrl_dev *bdev)
971{
972 struct bif_slave_dev *sdev;
973 int rc = 0, handled = 0;
974
975 if (IS_ERR_OR_NULL(bdev))
976 return -EINVAL;
977
978 mutex_lock(&bif_sdev_list_mutex);
979
980 list_for_each_entry(sdev, &bif_sdev_list, list) {
981 if (sdev->bdev == bdev && sdev->present) {
982 rc = bif_slave_handle_irq(sdev);
983 if (rc < 0) {
984 pr_err("Could not handle BIF slave irq, rc=%d\n",
985 rc);
986 break;
987 }
988 handled += rc;
989 }
990 }
991
992 mutex_unlock(&bif_sdev_list_mutex);
993
994 if (handled == 0)
995 pr_info("Spurious BIF slave interrupt detected.\n");
996
997 if (rc > 0)
998 rc = 0;
999
1000 return rc;
1001}
1002EXPORT_SYMBOL(bif_ctrl_notify_slave_irq);
1003
1004/**
1005 * bif_ctrl_notify_battery_changed() - notify the BIF framework that a battery
1006 * pack has been inserted or removed
1007 * @bdev: BIF controller device pointer
1008 *
1009 * This function should only be called from a BIF controller driver.
1010 *
1011 * Returns 0 for success or errno if an error occurred.
1012 */
1013int bif_ctrl_notify_battery_changed(struct bif_ctrl_dev *bdev)
1014{
1015 int rc = 0;
1016 int present;
1017
1018 if (IS_ERR_OR_NULL(bdev))
1019 return -EINVAL;
1020
1021 if (bdev->desc->ops->get_battery_presence) {
1022 present = bdev->desc->ops->get_battery_presence(bdev);
1023 if (present < 0) {
1024 pr_err("Could not determine battery presence, rc=%d\n",
1025 rc);
1026 return rc;
1027 }
1028
1029 if (bdev->battery_present == !!present)
1030 return 0;
1031
1032 bdev->battery_present = present;
1033
1034 rc = blocking_notifier_call_chain(&bdev->bus_change_notifier,
1035 present ? BIF_BUS_EVENT_BATTERY_INSERTED
1036 : BIF_BUS_EVENT_BATTERY_REMOVED, bdev);
1037 if (rc)
1038 pr_err("Call chain noification failed, rc=%d\n", rc);
1039 }
1040
1041 return rc;
1042}
1043EXPORT_SYMBOL(bif_ctrl_notify_battery_changed);
1044
1045/**
1046 * bif_ctrl_signal_battery_changed() - notify the BIF framework that a battery
1047 * pack has been inserted or removed
1048 * @ctrl: BIF controller consumer handle
1049 *
1050 * This function should only be called by a BIF consumer driver on systems where
1051 * the BIF controller driver is unable to determine when a battery is inserted
1052 * or removed.
1053 *
1054 * Returns 0 for success or errno if an error occurred.
1055 */
1056int bif_ctrl_signal_battery_changed(struct bif_ctrl *ctrl)
1057{
1058 if (IS_ERR_OR_NULL(ctrl))
1059 return -EINVAL;
1060
1061 return bif_ctrl_notify_battery_changed(ctrl->bdev);
1062}
1063EXPORT_SYMBOL(bif_ctrl_signal_battery_changed);
1064
1065/**
1066 * bif_ctrl_notifier_register() - register a notifier block to be called when
1067 * a battery pack is inserted or removed
1068 * @ctrl: BIF controller consumer handle
1069 *
1070 * The value passed into the notifier when it is called is one of
1071 * enum bif_bus_event.
1072 *
1073 * Returns 0 for success or errno if an error occurred.
1074 */
1075int bif_ctrl_notifier_register(struct bif_ctrl *ctrl, struct notifier_block *nb)
1076{
1077 int rc;
1078
1079 if (IS_ERR_OR_NULL(ctrl))
1080 return -EINVAL;
1081
1082 rc = blocking_notifier_chain_register(&ctrl->bdev->bus_change_notifier,
1083 nb);
1084 if (rc)
1085 pr_err("Notifier registration failed, rc=%d\n", rc);
1086
1087 return rc;
1088}
1089EXPORT_SYMBOL(bif_ctrl_notifier_register);
1090
1091/**
1092 * bif_ctrl_notifier_unregister() - unregister a battery status change notifier
1093 * block that was previously registered
1094 * @ctrl: BIF controller consumer handle
1095 *
1096 * Returns 0 for success or errno if an error occurred.
1097 */
1098int bif_ctrl_notifier_unregister(struct bif_ctrl *ctrl,
1099 struct notifier_block *nb)
1100{
1101 int rc;
1102
1103 if (IS_ERR_OR_NULL(ctrl))
1104 return -EINVAL;
1105
1106 rc =
1107 blocking_notifier_chain_unregister(&ctrl->bdev->bus_change_notifier,
1108 nb);
1109 if (rc)
1110 pr_err("Notifier unregistration failed, rc=%d\n", rc);
1111
1112 return rc;
1113}
1114EXPORT_SYMBOL(bif_ctrl_notifier_unregister);
1115
1116/**
1117 * bif_get_bus_handle() - returns the BIF controller consumer handle associated
1118 * with a BIF slave handle
1119 * @slave: BIF slave handle
1120 *
1121 * Note, bif_ctrl_put() should never be called for the pointer output by
1122 * bif_get_bus_handle().
1123 */
1124struct bif_ctrl *bif_get_bus_handle(struct bif_slave *slave)
1125{
1126 if (IS_ERR_OR_NULL(slave))
1127 return ERR_PTR(-EINVAL);
1128
1129 return &slave->ctrl;
1130}
1131EXPORT_SYMBOL(bif_get_bus_handle);
1132
1133/**
1134 * bif_ctrl_count() - returns the number of registered BIF controllers
1135 */
1136int bif_ctrl_count(void)
1137{
1138 struct bif_ctrl_dev *bdev;
1139 int count = 0;
1140
1141 mutex_lock(&bif_ctrl_list_mutex);
1142
1143 list_for_each_entry(bdev, &bif_ctrl_list, list) {
1144 count++;
1145 }
1146 mutex_unlock(&bif_ctrl_list_mutex);
1147
1148 return count;
1149}
1150EXPORT_SYMBOL(bif_ctrl_count);
1151
1152/**
1153 * bif_ctrl_get_by_id() - get a handle for the id'th BIF controller registered
1154 * in the system
1155 * @id: Arbitrary number associated with the BIF bus in the system
1156 *
1157 * id must be in the range [0, bif_ctrl_count() - 1]. This function should only
1158 * need to be called by a BIF consumer that is unable to link to a given BIF
1159 * controller via a device tree binding.
1160 *
1161 * Returns a BIF controller consumer handle if successful or an ERR_PTR if not.
1162 */
1163struct bif_ctrl *bif_ctrl_get_by_id(unsigned int id)
1164{
1165 struct bif_ctrl_dev *bdev;
1166 struct bif_ctrl_dev *bdev_found = NULL;
1167 struct bif_ctrl *ctrl = ERR_PTR(-ENODEV);
1168
1169 mutex_lock(&bif_ctrl_list_mutex);
1170
1171 list_for_each_entry(bdev, &bif_ctrl_list, list) {
1172 if (id == 0) {
1173 bdev_found = bdev;
1174 break;
1175 }
1176 id--;
1177 }
1178 mutex_unlock(&bif_ctrl_list_mutex);
1179
1180 if (bdev_found) {
1181 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1182 if (!ctrl) {
1183 pr_err("Bus handle allocation failed\n");
1184 ctrl = ERR_PTR(-ENOMEM);
1185 } else {
1186 ctrl->bdev = bdev_found;
1187 }
1188 }
1189
1190 return ctrl;
1191}
1192EXPORT_SYMBOL(bif_ctrl_get_by_id);
1193
1194/**
1195 * bif_ctrl_get() - get a handle for the BIF controller that is linked to the
1196 * consumer device in the device tree
1197 * @consumer_dev: Pointer to the consumer's device
1198 *
1199 * In order to use this function, the BIF consumer's device must specify the
1200 * "qcom,bif-ctrl" property in its device tree node which points to a BIF
1201 * controller device node.
1202 *
1203 * Returns a BIF controller consumer handle if successful or an ERR_PTR if not.
1204 * If the BIF controller linked to the consumer device has not yet probed, then
1205 * ERR_PTR(-EPROBE_DEFER) is returned.
1206 */
1207struct bif_ctrl *bif_ctrl_get(struct device *consumer_dev)
1208{
1209 struct device_node *ctrl_node = NULL;
1210 struct bif_ctrl_dev *bdev_found = NULL;
1211 struct bif_ctrl *ctrl = ERR_PTR(-EPROBE_DEFER);
1212 struct bif_ctrl_dev *bdev = NULL;
1213
1214 if (!consumer_dev || !consumer_dev->of_node) {
1215 pr_err("Invalid device node\n");
1216 return ERR_PTR(-EINVAL);
1217 }
1218
1219 ctrl_node = of_parse_phandle(consumer_dev->of_node, "qcom,bif-ctrl", 0);
1220 if (!ctrl_node) {
1221 pr_err("Could not find qcom,bif-ctrl property in %s\n",
1222 consumer_dev->of_node->full_name);
1223 return ERR_PTR(-ENXIO);
1224 }
1225
1226 mutex_lock(&bif_ctrl_list_mutex);
1227 list_for_each_entry(bdev, &bif_ctrl_list, list) {
1228 if (bdev->ctrl_dev && bdev->ctrl_dev->of_node == ctrl_node) {
1229 bdev_found = bdev;
1230 break;
1231 }
1232 }
1233 mutex_unlock(&bif_ctrl_list_mutex);
1234
1235 if (bdev_found) {
1236 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1237 if (!ctrl) {
1238 pr_err("Bus handle allocation failed\n");
1239 ctrl = ERR_PTR(-ENOMEM);
1240 } else {
1241 ctrl->bdev = bdev_found;
1242 }
1243 }
1244
1245 return ctrl;
1246}
1247EXPORT_SYMBOL(bif_ctrl_get);
1248
1249/**
1250 * bif_ctrl_put() - frees a BIF controller handle
1251 * @ctrl: BIF controller consumer handle
1252 */
1253void bif_ctrl_put(struct bif_ctrl *ctrl)
1254{
1255 if (!IS_ERR_OR_NULL(ctrl) && ctrl->exclusive_lock)
1256 mutex_unlock(&ctrl->bdev->mutex);
1257 kfree(ctrl);
1258}
1259EXPORT_SYMBOL(bif_ctrl_put);
1260
1261/*
1262 * Returns true if all parameters are matched, otherwise false.
1263 * function_type and function_version mean that their exists some function in
1264 * the slave which has the specified type and subtype. ctrl == NULL is treated
1265 * as a wildcard.
1266 */
1267static bool bif_slave_match(const struct bif_ctrl *ctrl,
1268 struct bif_slave_dev *sdev, const struct bif_match_criteria *criteria)
1269{
1270 int i, type, version;
1271
1272 if (ctrl && (ctrl->bdev != sdev->bdev))
1273 return false;
1274
1275 if (!sdev->present
1276 && (!(criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE)
1277 || ((criteria->match_mask & BIF_MATCH_IGNORE_PRESENCE)
1278 && !criteria->ignore_presence)))
1279 return false;
1280
1281 if ((criteria->match_mask & BIF_MATCH_MANUFACTURER_ID)
1282 && sdev->l1_data.manufacturer_id != criteria->manufacturer_id)
1283 return false;
1284
1285 if ((criteria->match_mask & BIF_MATCH_PRODUCT_ID)
1286 && sdev->l1_data.product_id != criteria->product_id)
1287 return false;
1288
1289 if (criteria->match_mask & BIF_MATCH_FUNCTION_TYPE) {
1290 if (!sdev->function_directory)
1291 return false;
1292 for (i = 0; i < sdev->l1_data.length / 4; i++) {
1293 type = sdev->function_directory[i].function_type;
1294 version = sdev->function_directory[i].function_version;
1295 if (type == criteria->function_type &&
1296 (version == criteria->function_version
1297 || !(criteria->match_mask
1298 & BIF_MATCH_FUNCTION_VERSION)))
1299 return true;
1300 }
1301 return false;
1302 }
1303
1304 return true;
1305}
1306
1307/**
1308 * bif_slave_match_count() - returns the number of slaves associated with the
1309 * specified BIF controller which fit the matching
1310 * criteria
1311 * @ctrl: BIF controller consumer handle
1312 * @match_criteria: Matching criteria used to filter slaves
1313 */
1314int bif_slave_match_count(const struct bif_ctrl *ctrl,
1315 const struct bif_match_criteria *match_criteria)
1316{
1317 struct bif_slave_dev *sdev;
1318 int count = 0;
1319
1320 mutex_lock(&bif_sdev_list_mutex);
1321
1322 list_for_each_entry(sdev, &bif_sdev_list, list) {
1323 if (bif_slave_match(ctrl, sdev, match_criteria))
1324 count++;
1325 }
1326
1327 mutex_unlock(&bif_sdev_list_mutex);
1328
1329 return count;
1330}
1331EXPORT_SYMBOL(bif_slave_match_count);
1332
1333/**
1334 * bif_slave_match_get() - get a slave handle for the id'th slave associated
1335 * with the specified BIF controller which fits the
1336 * matching criteria
1337 * @ctrl: BIF controller consumer handle
1338 * @id: Index into the set of matching slaves
1339 * @match_criteria: Matching criteria used to filter slaves
1340 *
1341 * id must be in the range [0, bif_slave_match_count(ctrl, match_criteria) - 1].
1342 *
1343 * Returns a BIF slave handle if successful or an ERR_PTR if not.
1344 */
1345struct bif_slave *bif_slave_match_get(const struct bif_ctrl *ctrl,
1346 unsigned int id, const struct bif_match_criteria *match_criteria)
1347{
1348 struct bif_slave_dev *sdev;
1349 struct bif_slave *slave = ERR_PTR(-ENODEV);
1350 struct bif_slave_dev *sdev_found = NULL;
1351 int count = 0;
1352
1353 mutex_lock(&bif_sdev_list_mutex);
1354
1355 list_for_each_entry(sdev, &bif_sdev_list, list) {
1356 if (bif_slave_match(ctrl, sdev, match_criteria))
1357 count++;
1358 if (count == id + 1) {
1359 sdev_found = sdev;
1360 break;
1361 }
1362 }
1363
1364 mutex_unlock(&bif_sdev_list_mutex);
1365
1366 if (sdev_found) {
1367 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1368 if (!slave) {
1369 pr_err("Slave allocation failed\n");
1370 slave = ERR_PTR(-ENOMEM);
1371 } else {
1372 slave->sdev = sdev_found;
1373 slave->ctrl.bdev = sdev_found->bdev;
1374 }
1375 }
1376
1377 return slave;
1378}
1379EXPORT_SYMBOL(bif_slave_match_get);
1380
1381/**
1382 * bif_slave_put() - frees a BIF slave handle
1383 * @slave: BIF slave handle
1384 */
1385void bif_slave_put(struct bif_slave *slave)
1386{
1387 if (!IS_ERR_OR_NULL(slave) && slave->ctrl.exclusive_lock)
1388 mutex_unlock(&slave->sdev->bdev->mutex);
1389 kfree(slave);
1390}
1391EXPORT_SYMBOL(bif_slave_put);
1392
1393/**
1394 * bif_slave_find_function() - get the function pointer and version of a
1395 * BIF function if it is present on the specified slave
1396 * @slave: BIF slave handle
1397 * @function: BIF function to search for inside of the slave
1398 * @version: If the function is found, then 'version' is set to the
1399 * version value of the function
1400 * @function_pointer: If the function is found, then 'function_pointer' is set
1401 * to the BIF slave address of the function
1402 *
1403 * Returns 0 for success or errno if an error occurred. If the function is not
1404 * found in the slave, then -ENODEV is returned.
1405 */
1406int bif_slave_find_function(struct bif_slave *slave, u8 function, u8 *version,
1407 u16 *function_pointer)
1408{
1409 int rc = -ENODEV;
1410 struct bif_ddb_l2_data *func;
1411 int i;
1412
1413 if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(version)
1414 || IS_ERR_OR_NULL(function_pointer)) {
1415 pr_err("Invalid pointer input.\n");
1416 return -EINVAL;
1417 }
1418
1419 func = slave->sdev->function_directory;
1420
1421 for (i = 0; i < slave->sdev->l1_data.length / 4; i++) {
1422 if (function == func[i].function_type) {
1423 *version = func[i].function_version;
1424 *function_pointer = func[i].function_pointer;
1425 rc = 0;
1426 break;
1427 }
1428 }
1429
1430 return rc;
1431}
1432EXPORT_SYMBOL(bif_slave_find_function);
1433
1434/**
1435 * bif_slave_read() - read contiguous memory values from a BIF slave
1436 * @slave: BIF slave handle
1437 * @addr: BIF slave address to begin reading at
1438 * @buf: Buffer to fill with memory values
1439 * @len: Number of byte to read
1440 *
1441 * Returns 0 for success or errno if an error occurred.
1442 */
1443int bif_slave_read(struct bif_slave *slave, u16 addr, u8 *buf, int len)
1444{
1445 int rc;
1446
1447 if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) {
1448 pr_err("Invalid pointer input.\n");
1449 return -EINVAL;
1450 }
1451
1452 bif_slave_ctrl_lock(slave);
1453
1454 rc = _bif_slave_read(slave->sdev, addr, buf, len);
1455 if (rc)
1456 pr_err("BIF slave read failed, rc=%d\n", rc);
1457
1458 bif_slave_ctrl_unlock(slave);
1459
1460 return rc;
1461}
1462EXPORT_SYMBOL(bif_slave_read);
1463
1464/**
1465 * bif_slave_write() - write contiguous memory values to a BIF slave
1466 * @slave: BIF slave handle
1467 * @addr: BIF slave address to begin writing at
1468 * @buf: Buffer containing values to write
1469 * @len: Number of byte to write
1470 *
1471 * Returns 0 for success or errno if an error occurred.
1472 */
1473int bif_slave_write(struct bif_slave *slave, u16 addr, u8 *buf, int len)
1474{
1475 int rc;
1476
1477 if (IS_ERR_OR_NULL(slave) || IS_ERR_OR_NULL(buf)) {
1478 pr_err("Invalid pointer input.\n");
1479 return -EINVAL;
1480 }
1481
1482 bif_slave_ctrl_lock(slave);
1483
1484 rc = _bif_slave_write(slave->sdev, addr, buf, len);
1485 if (rc)
1486 pr_err("BIF slave write failed, rc=%d\n", rc);
1487
1488 bif_slave_ctrl_unlock(slave);
1489
1490 return rc;
1491}
1492EXPORT_SYMBOL(bif_slave_write);
1493
1494/**
1495 * bif_slave_is_present() - check if a slave is currently physically present
1496 * in the system
1497 * @slave: BIF slave handle
1498 *
1499 * Returns 1 if the slave is present, 0 if the slave is not present, or errno
1500 * if an error occurred.
1501 *
1502 * This function can be used by BIF consumer drivers to check if their slave
1503 * handles are still meaningful after battery reinsertion.
1504 */
1505int bif_slave_is_present(struct bif_slave *slave)
1506{
1507 if (IS_ERR_OR_NULL(slave)) {
1508 pr_err("Invalid pointer input.\n");
1509 return -EINVAL;
1510 }
1511
1512 return slave->sdev->present;
1513}
1514EXPORT_SYMBOL(bif_slave_is_present);
1515
1516/**
1517 * bif_slave_is_selected() - check if a slave is currently selected on the BIF
1518 * bus
1519 * @slave: BIF slave handle
1520 *
1521 * Returns 1 if the slave is selected, 0 if the slave is not selected, or errno
1522 * if an error occurred.
1523 *
1524 * This function should not be required under normal circumstances since the
1525 * bif-core framework ensures that slaves are always selected when needed.
1526 * It would be most useful when used as a helper in conjunction with
1527 * bif_ctrl_bus_lock() and the raw transaction functions.
1528 */
1529int bif_slave_is_selected(struct bif_slave *slave)
1530{
1531 int rc;
1532
1533 if (IS_ERR_OR_NULL(slave)) {
1534 pr_err("Invalid pointer input.\n");
1535 return -EINVAL;
1536 }
1537
1538 if (slave->sdev->bdev->selected_sdev != slave->sdev)
1539 return false;
1540
1541 bif_slave_ctrl_lock(slave);
1542 rc = bif_is_slave_selected(slave->sdev->bdev);
1543 bif_slave_ctrl_unlock(slave);
1544
1545 return rc;
1546}
1547EXPORT_SYMBOL(bif_slave_is_selected);
1548
1549/**
1550 * bif_slave_select() - select a slave on the BIF bus
1551 * @slave: BIF slave handle
1552 *
1553 * Returns 0 on success or errno if an error occurred.
1554 *
1555 * This function should not be required under normal circumstances since the
1556 * bif-core framework ensures that slaves are always selected when needed.
1557 * It would be most useful when used as a helper in conjunction with
1558 * bif_ctrl_bus_lock() and the raw transaction functions.
1559 */
1560int bif_slave_select(struct bif_slave *slave)
1561{
1562 int rc;
1563
1564 if (IS_ERR_OR_NULL(slave)) {
1565 pr_err("Invalid pointer input.\n");
1566 return -EINVAL;
1567 }
1568
1569 bif_slave_ctrl_lock(slave);
1570 slave->sdev->bdev->selected_sdev = NULL;
1571 rc = bif_select_slave(slave->sdev);
1572 bif_slave_ctrl_unlock(slave);
1573
1574 return rc;
1575}
1576EXPORT_SYMBOL(bif_slave_select);
1577
1578/**
1579 * bif_ctrl_raw_transaction() - perform a raw BIF transaction on the bus which
1580 * expects no slave response
1581 * @ctrl: BIF controller consumer handle
1582 * @transaction: BIF transaction to carry out. This should be one of the
1583 * values in enum bif_transaction.
1584 * @data: 8-bit data to use in the transaction. The meaning of
1585 * this data depends upon the transaction that is to be
1586 * performed.
1587 *
1588 * When performing a bus command (BC) transaction, values in enum
1589 * bif_bus_command may be used for the data parameter. Additional manufacturer
1590 * specific values may also be used in a BC transaction.
1591 *
1592 * Returns 0 on success or errno if an error occurred.
1593 *
1594 * This function should only need to be used when BIF transactions are required
1595 * that are not handled by the bif-core directly.
1596 */
1597int bif_ctrl_raw_transaction(struct bif_ctrl *ctrl, int transaction, u8 data)
1598{
1599 int rc;
1600
1601 if (IS_ERR_OR_NULL(ctrl)) {
1602 pr_err("Invalid pointer input.\n");
1603 return -EINVAL;
1604 }
1605
1606 bif_ctrl_lock(ctrl);
1607
1608 rc = ctrl->bdev->desc->ops->bus_transaction(ctrl->bdev, transaction,
1609 data);
1610 if (rc)
1611 pr_err("BIF bus transaction failed, rc=%d\n", rc);
1612
1613 bif_ctrl_unlock(ctrl);
1614
1615 return rc;
1616}
1617EXPORT_SYMBOL(bif_ctrl_raw_transaction);
1618
1619/**
1620 * bif_ctrl_raw_transaction_read() - perform a raw BIF transaction on the bus
1621 * which expects an RD or TACK slave response word
1622 * @ctrl: BIF controller consumer handle
1623 * @transaction: BIF transaction to carry out. This should be one of the
1624 * values in enum bif_transaction.
1625 * @data: 8-bit data to use in the transaction. The meaning of
1626 * this data depends upon the transaction that is to be
1627 * performed.
1628 * @response: Pointer to an integer which is filled with the 11-bit
1629 * slave response word upon success. The 11-bit format is
1630 * (MSB to LSB) BCF, ACK, EOT, D7-D0.
1631 *
1632 * When performing a bus command (BC) transaction, values in enum
1633 * bif_bus_command may be used for the data parameter. Additional manufacturer
1634 * specific values may also be used in a BC transaction.
1635 *
1636 * Returns 0 on success or errno if an error occurred.
1637 *
1638 * This function should only need to be used when BIF transactions are required
1639 * that are not handled by the bif-core directly.
1640 */
1641int bif_ctrl_raw_transaction_read(struct bif_ctrl *ctrl, int transaction,
1642 u8 data, int *response)
1643{
1644 int rc;
1645
1646 if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(response)) {
1647 pr_err("Invalid pointer input.\n");
1648 return -EINVAL;
1649 }
1650
1651 bif_ctrl_lock(ctrl);
1652
1653 rc = ctrl->bdev->desc->ops->bus_transaction_read(ctrl->bdev,
1654 transaction, data, response);
1655 if (rc)
1656 pr_err("BIF bus transaction failed, rc=%d\n", rc);
1657
1658 bif_ctrl_unlock(ctrl);
1659
1660 return rc;
1661}
1662EXPORT_SYMBOL(bif_ctrl_raw_transaction_read);
1663
1664/**
1665 * bif_ctrl_raw_transaction_query() - perform a raw BIF transaction on the bus
1666 * which expects a BQ slave response
1667 * @ctrl: BIF controller consumer handle
1668 * @transaction: BIF transaction to carry out. This should be one of the
1669 * values in enum bif_transaction.
1670 * @data: 8-bit data to use in the transaction. The meaning of
1671 * this data depends upon the transaction that is to be
1672 * performed.
1673 * @query_response: Pointer to boolean which is set to true if a BQ pulse
1674 * is receieved, or false if no BQ pulse is received before
1675 * timing out.
1676 *
1677 * When performing a bus command (BC) transaction, values in enum
1678 * bif_bus_command may be used for the data parameter. Additional manufacturer
1679 * specific values may also be used in a BC transaction.
1680 *
1681 * Returns 0 on success or errno if an error occurred.
1682 *
1683 * This function should only need to be used when BIF transactions are required
1684 * that are not handled by the bif-core directly.
1685 */
1686int bif_ctrl_raw_transaction_query(struct bif_ctrl *ctrl, int transaction,
1687 u8 data, bool *query_response)
1688{
1689 int rc;
1690
1691 if (IS_ERR_OR_NULL(ctrl) || IS_ERR_OR_NULL(query_response)) {
1692 pr_err("Invalid pointer input.\n");
1693 return -EINVAL;
1694 }
1695
1696 bif_ctrl_lock(ctrl);
1697
1698 rc = ctrl->bdev->desc->ops->bus_transaction_query(ctrl->bdev,
1699 transaction, data, query_response);
1700 if (rc)
1701 pr_err("BIF bus transaction failed, rc=%d\n", rc);
1702
1703 bif_ctrl_unlock(ctrl);
1704
1705 return rc;
1706}
1707EXPORT_SYMBOL(bif_ctrl_raw_transaction_query);
1708
1709/**
1710 * bif_ctrl_bus_lock() - lock the BIF bus of a controller for exclusive access
1711 * @ctrl: BIF controller consumer handle
1712 *
1713 * This function should only need to be called in circumstances where a BIF
1714 * consumer is issuing special BIF bus commands that have strict ordering
1715 * requirements.
1716 */
1717void bif_ctrl_bus_lock(struct bif_ctrl *ctrl)
1718{
1719 if (IS_ERR_OR_NULL(ctrl)) {
1720 pr_err("Invalid controller handle.\n");
1721 return;
1722 }
1723
1724 if (ctrl->exclusive_lock) {
1725 pr_err("BIF bus exclusive lock already held\n");
1726 return;
1727 }
1728
1729 mutex_lock(&ctrl->bdev->mutex);
1730 ctrl->exclusive_lock = true;
1731 bif_cancel_irq_mode_work(ctrl->bdev);
1732}
1733EXPORT_SYMBOL(bif_ctrl_bus_lock);
1734
1735/**
1736 * bif_ctrl_bus_unlock() - lock the BIF bus of a controller that was previously
1737 * locked for exclusive access
1738 * @ctrl: BIF controller consumer handle
1739 *
1740 * This function must only be called after first calling bif_ctrl_bus_lock().
1741 */
1742void bif_ctrl_bus_unlock(struct bif_ctrl *ctrl)
1743{
1744 if (IS_ERR_OR_NULL(ctrl)) {
1745 pr_err("Invalid controller handle.\n");
1746 return;
1747 }
1748
1749 if (!ctrl->exclusive_lock) {
1750 pr_err("BIF bus exclusive lock not already held\n");
1751 return;
1752 }
1753
1754 ctrl->exclusive_lock = false;
1755 bif_schedule_irq_mode_work(ctrl->bdev);
1756 mutex_unlock(&ctrl->bdev->mutex);
1757}
1758EXPORT_SYMBOL(bif_ctrl_bus_unlock);
1759
1760/**
1761 * bif_ctrl_measure_rid() - measure the battery pack Rid pull-down resistance
1762 * in ohms
1763 * @ctrl: BIF controller consumer handle
1764 *
1765 * Returns the resistance of the Rid resistor in ohms if successful or errno
1766 * if an error occurred.
1767 */
1768int bif_ctrl_measure_rid(struct bif_ctrl *ctrl)
1769{
1770 int rc;
1771
1772 if (IS_ERR_OR_NULL(ctrl)) {
1773 pr_err("Invalid controller handle.\n");
1774 return -ENODEV;
1775 }
1776
1777 if (!ctrl->bdev->desc->ops->get_battery_rid) {
1778 pr_err("Cannot measure Rid.\n");
1779 return -ENXIO;
1780 }
1781
1782 bif_ctrl_lock(ctrl);
1783
1784 rc = ctrl->bdev->desc->ops->get_battery_rid(ctrl->bdev);
1785 if (rc < 0)
1786 pr_err("Error during Rid measurement, rc=%d\n", rc);
1787
1788 bif_ctrl_unlock(ctrl);
1789
1790 return rc;
1791}
1792EXPORT_SYMBOL(bif_ctrl_measure_rid);
1793
1794/**
1795 * bif_ctrl_get_bus_period() - get the BIF bus period (tau_bif) in nanoseconds
1796 * @ctrl: BIF controller consumer handle
1797 *
1798 * Returns the currently configured bus period in nanoseconds if successful or
1799 * errno if an error occurred.
1800 */
1801int bif_ctrl_get_bus_period(struct bif_ctrl *ctrl)
1802{
1803 int rc;
1804
1805 if (IS_ERR_OR_NULL(ctrl)) {
1806 pr_err("Invalid controller handle.\n");
1807 return -ENODEV;
1808 }
1809
1810 if (!ctrl->bdev->desc->ops->get_bus_period) {
1811 pr_err("Cannot get the BIF bus period.\n");
1812 return -ENXIO;
1813 }
1814
1815 rc = ctrl->bdev->desc->ops->get_bus_period(ctrl->bdev);
1816 if (rc < 0)
1817 pr_err("Error during bus period retrieval, rc=%d\n", rc);
1818
1819 return rc;
1820}
1821EXPORT_SYMBOL(bif_ctrl_get_bus_period);
1822
1823/**
1824 * bif_ctrl_set_bus_period() - set the BIF bus period (tau_bif) in nanoseconds
1825 * @ctrl: BIF controller consumer handle
1826 * @period_ns: BIF bus period in nanoseconds to use
1827 *
1828 * If the exact period is not supported by the BIF controller hardware, then the
1829 * next larger supported period will be used.
1830 *
1831 * Returns 0 on success or errno if an error occurred.
1832 */
1833int bif_ctrl_set_bus_period(struct bif_ctrl *ctrl, int period_ns)
1834{
1835 int rc;
1836
1837 if (IS_ERR_OR_NULL(ctrl)) {
1838 pr_err("Invalid controller handle.\n");
1839 return -ENODEV;
1840 }
1841
1842 if (!ctrl->bdev->desc->ops->set_bus_period) {
1843 pr_err("Cannot set the BIF bus period.\n");
1844 return -ENXIO;
1845 }
1846
1847 bif_ctrl_lock(ctrl);
1848 rc = ctrl->bdev->desc->ops->set_bus_period(ctrl->bdev, period_ns);
1849 if (rc)
1850 pr_err("Error during bus period configuration, rc=%d\n", rc);
1851 bif_ctrl_unlock(ctrl);
1852
1853 return rc;
1854}
1855EXPORT_SYMBOL(bif_ctrl_set_bus_period);
1856
1857/**
1858 * bif_ctrl_get_bus_state() - get the current state of the BIF bus
1859 * @ctrl: BIF controller consumer handle
1860 *
1861 * Returns a bus state from enum bif_bus_state if successful or errno if an
1862 * error occurred.
1863 */
1864int bif_ctrl_get_bus_state(struct bif_ctrl *ctrl)
1865{
1866 int rc;
1867
1868 if (IS_ERR_OR_NULL(ctrl)) {
1869 pr_err("Invalid controller handle.\n");
1870 return -ENODEV;
1871 }
1872
1873 rc = ctrl->bdev->desc->ops->get_bus_state(ctrl->bdev);
1874 if (rc < 0)
1875 pr_err("Error during bus state retrieval, rc=%d\n", rc);
1876
1877 return rc;
1878}
1879EXPORT_SYMBOL(bif_ctrl_get_bus_state);
1880
1881/**
1882 * bif_ctrl_set_bus_state() - set the state of the BIF bus
1883 * @ctrl: BIF controller consumer handle
1884 * @state: State for the BIF bus to enter
1885 *
1886 * Returns 0 on success or errno if an error occurred.
1887 */
1888int bif_ctrl_set_bus_state(struct bif_ctrl *ctrl, enum bif_bus_state state)
1889{
1890 int rc;
1891
1892 if (IS_ERR_OR_NULL(ctrl)) {
1893 pr_err("Invalid controller handle.\n");
1894 return -ENODEV;
1895 }
1896
1897 bif_ctrl_lock(ctrl);
1898
1899 rc = ctrl->bdev->desc->ops->set_bus_state(ctrl->bdev, state);
1900 if (rc < 0)
1901 pr_err("Error during bus state configuration, rc=%d\n", rc);
1902
1903 /*
1904 * Uncache the selected slave if the new bus state results in the slave
1905 * becoming unselected.
1906 */
1907 if (state == BIF_BUS_STATE_MASTER_DISABLED
1908 || state == BIF_BUS_STATE_POWER_DOWN
1909 || state == BIF_BUS_STATE_STANDBY)
1910 ctrl->bdev->selected_sdev = NULL;
1911
1912 bif_ctrl_unlock(ctrl);
1913
1914 return rc;
1915}
1916EXPORT_SYMBOL(bif_ctrl_set_bus_state);
1917
1918/*
1919 * Check if the specified function is a protocol function and if it is, then
1920 * instantiate protocol function data for the slave.
1921 */
1922static int bif_initialize_protocol_function(struct bif_slave_dev *sdev,
1923 struct bif_ddb_l2_data *func)
1924{
1925 int rc = 0;
1926 u8 buf[4];
1927
1928 /* Ensure that this is a protocol function. */
1929 if (func->function_type != BIF_FUNC_PROTOCOL)
1930 return 0;
1931
1932 if (sdev->protocol_function) {
1933 pr_err("Duplicate protocol function found for BIF slave; DEV_ADR=0x%02X\n",
1934 sdev->slave_addr);
1935 return -EPERM;
1936 }
1937
1938 sdev->protocol_function = kzalloc(sizeof(struct bif_protocol_function),
1939 GFP_KERNEL);
1940 if (!sdev->protocol_function) {
1941 pr_err("out of memory\n");
1942 return -ENOMEM;
1943 }
1944
1945 rc = _bif_slave_read(sdev, func->function_pointer, buf, 4);
1946 if (rc) {
1947 pr_err("Protocol function data read failed, rc=%d\n", rc);
1948 return rc;
1949 }
1950
1951 sdev->protocol_function->protocol_pointer = buf[0] << 8 | buf[1];
1952 sdev->protocol_function->device_id_pointer = buf[2] << 8 | buf[3];
1953 sdev->protocol_function->l2_entry = func;
1954
1955 rc = _bif_slave_read(sdev, sdev->protocol_function->device_id_pointer,
1956 sdev->protocol_function->device_id, BIF_DEVICE_ID_BYTE_LENGTH);
1957 if (rc) {
1958 pr_err("Device ID read failed, rc=%d\n", rc);
1959 return rc;
1960 }
1961
1962 /* Check if this slave does not have a UID value stored. */
1963 if (sdev->unique_id_bits_known == 0) {
1964 sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH;
1965 /* Fill in UID using manufacturer ID and device ID. */
1966 sdev->unique_id[0] = sdev->l1_data.manufacturer_id >> 8;
1967 sdev->unique_id[1] = sdev->l1_data.manufacturer_id;
1968 memcpy(&sdev->unique_id[2],
1969 sdev->protocol_function->device_id,
1970 BIF_DEVICE_ID_BYTE_LENGTH);
1971 }
1972
1973 return rc;
1974}
1975
1976/*
1977 * Check if the specified function is a slave control function and if it is,
1978 * then instantiate slave control function data for the slave.
1979 */
1980static int bif_initialize_slave_control_function(struct bif_slave_dev *sdev,
1981 struct bif_ddb_l2_data *func)
1982{
1983 int rc = 0;
1984 int i;
1985 u8 buf[3];
1986
1987 /* Ensure that this is a slave control function. */
1988 if (func->function_type != BIF_FUNC_SLAVE_CONTROL)
1989 return 0;
1990
1991 if (sdev->slave_ctrl_function) {
1992 pr_err("Duplicate slave control function found for BIF slave; DEV_ADR=0x%02X\n",
1993 sdev->slave_addr);
1994 return -EPERM;
1995 }
1996
1997 sdev->slave_ctrl_function
1998 = kzalloc(sizeof(struct bif_protocol_function), GFP_KERNEL);
1999 if (!sdev->slave_ctrl_function) {
2000 pr_err("out of memory\n");
2001 return -ENOMEM;
2002 }
2003
2004 rc = _bif_slave_read(sdev, func->function_pointer, buf, 3);
2005 if (rc) {
2006 pr_err("Slave control function data read failed, rc=%d\n", rc);
2007 return rc;
2008 }
2009
2010 sdev->slave_ctrl_function->slave_ctrl_pointer = buf[0] << 8 | buf[1];
2011 sdev->slave_ctrl_function->task_count
2012 = buf[2] * SLAVE_CTRL_TASKS_PER_SET;
2013 sdev->slave_ctrl_function->l2_entry = func;
2014
2015 if (sdev->slave_ctrl_function->task_count > 0) {
2016 sdev->slave_ctrl_function->irq_notifier_list =
2017 kzalloc(sizeof(struct blocking_notifier_head)
2018 * sdev->slave_ctrl_function->task_count,
2019 GFP_KERNEL);
2020 if (!sdev->slave_ctrl_function->irq_notifier_list) {
2021 pr_err("out of memory\n");
2022 kfree(sdev->slave_ctrl_function);
2023 return -ENOMEM;
2024 }
2025
2026 for (i = 0; i < sdev->slave_ctrl_function->task_count; i++) {
2027 BLOCKING_INIT_NOTIFIER_HEAD(
2028 &sdev->slave_ctrl_function->irq_notifier_list[i]);
2029 }
2030 }
2031
2032 return rc;
2033}
2034
2035/**
2036 * bif_crc_ccitt() - calculate the CRC-CCITT CRC value of the data specified
2037 * @buffer: Data to calculate the CRC of
2038 * @len: Length of the data buffer in bytes
2039 *
2040 * MIPI-BIF specifies the usage of CRC-CCITT for BIF data objects. This
2041 * function performs the CRC calculation while taking into account the bit
2042 * ordering used by BIF.
2043 */
2044u16 bif_crc_ccitt(const u8 *buffer, unsigned int len)
2045{
2046 u16 crc = 0xFFFF;
2047
2048 while (len--) {
2049 crc = crc_ccitt_byte(crc, bitrev8(*buffer));
2050 buffer++;
2051 }
2052 return bitrev16(crc);
2053}
2054EXPORT_SYMBOL(bif_crc_ccitt);
2055
2056static u16 bif_object_crc_ccitt(const struct bif_object *object)
2057{
2058 u16 crc = 0xFFFF;
2059 int i;
2060
2061 crc = crc_ccitt_byte(crc, bitrev8(object->type));
2062 crc = crc_ccitt_byte(crc, bitrev8(object->version));
2063 crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id >> 8));
2064 crc = crc_ccitt_byte(crc, bitrev8(object->manufacturer_id));
2065 crc = crc_ccitt_byte(crc, bitrev8(object->length >> 8));
2066 crc = crc_ccitt_byte(crc, bitrev8(object->length));
2067
2068 for (i = 0; i < object->length - 8; i++)
2069 crc = crc_ccitt_byte(crc, bitrev8(object->data[i]));
2070
2071 return bitrev16(crc);
2072}
2073
2074/*
2075 * Check if the specified function is an NVM function and if it is, then
2076 * instantiate NVM function data for the slave and read all objects.
2077 */
2078static int bif_initialize_nvm_function(struct bif_slave_dev *sdev,
2079 struct bif_ddb_l2_data *func)
2080{
2081 int rc = 0;
2082 int data_len;
2083 u8 buf[8], object_type;
2084 struct bif_object *object;
2085 struct bif_object *temp;
2086 u16 addr;
2087 u16 crc;
2088
2089 /* Ensure that this is an NVM function. */
2090 if (func->function_type != BIF_FUNC_NVM)
2091 return 0;
2092
2093 if (sdev->nvm_function) {
2094 pr_err("Duplicate NVM function found for BIF slave; DEV_ADR=0x%02X\n",
2095 sdev->slave_addr);
2096 return -EPERM;
2097 }
2098
2099 sdev->nvm_function
2100 = kzalloc(sizeof(*sdev->nvm_function), GFP_KERNEL);
2101 if (!sdev->nvm_function) {
2102 pr_err("out of memory\n");
2103 return -ENOMEM;
2104 }
2105
2106 rc = _bif_slave_read(sdev, func->function_pointer, buf, 8);
2107 if (rc) {
2108 pr_err("NVM function data read failed, rc=%d\n", rc);
2109 return rc;
2110 }
2111
2112 sdev->nvm_function->nvm_pointer = buf[0] << 8 | buf[1];
2113 sdev->nvm_function->slave_control_channel = buf[2];
2114 sdev->nvm_function->write_buffer_size = buf[3];
2115 sdev->nvm_function->nvm_base_address = buf[4] << 8 | buf[5];
2116 sdev->nvm_function->nvm_size = buf[6] << 8 | buf[7];
2117
2118 INIT_LIST_HEAD(&sdev->nvm_function->object_list);
2119
2120 /* Read object list */
2121 addr = sdev->nvm_function->nvm_base_address;
2122 rc = _bif_slave_read(sdev, addr, &object_type, 1);
2123 if (rc) {
2124 pr_err("Slave memory read failed, rc=%d\n", rc);
2125 return rc;
2126 }
2127
2128 /* Object type == 0x00 corresponds to the end of the object list. */
2129 while (object_type != 0x00) {
2130 object = kzalloc(sizeof(*object), GFP_KERNEL);
2131 if (!object) {
2132 pr_err("out of memory\n");
2133 rc = -ENOMEM;
2134 goto free_data;
2135 }
2136 list_add_tail(&object->list, &sdev->nvm_function->object_list);
2137
2138 rc = _bif_slave_read(sdev, addr + 1, buf + 1, 5);
2139 if (rc) {
2140 pr_err("Slave memory read of object header failed; addr=0x%04X, len=%d, rc=%d\n",
2141 addr + 1, 5, rc);
2142 goto free_data;
2143 }
2144
2145 object->addr = addr;
2146 object->type = object_type;
2147 object->version = buf[1];
2148 object->manufacturer_id = buf[2] << 8 | buf[3];
2149 object->length = buf[4] << 8 | buf[5];
2150
2151 if ((object->addr + object->length)
2152 > (sdev->nvm_function->nvm_base_address
2153 + sdev->nvm_function->nvm_size)) {
2154 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",
2155 sdev->nvm_function->nvm_base_address,
2156 sdev->nvm_function->nvm_size,
2157 object->addr,
2158 object->length);
2159 /* Limit object size to remaining NVM size. */
2160 object->length = sdev->nvm_function->nvm_size
2161 + sdev->nvm_function->nvm_base_address
2162 - object->addr;
2163 }
2164
2165 /* Object header + CRC takes up 8 bytes. */
2166 data_len = object->length - 8;
2167 object->data = kmalloc(data_len, GFP_KERNEL);
2168 if (!object->data) {
2169 pr_err("out of memory\n");
2170 rc = -ENOMEM;
2171 goto free_data;
2172 }
2173
2174 rc = _bif_slave_read(sdev, addr + 6, object->data, data_len);
2175 if (rc) {
2176 pr_err("Slave memory read of object data failed; addr=0x%04X, len=%d, rc=%d\n",
2177 addr + 6, data_len, rc);
2178 goto free_data;
2179 }
2180
2181 rc = _bif_slave_read(sdev, addr + 6 + data_len, buf, 3);
2182 if (rc) {
2183 pr_err("Slave memory read of object CRC failed; addr=0x%04X, len=%d, rc=%d\n",
2184 addr + 6 + data_len, 3, rc);
2185 goto free_data;
2186 }
2187
2188 object->crc = buf[0] << 8 | buf[1];
2189 object_type = buf[2];
2190 sdev->nvm_function->object_count++;
2191
2192 crc = bif_object_crc_ccitt(object);
2193 if (crc != object->crc)
2194 pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n",
2195 object->addr, crc, object->crc);
2196
2197 addr += object->length;
2198 }
2199
2200 return rc;
2201
2202free_data:
2203 list_for_each_entry_safe(object, temp,
2204 &sdev->nvm_function->object_list, list) {
2205 list_del(&object->list);
2206 kfree(object->data);
2207 kfree(object);
2208 }
2209 kfree(sdev->nvm_function);
2210 sdev->nvm_function = NULL;
2211 return rc;
2212}
2213
2214static int bif_parse_slave_data(struct bif_slave_dev *sdev)
2215{
2216 int rc = 0;
2217 u8 buf[10];
2218 u8 *func_buf;
2219 struct bif_ddb_l2_data *func;
2220 int function_count, i;
2221
2222 rc = _bif_slave_read(sdev, BIF_DDB_L1_BASE_ADDR, buf, 10);
2223 if (rc) {
2224 pr_err("DDB L1 data read failed, rc=%d\n", rc);
2225 return rc;
2226 }
2227
2228 sdev->l1_data.revision = buf[0];
2229 sdev->l1_data.level = buf[1];
2230 sdev->l1_data.device_class = buf[2] << 8 | buf[3];
2231 sdev->l1_data.manufacturer_id = buf[4] << 8 | buf[5];
2232 sdev->l1_data.product_id = buf[6] << 8 | buf[7];
2233 sdev->l1_data.length = buf[8] << 8 | buf[9];
2234
2235 function_count = sdev->l1_data.length / 4;
2236 if (sdev->l1_data.length % 4) {
2237 pr_err("Function directory length=%d is invalid\n",
2238 sdev->l1_data.length);
2239 return -EPROTO;
2240 }
2241
2242 /* No DDB L2 function directory */
2243 if (function_count == 0)
2244 return 0;
2245
2246 func_buf = kmalloc(sdev->l1_data.length, GFP_KERNEL);
2247 if (!func_buf) {
2248 pr_err("out of memory\n");
2249 return -ENOMEM;
2250 }
2251
2252 sdev->function_directory = kzalloc(
2253 function_count * sizeof(struct bif_ddb_l2_data), GFP_KERNEL);
2254 if (!sdev->function_directory) {
2255 pr_err("out of memory\n");
2256 return -ENOMEM;
2257 }
2258
2259 rc = _bif_slave_read(sdev, BIF_DDB_L2_BASE_ADDR, func_buf,
2260 sdev->l1_data.length);
2261 if (rc) {
2262 pr_err("DDB L2 data read failed, rc=%d\n", rc);
2263 return rc;
2264 }
2265
2266 for (i = 0; i < function_count; i++) {
2267 func = &sdev->function_directory[i];
2268 func->function_type = func_buf[i * 4];
2269 func->function_version = func_buf[i * 4 + 1];
2270 func->function_pointer = func_buf[i * 4 + 2] << 8
2271 | func_buf[i * 4 + 3];
2272 rc = bif_initialize_protocol_function(sdev, func);
2273 if (rc)
2274 goto done;
2275 rc = bif_initialize_slave_control_function(sdev, func);
2276 if (rc)
2277 goto done;
2278 rc = bif_initialize_nvm_function(sdev, func);
2279 if (rc)
2280 goto done;
2281 }
2282done:
2283 kfree(func_buf);
2284 return rc;
2285}
2286
2287static int bif_add_secondary_slaves(struct bif_slave_dev *primary_slave)
2288{
2289 int rc = 0;
2290 int data_len, i;
2291 u16 crc;
2292 struct bif_slave_dev *sdev;
2293 struct bif_object *object;
2294
2295 list_for_each_entry(object, &primary_slave->nvm_function->object_list,
2296 list) {
2297 if (object->type != BIF_OBJ_SEC_SLAVE)
2298 continue;
2299
2300 data_len = object->length - 8;
2301 if (data_len % BIF_UNIQUE_ID_BYTE_LENGTH) {
2302 pr_info("Invalid secondary slave object found, addr=0x%04X, data len=%d\n",
2303 object->addr, data_len);
2304 continue;
2305 }
2306
2307 crc = bif_object_crc_ccitt(object);
2308 if (crc != object->crc) {
2309 pr_info("BIF object at addr=0x%04X has invalid CRC; crc calc=0x%04X, crc exp=0x%04X\n",
2310 object->addr, crc, object->crc);
2311 continue;
2312 }
2313
2314 for (i = 0; i < data_len / BIF_UNIQUE_ID_BYTE_LENGTH; i++) {
2315 sdev = bif_add_slave(primary_slave->bdev);
2316 if (IS_ERR(sdev)) {
2317 rc = PTR_ERR(sdev);
2318 pr_err("bif_add_slave failed, rc=%d\n", rc);
2319 return rc;
2320 }
2321 memcpy(sdev->unique_id,
2322 &object->data[i * BIF_UNIQUE_ID_BYTE_LENGTH],
2323 BIF_UNIQUE_ID_BYTE_LENGTH);
2324 sdev->unique_id_bits_known = BIF_UNIQUE_ID_BIT_LENGTH;
2325
2326 rc = bif_select_slave(sdev);
2327 if (rc) {
2328 pr_err("Could not select slave, rc=%d\n", rc);
2329 goto free_slave;
2330 }
2331
2332 rc = bif_is_slave_selected(sdev->bdev);
2333 if (rc < 0) {
2334 pr_err("Transaction failed, rc=%d\n", rc);
2335 goto free_slave;
2336 } else if (rc == 1) {
2337 sdev->present = true;
2338 sdev->bdev->selected_sdev = sdev;
David Collins5a90ae92013-05-02 12:19:40 -07002339 rc = bif_parse_slave_data(sdev);
2340 if (rc) {
2341 pr_err("Failed to parse secondary slave data, rc=%d\n",
2342 rc);
2343 goto free_slave;
2344 }
David Collinsed930492013-01-23 13:57:09 -08002345 } else {
2346 sdev->present = false;
2347 sdev->bdev->selected_sdev = NULL;
2348 }
2349 }
2350 }
2351
2352 return rc;
2353
2354free_slave:
2355 bif_remove_slave(sdev);
2356 return rc;
2357}
2358
2359/*
2360 * Performs UID search to identify all slaves attached to the bus. Assumes that
2361 * all necessary locks are held.
2362 */
2363static int bif_perform_uid_search(struct bif_ctrl_dev *bdev)
2364{
2365 struct bif_slave_dev *sdev;
2366 struct bif_slave_dev *new_slave;
2367 bool resp[2], resp_dilc;
2368 int i;
2369 int rc = 0;
2370 u8 cmd_probe[2] = {BIF_CMD_DIP0, BIF_CMD_DIP1};
2371 u8 cmd_enter[2] = {BIF_CMD_DIE0, BIF_CMD_DIE1};
2372
2373 /*
2374 * Iterate over all partially known UIDs adding new ones as they are
2375 * found.
2376 */
2377 list_for_each_entry(sdev, &bif_sdev_list, list) {
2378 /* Skip slaves with fully known UIDs. */
2379 if (sdev->unique_id_bits_known == BIF_UNIQUE_ID_BIT_LENGTH
2380 || sdev->bdev != bdev)
2381 continue;
2382
2383 /* Begin a new UID search. */
2384 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_BC,
2385 BIF_CMD_DISS);
2386 if (rc) {
2387 pr_err("bus_transaction failed, rc=%d\n", rc);
2388 return rc;
2389 }
2390
2391 /* Step through all known UID bits (MSB to LSB). */
2392 for (i = 0; i < sdev->unique_id_bits_known; i++) {
2393 rc = bdev->desc->ops->bus_transaction(bdev,
2394 BIF_TRANS_BC,
2395 cmd_enter[get_uid_bit(sdev->unique_id, i)]);
2396 if (rc) {
2397 pr_err("bus_transaction failed, rc=%d\n", rc);
2398 return rc;
2399 }
2400 }
2401
2402 /* Step through unknown UID bits. */
2403 for (i = sdev->unique_id_bits_known;
2404 i < BIF_UNIQUE_ID_BIT_LENGTH; i++) {
2405 rc = bdev->desc->ops->bus_transaction_query(bdev,
2406 BIF_TRANS_BC, cmd_probe[0], &resp[0]);
2407 if (rc) {
2408 pr_err("bus_transaction failed, rc=%d\n", rc);
2409 return rc;
2410 }
2411
2412 rc = bdev->desc->ops->bus_transaction_query(bdev,
2413 BIF_TRANS_BC, cmd_probe[1], &resp[1]);
2414 if (rc) {
2415 pr_err("bus_transaction failed, rc=%d\n", rc);
2416 return rc;
2417 }
2418
2419 if (resp[0] && resp[1]) {
2420 /* Create an entry for the new UID branch. */
2421 new_slave = bif_add_slave(bdev);
2422 if (IS_ERR(new_slave)) {
2423 rc = PTR_ERR(sdev);
2424 pr_err("bif_add_slave failed, rc=%d\n",
2425 rc);
2426 return rc;
2427 }
2428 memcpy(new_slave->unique_id, sdev->unique_id,
2429 BIF_UNIQUE_ID_BYTE_LENGTH);
2430 new_slave->bdev = sdev->bdev;
2431
2432 set_uid_bit(sdev->unique_id, i, 0);
2433 sdev->unique_id_bits_known = i + 1;
2434
2435 set_uid_bit(new_slave->unique_id, i, 1);
2436 new_slave->unique_id_bits_known = i + 1;
2437 } else if (resp[0]) {
2438 set_uid_bit(sdev->unique_id, i, 0);
2439 sdev->unique_id_bits_known = i + 1;
2440 } else if (resp[1]) {
2441 set_uid_bit(sdev->unique_id, i, 1);
2442 sdev->unique_id_bits_known = i + 1;
2443 } else {
2444 pr_debug("no bus query response received\n");
2445 rc = -ENXIO;
2446 return rc;
2447 }
2448
2449 rc = bdev->desc->ops->bus_transaction(bdev,
2450 BIF_TRANS_BC, cmd_enter[resp[0] ? 0 : 1]);
2451 if (rc) {
2452 pr_err("bus_transaction failed, rc=%d\n", rc);
2453 return rc;
2454 }
2455 }
2456
2457 rc = bdev->desc->ops->bus_transaction_query(bdev,
2458 BIF_TRANS_BC, BIF_CMD_DILC, &resp_dilc);
2459 if (rc) {
2460 pr_err("bus_transaction failed, rc=%d\n", rc);
2461 return rc;
2462 }
2463
2464 if (resp_dilc) {
2465 sdev->present = true;
2466 sdev->bdev->selected_sdev = sdev;
2467 rc = bif_parse_slave_data(sdev);
David Collins5a90ae92013-05-02 12:19:40 -07002468 if (rc) {
2469 pr_err("Failed to parse secondary slave data, rc=%d\n",
2470 rc);
2471 return rc;
2472 }
David Collinsed930492013-01-23 13:57:09 -08002473 } else {
2474 pr_err("Slave failed to respond to DILC bus command; its UID is thus unverified.\n");
2475 sdev->unique_id_bits_known = 0;
2476 rc = -ENXIO;
2477 return rc;
2478 }
2479 }
2480
2481 return rc;
2482}
2483
2484/*
2485 * Removes slaves from the bif_sdev_list which have the same UID as previous
2486 * slaves in the list.
2487 */
2488static int bif_remove_duplicate_slaves(struct bif_ctrl_dev *bdev)
2489{
2490 struct bif_slave_dev *sdev;
2491 struct bif_slave_dev *last_slave;
2492 struct bif_slave_dev *temp;
2493
2494 list_for_each_entry_safe(last_slave, temp, &bif_sdev_list, list) {
2495 list_for_each_entry(sdev, &bif_sdev_list, list) {
2496 if (last_slave == sdev) {
2497 break;
2498 } else if (memcmp(last_slave->unique_id,
2499 sdev->unique_id,
2500 BIF_UNIQUE_ID_BYTE_LENGTH) == 0) {
2501 bif_remove_slave(last_slave);
2502 break;
2503 }
2504 }
2505 }
2506
2507 return 0;
2508}
2509
2510static int bif_add_all_slaves(struct bif_ctrl_dev *bdev)
2511{
2512 struct bif_slave_dev *sdev;
2513 int rc = 0;
2514 int i;
2515 bool has_slave = false, is_primary_slave = false;
2516
2517 mutex_lock(&bif_sdev_list_mutex);
2518 mutex_lock(&bdev->mutex);
2519
2520 list_for_each_entry(sdev, &bif_sdev_list, list) {
2521 if (sdev->bdev == bdev) {
2522 has_slave = true;
2523 break;
2524 }
2525 }
2526
2527 if (!has_slave) {
2528 /* Create a single empty slave to start the search algorithm. */
2529 sdev = bif_add_slave(bdev);
2530 if (IS_ERR(sdev)) {
2531 rc = PTR_ERR(sdev);
2532 pr_err("bif_add_slave failed, rc=%d\n", rc);
2533 goto out;
2534 }
2535
2536 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
2537 /* Attempt to select primary slave in battery pack. */
2538 rc = bdev->desc->ops->bus_transaction(bdev,
2539 BIF_TRANS_SDA, BIF_PRIMARY_SLAVE_DEV_ADR);
2540 if (rc == 0)
2541 break;
2542 }
2543 if (rc) {
2544 pr_err("BIF bus_transaction failed, rc=%d\n", rc);
2545 goto out;
2546 }
2547
2548 /* Check if a slave is selected. */
2549 rc = bif_is_slave_selected(bdev);
2550 if (rc < 0) {
2551 pr_err("BIF bus_transaction failed, rc=%d\n", rc);
2552 goto out;
2553 } else {
2554 is_primary_slave = rc;
2555 }
2556 }
2557
2558 if (is_primary_slave) {
2559 pr_debug("Using primary slave at DEV_ADR==0x%02X\n",
2560 BIF_PRIMARY_SLAVE_DEV_ADR);
2561 sdev->bdev->selected_sdev = sdev;
2562 sdev->present = true;
2563 sdev->slave_addr = BIF_PRIMARY_SLAVE_DEV_ADR;
2564 rc = bif_parse_slave_data(sdev);
2565 if (rc) {
2566 pr_err("Failed to parse primary slave data, rc=%d\n",
2567 rc);
2568 goto out;
2569 }
2570 rc = bif_add_secondary_slaves(sdev);
2571 if (rc) {
2572 pr_err("Failed to add secondary slaves, rc=%d\n", rc);
2573 goto out;
2574 }
2575 } else {
2576 pr_debug("Falling back on full UID search.\n");
2577 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
2578 rc = bif_perform_uid_search(bdev);
2579 if (rc == 0)
2580 break;
2581 }
2582 if (rc) {
2583 pr_debug("BIF UID search failed, rc=%d\n", rc);
2584 goto out;
2585 }
2586 }
2587
2588 bif_remove_duplicate_slaves(bdev);
2589
2590 mutex_unlock(&bdev->mutex);
2591 mutex_unlock(&bif_sdev_list_mutex);
2592
2593 return rc;
2594
2595out:
2596 mutex_unlock(&bdev->mutex);
2597 mutex_unlock(&bif_sdev_list_mutex);
2598 pr_debug("BIF slave search failed, rc=%d\n", rc);
2599 return rc;
2600}
2601
2602static int bif_add_known_slave(struct bif_ctrl_dev *bdev, u8 slave_addr)
2603{
2604 struct bif_slave_dev *sdev;
2605 int rc = 0;
2606 int i;
2607
2608 for (i = 0; i < BIF_TRANSACTION_RETRY_COUNT; i++) {
2609 /* Attempt to select the slave. */
2610 rc = bdev->desc->ops->bus_transaction(bdev, BIF_TRANS_SDA,
2611 slave_addr);
2612 if (rc == 0)
2613 break;
2614 }
2615 if (rc) {
2616 pr_err("BIF bus_transaction failed, rc=%d\n", rc);
2617 return rc;
2618 }
2619
2620 /* Check if a slave is selected. */
2621 rc = bif_is_slave_selected(bdev);
2622 if (rc < 0) {
2623 pr_err("BIF bus_transaction failed, rc=%d\n", rc);
2624 return rc;
2625 }
2626
2627 sdev = bif_add_slave(bdev);
2628 if (IS_ERR(sdev)) {
2629 rc = PTR_ERR(sdev);
2630 pr_err("bif_add_slave failed, rc=%d\n", rc);
2631 return rc;
2632 }
2633
2634 sdev->bdev->selected_sdev = sdev;
2635 sdev->present = true;
2636 sdev->slave_addr = slave_addr;
2637 rc = bif_parse_slave_data(sdev);
2638 if (rc) {
2639 pr_err("Failed to parse slave data, addr=0x%02X, rc=%d\n",
2640 slave_addr, rc);
2641 return rc;
2642 }
2643
2644 return rc;
2645}
2646
2647static int bif_add_known_slaves_from_dt(struct bif_ctrl_dev *bdev,
2648 struct device_node *of_node)
2649{
2650 int len = 0;
2651 int rc, i;
2652 u32 addr;
2653 const __be32 *val;
2654
2655 mutex_lock(&bif_sdev_list_mutex);
2656 mutex_lock(&bdev->mutex);
2657
2658 val = of_get_property(of_node, "qcom,known-device-addresses", &len);
2659 len /= sizeof(u32);
2660 if (val && len == 0) {
2661 pr_err("qcom,known-device-addresses property is invalid\n");
2662 rc = -EINVAL;
2663 goto out;
2664 }
2665
2666 for (i = 0; i < len; i++) {
2667 addr = be32_to_cpup(val++);
2668 if (addr == 0x00 || addr > 0xFF) {
2669 rc = -EINVAL;
2670 pr_err("qcom,known-device-addresses property contains invalid address=0x%X\n",
2671 addr);
2672 goto out;
2673 }
2674 rc = bif_add_known_slave(bdev, addr);
2675 if (rc) {
2676 pr_err("bif_add_known_slave() failed, rc=%d\n", rc);
2677 goto out;
2678 }
2679 }
2680
2681out:
2682 if (len > 0)
2683 bif_remove_duplicate_slaves(bdev);
2684
2685 mutex_unlock(&bdev->mutex);
2686 mutex_unlock(&bif_sdev_list_mutex);
2687
2688 return rc;
2689}
2690
2691/*
2692 * Programs a device address for the specified slave in order to simplify
2693 * slave selection in the future.
2694 */
2695static int bif_assign_slave_dev_addr(struct bif_slave_dev *sdev, u8 dev_addr)
2696{
2697 int rc;
2698 u16 addr;
2699
2700 if (!sdev->protocol_function) {
2701 pr_err("Protocol function not present; cannot set device address.\n");
2702 return -ENODEV;
2703 }
2704
2705 addr = PROTOCOL_FUNC_DEV_ADR_ADDR(
2706 sdev->protocol_function->protocol_pointer);
2707
2708 rc = _bif_slave_write(sdev, addr, &dev_addr, 1);
2709 if (rc)
2710 pr_err("Failed to set slave device address.\n");
2711 else
2712 sdev->slave_addr = dev_addr;
2713
2714 return rc;
2715}
2716
2717/* Assigns a unique device address to all slaves which do not have one. */
2718static int bif_assign_all_slaves_dev_addr(struct bif_ctrl_dev *bdev)
2719{
2720 struct bif_slave_dev *sdev;
2721 struct bif_slave_dev *sibling;
2722 bool duplicate;
2723 int rc = 0;
2724 u8 dev_addr, first_dev_addr;
2725
2726 mutex_lock(&bif_sdev_list_mutex);
2727 mutex_lock(&bdev->mutex);
2728
2729 first_dev_addr = next_dev_addr;
2730 /*
2731 * Iterate over all partially known UIDs adding new ones as they are
2732 * found.
2733 */
2734 list_for_each_entry(sdev, &bif_sdev_list, list) {
2735 /*
2736 * Skip slaves without known UIDs, which already have a device
2737 * address or which aren't present.
2738 */
2739 if (sdev->unique_id_bits_known != BIF_UNIQUE_ID_BIT_LENGTH
2740 || sdev->slave_addr != 0x00 || !sdev->present)
2741 continue;
2742
2743 do {
2744 dev_addr = next_dev_addr;
2745 duplicate = false;
2746 list_for_each_entry(sibling, &bif_sdev_list, list) {
2747 if (sibling->slave_addr == dev_addr) {
2748 duplicate = true;
2749 break;
2750 }
2751 }
2752
2753 next_dev_addr = dev_addr + 1;
2754 } while (duplicate && (next_dev_addr != first_dev_addr));
2755
2756 if (next_dev_addr == first_dev_addr) {
2757 pr_err("No more BIF slave device addresses available.\n");
2758 rc = -ENODEV;
2759 goto out;
2760 }
2761
2762 rc = bif_assign_slave_dev_addr(sdev, dev_addr);
2763 if (rc) {
2764 pr_err("Failed to set slave address.\n");
2765 goto out;
2766 }
2767 }
2768
2769 mutex_unlock(&bdev->mutex);
2770 mutex_unlock(&bif_sdev_list_mutex);
2771
2772 return rc;
2773
2774out:
2775 mutex_unlock(&bdev->mutex);
2776 mutex_unlock(&bif_sdev_list_mutex);
2777 pr_err("BIF slave device address setting failed, rc=%d\n", rc);
2778 return rc;
2779}
2780
2781/**
2782 * bdev_get_drvdata() - get the private BIF controller driver data
2783 * @bdev: BIF controller device pointer
2784 */
2785void *bdev_get_drvdata(struct bif_ctrl_dev *bdev)
2786{
2787 return bdev->driver_data;
2788}
2789EXPORT_SYMBOL(bdev_get_drvdata);
2790
2791static const char * const battery_label[] = {
2792 "unknown",
2793 "none",
2794 "special 1",
2795 "special 2",
2796 "special 3",
2797 "low cost",
2798 "smart",
2799};
2800
2801static const char *bif_get_battery_pack_type(int rid_ohm)
2802{
2803 const char *label = battery_label[0];
2804
2805 if (rid_ohm > BIF_BATT_RID_SMART_MAX)
2806 label = battery_label[1];
2807 else if (rid_ohm >= BIF_BATT_RID_SMART_MIN)
2808 label = battery_label[6];
2809 else if (rid_ohm >= BIF_BATT_RID_LOW_COST_MIN
2810 && rid_ohm <= BIF_BATT_RID_LOW_COST_MAX)
2811 label = battery_label[5];
2812 else if (rid_ohm >= BIF_BATT_RID_SPECIAL3_MIN
2813 && rid_ohm <= BIF_BATT_RID_SPECIAL3_MAX)
2814 label = battery_label[4];
2815 else if (rid_ohm >= BIF_BATT_RID_SPECIAL2_MIN
2816 && rid_ohm <= BIF_BATT_RID_SPECIAL2_MAX)
2817 label = battery_label[3];
2818 else if (rid_ohm >= BIF_BATT_RID_SPECIAL1_MIN
2819 && rid_ohm <= BIF_BATT_RID_SPECIAL1_MAX)
2820 label = battery_label[2];
2821
2822 return label;
2823}
2824
2825/**
2826 * bif_ctrl_register() - register a BIF controller with the BIF framework
2827 * @bif_desc: Pointer to BIF controller descriptor
2828 * @dev: Device pointer of the BIF controller
2829 * @driver_data: Private driver data to associate with the BIF controller
2830 * @of_node Pointer to the device tree node of the BIF controller
2831 *
2832 * Returns a BIF controller device pointer for the controller if registration
2833 * is successful or an ERR_PTR if an error occurred.
2834 */
2835struct bif_ctrl_dev *bif_ctrl_register(struct bif_ctrl_desc *bif_desc,
2836 struct device *dev, void *driver_data, struct device_node *of_node)
2837{
2838 struct bif_ctrl_dev *bdev = ERR_PTR(-EINVAL);
2839 struct bif_slave_dev *sdev;
2840 bool battery_present = false;
David Collins94a763e2013-09-12 13:15:44 -07002841 bool slaves_present = false;
David Collinsed930492013-01-23 13:57:09 -08002842 int rc, rid_ohm;
2843
2844 if (!bif_desc) {
2845 pr_err("Invalid bif_desc specified\n");
2846 return bdev;
2847 } else if (!bif_desc->name) {
2848 pr_err("BIF name missing\n");
2849 return bdev;
2850 } else if (!bif_desc->ops) {
2851 pr_err("BIF operations missing\n");
2852 return bdev;
2853 } else if (!bif_desc->ops->bus_transaction
2854 || !bif_desc->ops->bus_transaction_query
2855 || !bif_desc->ops->bus_transaction_read
2856 || !bif_desc->ops->get_bus_state
2857 || !bif_desc->ops->set_bus_state) {
2858 pr_err("BIF operation callback function(s) missing\n");
2859 return bdev;
2860 }
2861
2862 bdev = kzalloc(sizeof(struct bif_ctrl_dev), GFP_KERNEL);
2863 if (bdev == NULL) {
2864 pr_err("Memory allocation failed for bif_ctrl_dev\n");
2865 return ERR_PTR(-ENOMEM);
2866 }
2867
2868 mutex_init(&bdev->mutex);
2869 INIT_LIST_HEAD(&bdev->list);
2870 INIT_DELAYED_WORK(&bdev->enter_irq_mode_work, bif_enter_irq_mode_work);
2871 bdev->desc = bif_desc;
2872 bdev->ctrl_dev = dev;
2873 bdev->driver_data = driver_data;
2874 bdev->irq_mode_delay_jiffies = 2;
2875
2876 mutex_lock(&bif_ctrl_list_mutex);
2877 list_add_tail(&bdev->list, &bif_ctrl_list);
2878 mutex_unlock(&bif_ctrl_list_mutex);
2879
2880 rc = bif_add_all_slaves(bdev);
2881 if (rc)
2882 pr_debug("Search for all slaves failed, rc=%d\n", rc);
2883 rc = bif_add_known_slaves_from_dt(bdev, of_node);
2884 if (rc)
2885 pr_err("Adding slaves based on device tree addressed failed, rc=%d.\n",
2886 rc);
2887 rc = bif_assign_all_slaves_dev_addr(bdev);
2888 if (rc)
2889 pr_err("Failed to set slave device address, rc=%d\n", rc);
2890
2891 bif_print_slaves();
2892
2893 if (bdev->desc->ops->get_battery_presence) {
2894 rc = bdev->desc->ops->get_battery_presence(bdev);
2895 if (rc < 0) {
2896 pr_err("Could not determine battery presence, rc=%d\n",
2897 rc);
2898 } else {
2899 battery_present = rc;
2900 pr_info("Battery pack present = %c\n", rc ? 'Y' : 'N');
2901 }
2902 }
2903
2904 if (bdev->desc->ops->get_battery_rid) {
2905 rid_ohm = bdev->desc->ops->get_battery_rid(bdev);
2906 if (rid_ohm >= 0)
2907 pr_info("Battery pack type = %s (Rid=%d ohm)\n",
2908 bif_get_battery_pack_type(rid_ohm), rid_ohm);
2909 else
2910 pr_err("Could not read Rid, rc=%d\n", rid_ohm);
2911 }
2912
2913 list_for_each_entry(sdev, &bif_sdev_list, list) {
2914 if (sdev->present) {
2915 battery_present = true;
David Collins94a763e2013-09-12 13:15:44 -07002916 slaves_present = true;
David Collinsed930492013-01-23 13:57:09 -08002917 break;
2918 }
2919 }
2920
2921 BLOCKING_INIT_NOTIFIER_HEAD(&bdev->bus_change_notifier);
2922
David Collins94a763e2013-09-12 13:15:44 -07002923 /* Disable the BIF bus master if no slaves are found. */
2924 if (!slaves_present) {
2925 rc = bdev->desc->ops->set_bus_state(bdev,
2926 BIF_BUS_STATE_MASTER_DISABLED);
2927 if (rc < 0)
2928 pr_err("Could not disble BIF master, rc=%d\n", rc);
2929 }
2930
David Collinsed930492013-01-23 13:57:09 -08002931 if (battery_present) {
2932 bdev->battery_present = true;
2933 rc = blocking_notifier_call_chain(&bdev->bus_change_notifier,
2934 BIF_BUS_EVENT_BATTERY_INSERTED, bdev);
2935 if (rc)
2936 pr_err("Call chain noification failed, rc=%d\n", rc);
2937 }
2938
2939 return bdev;
2940}
2941EXPORT_SYMBOL(bif_ctrl_register);
2942
2943/**
2944 * bif_ctrl_unregister() - unregisters a BIF controller
2945 * @bdev: BIF controller device pointer
2946 */
2947void bif_ctrl_unregister(struct bif_ctrl_dev *bdev)
2948{
2949 if (bdev) {
2950 mutex_lock(&bif_ctrl_list_mutex);
2951 list_del(&bdev->list);
2952 mutex_unlock(&bif_ctrl_list_mutex);
2953 }
2954}
2955EXPORT_SYMBOL(bif_ctrl_unregister);