blob: 9831c800806d0e82e3172da5c3a8304679f2b138 [file] [log] [blame]
David Collins2369cc42013-03-25 15:57:34 -07001Introduction
2============
3
4BIF (Battery Interface) is a MIPI (Mobile Industry Processor Interface)
5Alliance specification for a serial interface between a host device and a
6battery pack. It provides a means to handle smart battery packs which can
7communicate over BIF as well as low cost battery packs which provide no
8serial communication interface.
9
10The BIF bus supports 1 master and up to 256 slaves. It supports data rates
11up to 250 kbps. The master is in charge of initiating all bus
12communications. Slaves may only respond asynchronously when they need to
13signal the master that they have an interrupt pending and when the bus is
14configured for interrupt mode.
15
16The BIF framework consists of a core into which BIF controller drivers
17register. At runtime, consumers are notified of various events (e.g. battery
18insertion and battery removal) via a notifier. Various framework functions are
19available for consumers to read and write slave registers as well as to send
20arbitrary BIF commands on the bus.
21
22Hardware description
23====================
24
25The BIF bus is a 1-wire wired-or interface. The bus signal is referred to as
26the battery communication line (BCL). The BCL is pulled high by a resistor on
27the host side and is driven low when the master or one of the slaves is
28communicating. Additionally, there is a pull down resistor in the battery
29pack which is used to identify whether or not the battery pack has BIF slaves.
30Battery removal detection is achieved by comparing the analog voltage of the BCL
31when idle to the host side reference voltage. If these voltages are within a
32certain threshold, then a battery pack is not present.
33
34Slaves are addressed on the BIF bus using an 8-bit device address (DEV_ADR).
35Notably, it is possible for no slaves to have defined DEV_ADR. In this case,
36slave addressing is achieved via the always present unique ID (UID). The UID
37of a slave is 80 bits long and guaranteed to be globally unique. A UID search
38algorithm can be followed in order determine the UID of all slaves on the bus.
39
40BIF slaves come in two varieties: primary and secondary. A single primary
41slave may be present on the battery pack and a single primary slave may be
42present on the host. A battery pack primary slave has DEV_ADR=0x01. The
43DEV_ADR of a host primary slave is set by the manufacturer. A given primary
44slave contains a list of the UIDs of all secondary slaves in the same
45subsystem. This provides a fast mechanism to determine the address of all
46slaves without having to resort to the lengthy UID search algorithm.
47
48Each slave has a 64 kB address space. Part of this address space consists of
49generic DDB L1 and L2 data structures at known addresses. This allows for
50runtime discovery of supported battery properties and functions of a given
51smart battery pack.
52
53System Diagram:
54 +-------------------------------+ +---------------------------------+
55 | Host | | Smart Battery Pack |
56 | | | |
57 | Vbat-<+>-------<+>----------------------------+ |
58 | | | | |
59 | +--------------+ | | +--------------+ | |
60 | | Master BIF<+>-+---------<+>--BCL--<+>------+-<+>BIF Primary | | |
61 | | | | | | | | Slave | | |
62 | +--------------+ | | | | +--------------+ | |
63 | | | | | | |
64 | + - - - - - - -+ | | | | + - - - - - - -+ | |
65 | | Primary BIF<+>-+ | | +-<+>BIF Secondary| | |
66 | | Slave | | | | | | Slave | | |
67 | +- - - - - - - + | | | | +-- - - - - - -+ | |
68 | | | | | | |
69 | + - - - - - - -+ | | | | + - - - - - - -+ | |
70 | |Secondary BIF<+>-+ | | +-<+>BIF Secondary| | |
71 | |Slave | | | | | | Slave | | |
72 | +- - - - - - - + | | | | +-- - - - - - -+ | |
73 | / | | / | |
74 | Vref \ Rpu | | Rid \ ---- |
75 | ___ / | | / Battery -- |
76 | | \ | | \ Cell ---- |
77 | +-------+ | | | -- |
78 | | | | | |
79 | GND-<+>-------<+>------+---------------------+ |
80 | | | |
81 +-------------------------------+ +---------------------------------+
82
83An overview of BIF is available at:
84http://mipi.org/specifications/battery-interface
85
86Software description
87====================
88
89A given BIF hardware interface driver registers as a BIF controller in the
90BIF framework during its probe function. The controller specifies a set of
91callback functions which are used by the BIF framework to initiate bus
92transactions (e.g. register read, register write, wait for slave interrupt)
93and to configure the bus. The framework exposes a small API to controllers
94which is used to notify the framework about asynchronous events such as
95battery pack insertion/removal and slave interrupts.
96
97A given BIF consumer is linked to a BIF controller by specifying a property
98in the consumer's device tree node which takes as its value the phandle of
99the BIF controller's device tree node.
100
101A consumer driver calls a get function during its probe function with its
102device pointer in order to get a handle to the BIF controller if it has probed.
103If it hasn't, then ERR_PTR(-EPROBE_DEFER) is returned. The controller handle
104can be used directly by the consumer to issue raw bus transactions if needed.
105The controller handle can then be used to query which slaves are currently
106present on the bus, if any. Handles to these slaves may be used by a consumer
107driver in high level framework APIs such as register read and register write
108which are slave oriented. All BIF framework API functions are synchronous,
109blocking, and can sleep.
110
111Consumer drivers may also register a notifier function which is called when
112certain bus activities occur such as battery pack insertion and removal.
113Additionally, consumer drivers may register a notifier function which is called
114when a specified slave interrupt fires.
115
116The framework maintains several linked-lists. One list contains all controllers
117that have been registered. A second list contains all slaves that have been
118seen since the system booted as well as a flag to indicate if they are currently
119present or not. This scheme is used to avoid issues with slave handles existing
120after a slave is removed and also so that function and object values do not have
121to be searched when a slave is reinserted in the system since slaves are
122globally unique and these features are read-only. Two further lists are
123maintained inside slave device structures which contain BIF functions and
124objects found in the slave. API functions are provided so that consumers can
125find functions supported by slaves.
126
127Design
128======
129
130Design Goals:
131One major goal of the BIF framework is to provide a uniform API for BIF
132consumers to communicate with battery packs. This ensures that consumers are
133unaffected by changes in the controller driver which actually interfaces with
134the BCL at a hardware level.
135
136Another goal of the framework is to ensure the BIF bus can be shared between
137multiple consumers in a simple and functionally correct way. Locking is used
138inside of the framework to provide mutual exclusion on the bus.
139
140The framework also exposes features that almost all consumers will need, such
141as BIF slave identification and BIF function enumeration within a given slave.
142
143The framework allows consumers to issue very specific bus commands which may
144not be used within high level APIs. This provides maximum flexibility so
145that consumers can make use of manufacturer defined bus commands which cannot be
146handled in a generic fashion.
147
148Design Trade-offs:
149The choice to not treat BIF like a traditional Linux bus was made because
150there is nothing within BIF that naturally maps to a device on the bus for a
151driver to manage. Slave devices would be a good candidate except that
152consumers will not be managing slaves so much as functions exposed within
153slaves. Bus matching could then instead be made at a BIF slave function
154level. Unfortunately, the BIF specification allows for manufacturer specific
155features to reside at any non-defined addresses. Additionally, consumers may
156wish only to read and make policy decisions based on BIF non-volatile memory
157(NVM) objects read out of memory. Thus, there are use-cases that require
158consumers to utilize the bus without having a particular function to match to.
159
160Another trade-off was the choice to use custom interrupt handling functions
161instead of the Linux interrupt framework. This choice was made because there is
162no obvious way to handle IRQ chip registration given the dynamic nature of BIF
163slaves (i.e. slaves may come and go at runtime if battery packs are swapped).
164
165Software layering:
166BIF controller drivers register a set of callback functions with the BIF
167framework which implement various BIF transaction primitives. These
168callbacks ensure that tight timing constraints are met such as when receiving
169a bus query response immediately after issuing a command. Such actions
170cannot be carried out at the framework level as timing requirements are on
171the order of 32 us when using the maximum data rate.
172
173The BIF framework provides easy access to standard BIF features such as
174slave, functions, and interrupts. The framework also ensures mutual exclusion
175between different BIF consumers.
176
177BIF consumer drivers make use of the API exposed by the framework in order
178utilize functionality found on smart battery packs. One example of a
179consumer driver is a temperature monitoring driver which reads the
180temperature reported by the BIF temperature function on a BIF slave and
181reports it to the Linux thermal framework.
182
183Power Management
184================
185
186The framework does not perform any special actions during system suspend and
187resume. Controller drivers may choose to enter low power states during
188suspend if they wish as long as it does not affect the logical state of the
189bus.
190
191SMP/multi-core
192==============
193
194Various linked lists are maintained inside of the framework which are
195protected by mutexes. Mutex locks are also used during transactions at a bus
196level in order to ensure mutual exclusion between consumers of the bus.
197
198Performance
199===========
200
201The BIF bus is inherently slow. Consumers should expect transactions to take
202a long time to execute. Consumers are responsible for blocking suspend if
203their transactions must be completed before the system enters suspend.
204
205Interface - BIF Consumer API
206============================
207
208BIF framework structs, enums, and functions used by BIF consumers are defined in
209include/linux/bif/consumer.h
210
211Detailed descriptions of the BIF framework functions can be found in:
212drivers/bif/bif-core.c
213
214Get/put handle for a BIF controller:
215------------------------------------
216
217struct bif_ctrl *bif_ctrl_get(struct device *consumer_dev);
218
219void bif_ctrl_put(struct bif_ctrl *ctrl);
220
221int bif_ctrl_count(void);
222
223struct bif_ctrl *bif_ctrl_get_by_id(unsigned int id);
224
225The function bif_ctrl_get() is intended to be the primary way to get a consumer
226BIF controller handle. It relies upon the consumer device specifying a
227"qcom,bif-ctrl" property in its device tree node which points to the phandle of
228the BIF controller it wishes to use.
229
230A secondary mechanism is also provided for drivers without device tree support.
231bif_ctrl_count() returns the number of BIF controllers currently registered.
232bif_ctrl_get_by_id() returns a handle to the id'th controller enumerated in
233registration order.
234
235Get/put handle for a BIF slave:
236-------------------------------
237
David Collins387d5fb2013-09-23 11:51:16 -0700238int bif_slave_match_count(struct bif_ctrl *ctrl,
David Collins2369cc42013-03-25 15:57:34 -0700239 const struct bif_match_criteria *match_criteria);
240
David Collins387d5fb2013-09-23 11:51:16 -0700241struct bif_slave *bif_slave_match_get(struct bif_ctrl *ctrl,
David Collins2369cc42013-03-25 15:57:34 -0700242 unsigned int id, const struct bif_match_criteria *match_criteria);
243
244void bif_slave_put(struct bif_slave *slave);
245
246A consumer finds a slave attached to a given BIF controller by specifying a set
247of matching criteria. The criteria can include such quantities as manufacturer
248ID, product ID, function type or function version. It is possible that multiple
249slaves will match the criteria. bif_slave_match_count() returns how many slaves
250match the specified criteria. bif_slave_match_get() returns the id'th slave
251which matches the criteria in an arbitrary, but fixed order (for a constant set
252of slaves). Consumer drivers need to be able to handle the case of multiple
253slaves matching the criteria.
254
255Additionally, if a battery pack is inserted or removed, then the output of
256bif_slave_match_count() and bif_slave_match_get() could change. A consumer
257driver can register to receive notification of battery pack insertion and
258removal using the bif_ctrl_notifier_register() function listed below.
259
260Check if slave handle is still meaningful:
261------------------------------------------
262
263int bif_slave_is_present(struct bif_slave *slave);
264
265If a battery pack is removed, then the handles for its slaves will no longer be
266meaningful. All transactions using a handle for a slave that isn't present will
267fail. The function bif_slave_is_present() allows a consumer to determine if
268a given slave is still physically present in the system.
269
270Get access to the controller handle present in a slave handle:
271--------------------------------------------------------------
272
273struct bif_ctrl *bif_get_ctrl_handle(struct bif_slave *slave);
274
275This function is useful if a consumer wishes to only store a slave handle but
276also has need to call bus oriented BIF framework functions.
277
278Get version and register offset of a BIF function if it is present in a slave:
279------------------------------------------------------------------------------
280
281int bif_slave_find_function(struct bif_slave *slave, u8 function, u8 *version,
282 u16 *function_pointer);
283
284This function is used by consumers who wish to support given BIF functions
285(e.g. temperature measurement, authentication, etc.) found inside of slaves.
286
287Receive notification upon battery insertion and removal:
288--------------------------------------------------------
289
290int bif_ctrl_notifier_register(struct bif_ctrl *ctrl,
291 struct notifier_block *nb);
292
293int bif_ctrl_notifier_unregister(struct bif_ctrl *ctrl,
294 struct notifier_block *nb);
295
David Collins2369cc42013-03-25 15:57:34 -0700296Read or write BIF slave registers:
297----------------------------------
298
299int bif_slave_read(struct bif_slave *slave, u16 addr, u8 *buf, int len);
300
301int bif_slave_write(struct bif_slave *slave, u16 addr, u8 *buf, int len);
302
303
David Collins95738fe2013-09-18 17:17:08 -0700304BIF slave non-volatile memory manipulation:
305-------------------------------------------
306
307int bif_slave_nvm_raw_read(struct bif_slave *slave, u16 offset, u8 *buf,
308 int len);
309
310int bif_slave_nvm_raw_write(struct bif_slave *slave, u16 offset, u8 *buf,
311 int len);
312
313Raw NVM writing may be needed in order to intialize the NVM BIF object list.
314However, its use can be dangerous as it can overwrite existing objects in the
315list and make the list unparsable.
316
David Collins387d5fb2013-09-23 11:51:16 -0700317BIF object search in slave non-volatile memory:
318-----------------------------------------------
319int bif_object_match_count(struct bif_slave *slave,
320 const struct bif_obj_match_criteria *match_criteria);
321
322struct bif_object *bif_object_match_get(struct bif_slave *slave,
323 unsigned int id, const struct bif_obj_match_criteria *match_criteria);
324
325void bif_object_put(struct bif_object *object);
326
327bif_object_match_count() and bif_object_match_get() can be used together in
328order to retrieve the set of BIF objects within a slave which match certain
329criteria. bif_object_put() is used to free the memory allocated by
330bif_object_match_get().
David Collins95738fe2013-09-18 17:17:08 -0700331
David Collins2cf302f2013-09-23 17:33:13 -0700332BIF object manipulation in slave non-volatile memory:
333-----------------------------------------------------
334int bif_object_write(struct bif_slave *slave, u8 type, u8 version, u16
335 manufacturer_id, const u8 *data, int data_len);
336
337int bif_object_overwrite(struct bif_slave *slave,
338 struct bif_object *object, u8 type, u8 version,
339 u16 manufacturer_id, const u8 *data, int data_len);
340
341int bif_object_delete(struct bif_slave *slave, const struct bif_object *object);
342
343bif_object_write() can be used to write a new BIF data object into the NVM of
344a given slave. The new object is added to the end of the NVM object list.
345bif_object_overwrite() can be used to overwrite an existing BIF data object
346in the NVM of a slave. The new object data must be the same size as the
347existing object data. bif_object_delete() can be used to delete a object from
348the NVM object list and shift all of the objects after it in order to fill the
349deleted object's space.
350
David Collins2369cc42013-03-25 15:57:34 -0700351Get or set the BIF bus state or period:
352---------------------------------------
353
354int bif_ctrl_get_bus_state(struct bif_ctrl *ctrl);
355
356int bif_ctrl_set_bus_state(struct bif_ctrl *ctrl, enum bif_bus_state state);
357
358int bif_ctrl_get_bus_period(struct bif_ctrl *ctrl);
359
360int bif_ctrl_set_bus_period(struct bif_ctrl *ctrl, int period_ns);
361
362Bus states include: active for communication, active waiting for interrupt,
363standby, and power down. The MIPI-BIF specification defines the allowed range
364of bus periods as 2000 ns to 153000 ns. Individual controllers may further
365restrict the range of allowed periods. When bif_ctrl_set_bus_period() is called
366the first supported period that greater than or equal to the specified period
367will be set.
368
369Measure battery pack resistance:
370--------------------------------
371
372int bif_ctrl_measure_rid(struct bif_ctrl *ctrl);
373
374This function returns an estimate of the battery pack resistance in ohms. If
375no battery pack is connected, then the output of this function is undefined.
376
377Utilize BIF slave tasks and interrupts:
378---------------------------------------
379
380int bif_request_irq(struct bif_slave *slave, unsigned int task,
381 struct notifier_block *nb);
382
383int bif_free_irq(struct bif_slave *slave, unsigned int task,
384 struct notifier_block *nb);
385
386int bif_trigger_task(struct bif_slave *slave, unsigned int task);
387
388int bif_task_is_busy(struct bif_slave *slave, unsigned int task);
389
David Collinsbf6a6352013-04-11 13:46:53 -0700390int bif_enable_auto_task(struct bif_slave *slave, unsigned int task);
391
392int bif_disable_auto_task(struct bif_slave *slave, unsigned int task);
393
David Collins2369cc42013-03-25 15:57:34 -0700394A consumer can request a slave interrupt and specify a notifier to call when the
395interrupt is triggered. Once the interrupt is requested the consumer will need
396to call bif_trigger_task() in order to start the task associated with the
397interrupt (both are identified by the same index). Polling for task completion
David Collinsbf6a6352013-04-11 13:46:53 -0700398is also supported via the bif_task_is_busy() function. Auto task triggered can
399be enabled and disabled for a given task using bif_enable_auto_task() and
400bif_disable_auto_task() respectively.
David Collins2369cc42013-03-25 15:57:34 -0700401
402Raw BIF bus transactions:
403-------------------------
404
405void bif_ctrl_bus_lock(struct bif_ctrl *ctrl);
406
407void bif_ctrl_bus_unlock(struct bif_ctrl *ctrl);
408
409int bif_ctrl_raw_transaction(struct bif_ctrl *ctrl, int transaction, u8 data);
410
411int bif_ctrl_raw_transaction_read(struct bif_ctrl *ctrl, int transaction,
412 u8 data, int *response);
413
414int bif_ctrl_raw_transaction_query(struct bif_ctrl *ctrl, int transaction,
415 u8 data, bool *query_response);
416
417int bif_slave_is_selected(struct bif_slave *slave);
418
419int bif_slave_select(struct bif_slave *slave);
420
421The function bif_ctrl_bus_lock() locks the BIF bus for exclusive use by the
422consumer. No other transactions will be allowed on the bus including those
423that would arise from battery insertion/removal or slave interrupt reception.
424This lock is primarily intended to be used along with the raw transaction
425functions. These functions allow a consumer to issue any BIF transaction
426including manufacturer specific bus commands not handled by the BIF framework.
427
428While performing raw transactions, features normally performed transparently by
429the core, such as device selection, are not available. The functions
430bif_slave_select() and bif_slave_is_selected() can be used to fill in this gap
431so that raw transactions are performed on the desired slave.
432
433Notify the BIF core that a battery has been inserted or removed:
434----------------------------------------------------------------
435
436int bif_ctrl_signal_battery_changed(struct bif_ctrl *ctrl);
437
438This function should only be called on systems where the BIF controller driver
439is architecturally unable to detect battery insertion and removal on its own.
440
441Perform BIF object CRC using CRC-CCITT algorithm:
442-------------------------------------------------
443
444u16 bif_crc_ccitt(const u8 *buffer, int len);
445
446Interface - BIF Controller API
447==============================
448
449BIF framework structs and functions used by BIF controllers are defined in:
450include/linux/bif/driver.h
451
452Ops found in struct bif_ctrl_ops:
453---------------------------------
454
455int (*bus_transaction) (struct bif_ctrl_dev *bdev, int transaction, u8 data);
456
457int (*bus_transaction_query) (struct bif_ctrl_dev *bdev, int transaction,
458 u8 data, bool *query_response);
459
460int (*bus_transaction_read) (struct bif_ctrl_dev *bdev, int transaction,
461 u8 data, int *response);
462
463int (*read_slave_registers) (struct bif_ctrl_dev *bdev, u16 addr,
464 u8 *data, int len);
465
466int (*write_slave_registers) (struct bif_ctrl_dev *bdev, u16 addr,
467 const u8 *data, int len);
468
469int (*get_bus_period) (struct bif_ctrl_dev *bdev);
470
471int (*set_bus_period) (struct bif_ctrl_dev *bdev, int period_ns);
472
473int (*get_battery_presence) (struct bif_ctrl_dev *bdev);
474
475int (*get_battery_rid) (struct bif_ctrl_dev *bdev);
476
477int (*get_bus_state) (struct bif_ctrl_dev *bdev);
478
479int (*set_bus_state) (struct bif_ctrl_dev *bdev, int state);
480
481A BIF controller driver registers a set of call back functions which instantiate
482these ops. The BIF framework then calls these functions based on internal and
483consumer needs.
484
485The ops bus_transaction(), bus_transaction_query(), and bus_transaction_read()
486carry out the controller hardware specific actions to perform BIF transactions
487on the BIF bus. These transactions result in no slave response, a pulse in
488response, or a word in response respectively. The ops read_slave_registers()
489and write_slave_registers() internally must perform all transactions necessary
490to read and write to BIF slave registers. These ops exist so that burst reads
491and writes can take place since these activities have very tight timing
492constraints that the BIF core cannot handle.
493
494The ops get_bus_period() and set_bus_period() return the current bus clock base
495period in nanoseconds and change the period to a new value respectively. The
496ops get_bus_state() and set_bus_state() allow for monitoring and controlling the
497bus state (i.e. active for communication, active waiting for interrupt, standby,
498or power down). The op get_battery_presence() returns if any battery pack
499(smart or low cost) is currently connected to the BCL. The op get_battery_rid()
500returns a best estimate of the Rid battery pack pull down ID resistance in ohms
501which can be used to determine if the battery pack is smart or low cost.
502
503Register/unregister a BIF controller:
504-------------------------------------
505
506struct bif_ctrl_dev *bif_ctrl_register(struct bif_ctrl_desc *bif_desc,
507 struct device *dev, void *driver_data, struct device_node *of_node);
508
509void bif_ctrl_unregister(struct bif_ctrl_dev *bdev);
510
511Notify the BIF framework that a battery has been inserted or removed:
512---------------------------------------------------------------------
513
514int bif_ctrl_notify_battery_changed(struct bif_ctrl_dev *bdev);
515
516The BIF core will then call the get_battery_presence() op internally to
517determine if the event is an insertion or removal.
518
519Notify the BIF framework that a slave interrupt has been received:
520------------------------------------------------------------------
521
522int bif_ctrl_notify_slave_irq(struct bif_ctrl_dev *bdev);
523
524Upon receiving this call, the BIF core interrogates each slave to determine
525which slaves have pending interrupts. It then iterates through all interrupts
526on those slaves clearing all pending interrupts and notifying any consumers
527waiting for the interrupts.
528
529Get BIF controller private data:
530--------------------------------
531
532void *bdev_get_drvdata(struct bif_ctrl_dev *bdev);
533
534Config options
535==============
536
537CONFIG_BIF - Enables BIF framework support.
538
539User space utilities
540====================
541
542No user space interface is provided in the BIF framework. Therefore, user
543space will not be able to directly use it.
544
545To do
546=====
547
548It is conceivable that the BIF framework should take some action during
549system suspend and resume. However, it is not clear exactly what should be
550done given that the BCL would still need to be active in order to detect
551battery removal while suspended.
552
553sysfs nodes could be added which describe slaves as well as functions and
554objects within the slaves. However these nodes would be read-only and would
555really only be useful for descriptive as opposed to control purposes.
556
557The exact time at which slave searching, function enumeration, and object
558loading takes place could be optimized in order to improve performance to
559some degree. It could also be made configurable at a controller level if
560needed.