blob: 3ba500d0c7a2d568cd6f63844edcd29842c83cb3 [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
238int bif_slave_match_count(const struct bif_ctrl *ctrl,
239 const struct bif_match_criteria *match_criteria);
240
241struct bif_slave *bif_slave_match_get(const struct bif_ctrl *ctrl,
242 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
296
297Read or write BIF slave registers:
298----------------------------------
299
300int bif_slave_read(struct bif_slave *slave, u16 addr, u8 *buf, int len);
301
302int bif_slave_write(struct bif_slave *slave, u16 addr, u8 *buf, int len);
303
304
305Get or set the BIF bus state or period:
306---------------------------------------
307
308int bif_ctrl_get_bus_state(struct bif_ctrl *ctrl);
309
310int bif_ctrl_set_bus_state(struct bif_ctrl *ctrl, enum bif_bus_state state);
311
312int bif_ctrl_get_bus_period(struct bif_ctrl *ctrl);
313
314int bif_ctrl_set_bus_period(struct bif_ctrl *ctrl, int period_ns);
315
316Bus states include: active for communication, active waiting for interrupt,
317standby, and power down. The MIPI-BIF specification defines the allowed range
318of bus periods as 2000 ns to 153000 ns. Individual controllers may further
319restrict the range of allowed periods. When bif_ctrl_set_bus_period() is called
320the first supported period that greater than or equal to the specified period
321will be set.
322
323Measure battery pack resistance:
324--------------------------------
325
326int bif_ctrl_measure_rid(struct bif_ctrl *ctrl);
327
328This function returns an estimate of the battery pack resistance in ohms. If
329no battery pack is connected, then the output of this function is undefined.
330
331Utilize BIF slave tasks and interrupts:
332---------------------------------------
333
334int bif_request_irq(struct bif_slave *slave, unsigned int task,
335 struct notifier_block *nb);
336
337int bif_free_irq(struct bif_slave *slave, unsigned int task,
338 struct notifier_block *nb);
339
340int bif_trigger_task(struct bif_slave *slave, unsigned int task);
341
342int bif_task_is_busy(struct bif_slave *slave, unsigned int task);
343
344A consumer can request a slave interrupt and specify a notifier to call when the
345interrupt is triggered. Once the interrupt is requested the consumer will need
346to call bif_trigger_task() in order to start the task associated with the
347interrupt (both are identified by the same index). Polling for task completion
348is also supported via the bif_task_is_busy() function.
349
350Raw BIF bus transactions:
351-------------------------
352
353void bif_ctrl_bus_lock(struct bif_ctrl *ctrl);
354
355void bif_ctrl_bus_unlock(struct bif_ctrl *ctrl);
356
357int bif_ctrl_raw_transaction(struct bif_ctrl *ctrl, int transaction, u8 data);
358
359int bif_ctrl_raw_transaction_read(struct bif_ctrl *ctrl, int transaction,
360 u8 data, int *response);
361
362int bif_ctrl_raw_transaction_query(struct bif_ctrl *ctrl, int transaction,
363 u8 data, bool *query_response);
364
365int bif_slave_is_selected(struct bif_slave *slave);
366
367int bif_slave_select(struct bif_slave *slave);
368
369The function bif_ctrl_bus_lock() locks the BIF bus for exclusive use by the
370consumer. No other transactions will be allowed on the bus including those
371that would arise from battery insertion/removal or slave interrupt reception.
372This lock is primarily intended to be used along with the raw transaction
373functions. These functions allow a consumer to issue any BIF transaction
374including manufacturer specific bus commands not handled by the BIF framework.
375
376While performing raw transactions, features normally performed transparently by
377the core, such as device selection, are not available. The functions
378bif_slave_select() and bif_slave_is_selected() can be used to fill in this gap
379so that raw transactions are performed on the desired slave.
380
381Notify the BIF core that a battery has been inserted or removed:
382----------------------------------------------------------------
383
384int bif_ctrl_signal_battery_changed(struct bif_ctrl *ctrl);
385
386This function should only be called on systems where the BIF controller driver
387is architecturally unable to detect battery insertion and removal on its own.
388
389Perform BIF object CRC using CRC-CCITT algorithm:
390-------------------------------------------------
391
392u16 bif_crc_ccitt(const u8 *buffer, int len);
393
394Interface - BIF Controller API
395==============================
396
397BIF framework structs and functions used by BIF controllers are defined in:
398include/linux/bif/driver.h
399
400Ops found in struct bif_ctrl_ops:
401---------------------------------
402
403int (*bus_transaction) (struct bif_ctrl_dev *bdev, int transaction, u8 data);
404
405int (*bus_transaction_query) (struct bif_ctrl_dev *bdev, int transaction,
406 u8 data, bool *query_response);
407
408int (*bus_transaction_read) (struct bif_ctrl_dev *bdev, int transaction,
409 u8 data, int *response);
410
411int (*read_slave_registers) (struct bif_ctrl_dev *bdev, u16 addr,
412 u8 *data, int len);
413
414int (*write_slave_registers) (struct bif_ctrl_dev *bdev, u16 addr,
415 const u8 *data, int len);
416
417int (*get_bus_period) (struct bif_ctrl_dev *bdev);
418
419int (*set_bus_period) (struct bif_ctrl_dev *bdev, int period_ns);
420
421int (*get_battery_presence) (struct bif_ctrl_dev *bdev);
422
423int (*get_battery_rid) (struct bif_ctrl_dev *bdev);
424
425int (*get_bus_state) (struct bif_ctrl_dev *bdev);
426
427int (*set_bus_state) (struct bif_ctrl_dev *bdev, int state);
428
429A BIF controller driver registers a set of call back functions which instantiate
430these ops. The BIF framework then calls these functions based on internal and
431consumer needs.
432
433The ops bus_transaction(), bus_transaction_query(), and bus_transaction_read()
434carry out the controller hardware specific actions to perform BIF transactions
435on the BIF bus. These transactions result in no slave response, a pulse in
436response, or a word in response respectively. The ops read_slave_registers()
437and write_slave_registers() internally must perform all transactions necessary
438to read and write to BIF slave registers. These ops exist so that burst reads
439and writes can take place since these activities have very tight timing
440constraints that the BIF core cannot handle.
441
442The ops get_bus_period() and set_bus_period() return the current bus clock base
443period in nanoseconds and change the period to a new value respectively. The
444ops get_bus_state() and set_bus_state() allow for monitoring and controlling the
445bus state (i.e. active for communication, active waiting for interrupt, standby,
446or power down). The op get_battery_presence() returns if any battery pack
447(smart or low cost) is currently connected to the BCL. The op get_battery_rid()
448returns a best estimate of the Rid battery pack pull down ID resistance in ohms
449which can be used to determine if the battery pack is smart or low cost.
450
451Register/unregister a BIF controller:
452-------------------------------------
453
454struct bif_ctrl_dev *bif_ctrl_register(struct bif_ctrl_desc *bif_desc,
455 struct device *dev, void *driver_data, struct device_node *of_node);
456
457void bif_ctrl_unregister(struct bif_ctrl_dev *bdev);
458
459Notify the BIF framework that a battery has been inserted or removed:
460---------------------------------------------------------------------
461
462int bif_ctrl_notify_battery_changed(struct bif_ctrl_dev *bdev);
463
464The BIF core will then call the get_battery_presence() op internally to
465determine if the event is an insertion or removal.
466
467Notify the BIF framework that a slave interrupt has been received:
468------------------------------------------------------------------
469
470int bif_ctrl_notify_slave_irq(struct bif_ctrl_dev *bdev);
471
472Upon receiving this call, the BIF core interrogates each slave to determine
473which slaves have pending interrupts. It then iterates through all interrupts
474on those slaves clearing all pending interrupts and notifying any consumers
475waiting for the interrupts.
476
477Get BIF controller private data:
478--------------------------------
479
480void *bdev_get_drvdata(struct bif_ctrl_dev *bdev);
481
482Config options
483==============
484
485CONFIG_BIF - Enables BIF framework support.
486
487User space utilities
488====================
489
490No user space interface is provided in the BIF framework. Therefore, user
491space will not be able to directly use it.
492
493To do
494=====
495
496It is conceivable that the BIF framework should take some action during
497system suspend and resume. However, it is not clear exactly what should be
498done given that the BCL would still need to be active in order to detect
499battery removal while suspended.
500
501sysfs nodes could be added which describe slaves as well as functions and
502objects within the slaves. However these nodes would be read-only and would
503really only be useful for descriptive as opposed to control purposes.
504
505The exact time at which slave searching, function enumeration, and object
506loading takes place could be optimized in order to improve performance to
507some degree. It could also be made configurable at a controller level if
508needed.