blob: 33ccb829d26074d885165ff089632a235b391942 [file] [log] [blame]
David Collins5fa19932013-01-23 14:10:28 -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/atomic.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/spmi.h>
26#include <linux/workqueue.h>
27#include <linux/bif/driver.h>
28#include <linux/qpnp/qpnp-adc.h>
29
30enum qpnp_bsi_irq {
31 QPNP_BSI_IRQ_ERR,
32 QPNP_BSI_IRQ_RX,
33 QPNP_BSI_IRQ_TX,
34 QPNP_BSI_IRQ_COUNT,
35};
36
37enum qpnp_bsi_com_mode {
38 QPNP_BSI_COM_MODE_IRQ,
39 QPNP_BSI_COM_MODE_POLL,
40};
41
42struct qpnp_bsi_chip {
43 struct bif_ctrl_desc bdesc;
44 struct spmi_device *spmi_dev;
45 struct bif_ctrl_dev *bdev;
46 struct work_struct slave_irq_work;
47 u16 base_addr;
48 u16 batt_id_stat_addr;
49 int r_pullup_ohm;
50 int vid_ref_uV;
51 int tau_index;
52 int tau_sampling_mask;
53 enum bif_bus_state state;
54 enum qpnp_bsi_com_mode com_mode;
55 int irq[QPNP_BSI_IRQ_COUNT];
56 atomic_t irq_flag[QPNP_BSI_IRQ_COUNT];
57 int batt_present_irq;
58 enum qpnp_vadc_channels batt_id_adc_channel;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -070059 struct qpnp_vadc_chip *vadc_dev;
David Collins5fa19932013-01-23 14:10:28 -080060};
61
62#define QPNP_BSI_DRIVER_NAME "qcom,qpnp-bsi"
63
64enum qpnp_bsi_registers {
65 QPNP_BSI_REG_TYPE = 0x04,
66 QPNP_BSI_REG_SUBTYPE = 0x05,
67 QPNP_BSI_REG_STATUS = 0x08,
68 QPNP_BSI_REG_ENABLE = 0x46,
69 QPNP_BSI_REG_CLEAR_ERROR = 0x4F,
70 QPNP_BSI_REG_FORCE_BCL_LOW = 0x51,
71 QPNP_BSI_REG_TAU_CONFIG = 0x52,
72 QPNP_BSI_REG_MODE = 0x53,
73 QPNP_BSI_REG_RX_TX_ENABLE = 0x54,
74 QPNP_BSI_REG_TX_DATA_LOW = 0x5A,
75 QPNP_BSI_REG_TX_DATA_HIGH = 0x5B,
76 QPNP_BSI_REG_TX_CTRL = 0x5D,
77 QPNP_BSI_REG_RX_DATA_LOW = 0x60,
78 QPNP_BSI_REG_RX_DATA_HIGH = 0x61,
79 QPNP_BSI_REG_RX_SOURCE = 0x62,
80 QPNP_BSI_REG_BSI_ERROR = 0x70,
81};
82
83#define QPNP_BSI_TYPE 0x02
84#define QPNP_BSI_SUBTYPE 0x10
85
86#define QPNP_BSI_STATUS_ERROR 0x10
87#define QPNP_BSI_STATUS_TX_BUSY 0x08
88#define QPNP_BSI_STATUS_RX_BUSY 0x04
89#define QPNP_BSI_STATUS_TX_GO_BUSY 0x02
90#define QPNP_BSI_STATUS_RX_DATA_READY 0x01
91
92#define QPNP_BSI_ENABLE_MASK 0x80
93#define QPNP_BSI_ENABLE 0x80
94#define QPNP_BSI_DISABLE 0x00
95
96#define QPNP_BSI_TAU_CONFIG_SAMPLE_MASK 0x10
97#define QPNP_BSI_TAU_CONFIG_SAMPLE_8X 0x10
98#define QPNP_BSI_TAU_CONFIG_SAMPLE_4X 0x00
99#define QPNP_BSI_TAU_CONFIG_SPEED_MASK 0x07
100
101#define QPNP_BSI_MODE_TX_PULSE_MASK 0x10
102#define QPNP_BSI_MODE_TX_PULSE_INT 0x10
103#define QPNP_BSI_MODE_TX_PULSE_DATA 0x00
104#define QPNP_BSI_MODE_RX_PULSE_MASK 0x08
105#define QPNP_BSI_MODE_RX_PULSE_INT 0x08
106#define QPNP_BSI_MODE_RX_PULSE_DATA 0x00
107#define QPNP_BSI_MODE_TX_PULSE_T_MASK 0x04
108#define QPNP_BSI_MODE_TX_PULSE_T_WAKE 0x04
109#define QPNP_BSI_MODE_TX_PULSE_T_1_TAU 0x00
110#define QPNP_BSI_MODE_RX_FORMAT_MASK 0x02
111#define QPNP_BSI_MODE_RX_FORMAT_17_BIT 0x02
112#define QPNP_BSI_MODE_RX_FORMAT_11_BIT 0x00
113#define QPNP_BSI_MODE_TX_FORMAT_MASK 0x01
114#define QPNP_BSI_MODE_TX_FORMAT_17_BIT 0x01
115#define QPNP_BSI_MODE_TX_FORMAT_11_BIT 0x00
116
117#define QPNP_BSI_TX_ENABLE_MASK 0x80
118#define QPNP_BSI_TX_ENABLE 0x80
119#define QPNP_BSI_TX_DISABLE 0x00
120#define QPNP_BSI_RX_ENABLE_MASK 0x40
121#define QPNP_BSI_RX_ENABLE 0x40
122#define QPNP_BSI_RX_DISABLE 0x00
123
124#define QPNP_BSI_TX_DATA_HIGH_MASK 0x07
125
126#define QPNP_BSI_TX_CTRL_GO 0x01
127
128#define QPNP_BSI_RX_DATA_HIGH_MASK 0x07
129
130#define QPNP_BSI_RX_SRC_LOOPBACK_FLAG 0x10
131
132#define QPNP_BSI_BSI_ERROR_CLEAR 0x80
133
134#define QPNP_SMBB_BAT_IF_BATT_PRES_MASK 0x80
135#define QPNP_SMBB_BAT_IF_BATT_ID_MASK 0x01
136
137#define QPNP_BSI_NUM_CLOCK_PERIODS 8
138
139struct qpnp_bsi_tau {
140 int period_4x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
141 int period_8x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
142 int period_4x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
143 int period_8x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
144};
145
146/* Tau BIF clock periods in ns supported by BSI for either 4x or 8x sampling. */
147static const struct qpnp_bsi_tau qpnp_bsi_tau_period = {
148 .period_4x_ns = {
149 150420, 122080, 61040, 31670, 15830, 7920, 3960, 2080
150 },
151 .period_8x_ns = {
152 150420, 122080, 63330, 31670, 15830, 7920, 4170, 2080
153 },
154 .period_4x_us = {
155 151, 122, 61, 32, 16, 8, 4, 2
156 },
157 .period_8x_us = {
158 151, 122, 64, 32, 16, 8, 4, 2
159 },
160
161};
162#define QPNP_BSI_MIN_CLOCK_SPEED_NS 2080
163#define QPNP_BSI_MAX_CLOCK_SPEED_NS 150420
164
165#define QPNP_BSI_MIN_PULLUP_OHM 1000
166#define QPNP_BSI_MAX_PULLUP_OHM 500000
167#define QPNP_BSI_DEFAULT_PULLUP_OHM 100000
168#define QPNP_BSI_MIN_VID_REF_UV 500000
169#define QPNP_BSI_MAX_VID_REF_UV 5000000
170#define QPNP_BSI_DEFAULT_VID_REF_UV 1800000
171
172/* These have units of tau_bif. */
David Collins9941f402013-09-04 17:07:06 -0700173#define QPNP_BSI_MAX_TRANSMIT_CYCLES 46
David Collins5fa19932013-01-23 14:10:28 -0800174#define QPNP_BSI_MIN_RECEIVE_CYCLES 24
175#define QPNP_BSI_MAX_BUS_QUERY_CYCLES 17
176
177/*
178 * Maximum time in microseconds for a slave to transition from suspend to active
179 * state.
180 */
181#define QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US 50
182
183/*
184 * Maximum time in milliseconds for a slave to transition from power down to
185 * active state.
186 */
187#define QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS 10
188
189#define QPNP_BSI_POWER_UP_LOW_DELAY_US 240
190
191/*
192 * Latencies that are used when determining if polling or interrupts should be
193 * used for a given transaction.
194 */
195#define QPNP_BSI_MAX_IRQ_LATENCY_US 170
196#define QPNP_BSI_MAX_BSI_DATA_READ_LATENCY_US 16
197
198static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state);
199
200static inline int qpnp_bsi_read(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
201 int len)
202{
203 int rc;
204
205 rc = spmi_ext_register_readl(chip->spmi_dev->ctrl,
206 chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
207 if (rc)
208 dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
209 __func__, chip->spmi_dev->sid, chip->base_addr + addr,
210 len, rc);
211
212 return rc;
213}
214
215static inline int qpnp_bsi_write(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
216 int len)
217{
218 int rc;
219
220 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
221 chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
222
223 if (rc)
224 dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_writel() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
225 __func__, chip->spmi_dev->sid, chip->base_addr + addr,
226 len, rc);
227
228 return rc;
229}
230
231enum qpnp_bsi_rx_tx_state {
232 QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF,
233 QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA,
234 QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT,
235 QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA,
236 QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA,
237 QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF,
238};
239
240static int qpnp_bsi_rx_tx_config(struct qpnp_bsi_chip *chip,
241 enum qpnp_bsi_rx_tx_state state)
242{
243 u8 buf[2] = {0, 0};
244 int rc;
245
246 buf[0] = QPNP_BSI_MODE_TX_FORMAT_11_BIT
247 | QPNP_BSI_MODE_RX_FORMAT_11_BIT;
248
249 switch (state) {
250 case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF:
251 buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
252 QPNP_BSI_MODE_RX_PULSE_DATA;
253 buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
254 break;
255 case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA:
256 buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
257 QPNP_BSI_MODE_RX_PULSE_DATA;
258 buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
259 break;
260 case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT:
261 buf[0] |= QPNP_BSI_MODE_TX_PULSE_INT |
262 QPNP_BSI_MODE_RX_PULSE_DATA;
263 buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
264 break;
265 case QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA:
266 buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
267 QPNP_BSI_MODE_RX_PULSE_INT;
268 buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
269 break;
270 case QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA:
271 buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
272 QPNP_BSI_MODE_RX_PULSE_DATA;
273 buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
274 break;
275 case QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF:
276 buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
277 QPNP_BSI_MODE_RX_PULSE_INT;
278 buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
279 break;
280 default:
281 dev_err(&chip->spmi_dev->dev, "%s: invalid state=%d\n",
282 __func__, state);
283 return -EINVAL;
284 }
285
286 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
287 if (rc)
288 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
289 __func__, rc);
290
291 return rc;
292}
293
294static void qpnp_bsi_slave_irq_work(struct work_struct *work)
295{
296 struct qpnp_bsi_chip *chip
297 = container_of(work, struct qpnp_bsi_chip, slave_irq_work);
298 int rc;
299
300 rc = bif_ctrl_notify_slave_irq(chip->bdev);
301 if (rc)
302 pr_err("Could not notify BIF core about slave interrupt, rc=%d\n",
303 rc);
304}
305
306static irqreturn_t qpnp_bsi_isr(int irq, void *data)
307{
308 struct qpnp_bsi_chip *chip = data;
309 bool found = false;
310 int i;
311
312 for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++) {
313 if (irq == chip->irq[i]) {
314 found = true;
315 atomic_cmpxchg(&chip->irq_flag[i], 0, 1);
316
317 /* Check if this is a slave interrupt. */
318 if (i == QPNP_BSI_IRQ_RX
319 && chip->state == BIF_BUS_STATE_INTERRUPT) {
320 /* Slave IRQ makes the bus active. */
321 qpnp_bsi_rx_tx_config(chip,
322 QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
323 chip->state = BIF_BUS_STATE_ACTIVE;
324 schedule_work(&chip->slave_irq_work);
325 }
326 }
327 }
328
329 if (!found)
330 pr_err("Unknown interrupt: %d\n", irq);
331
332 return IRQ_HANDLED;
333}
334
335static irqreturn_t qpnp_bsi_batt_present_isr(int irq, void *data)
336{
337 struct qpnp_bsi_chip *chip = data;
338 int rc;
339
340 if (!chip->bdev)
341 return IRQ_HANDLED;
342
343 rc = bif_ctrl_notify_battery_changed(chip->bdev);
344 if (rc)
345 pr_err("Could not notify about battery state change, rc=%d\n",
346 rc);
347
348 return IRQ_HANDLED;
349}
350
351static void qpnp_bsi_set_com_mode(struct qpnp_bsi_chip *chip,
352 enum qpnp_bsi_com_mode mode)
353{
354 int i;
355
356 if (chip->com_mode == mode)
357 return;
358
359 if (mode == QPNP_BSI_COM_MODE_IRQ)
360 for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
361 enable_irq(chip->irq[i]);
362 else
363 for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
364 disable_irq(chip->irq[i]);
365
366 chip->com_mode = mode;
367}
368
369static inline bool qpnp_bsi_check_irq(struct qpnp_bsi_chip *chip, int irq)
370{
371 return atomic_cmpxchg(&chip->irq_flag[irq], 1, 0);
372}
373
374static void qpnp_bsi_clear_irq_flags(struct qpnp_bsi_chip *chip)
375{
376 int i;
377
378 for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
379 atomic_set(&chip->irq_flag[i], 0);
380}
381
382static inline int qpnp_bsi_get_tau_ns(struct qpnp_bsi_chip *chip)
383{
384 if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
385 return qpnp_bsi_tau_period.period_4x_ns[chip->tau_index];
386 else
387 return qpnp_bsi_tau_period.period_8x_ns[chip->tau_index];
388}
389
390static inline int qpnp_bsi_get_tau_us(struct qpnp_bsi_chip *chip)
391{
392 if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
393 return qpnp_bsi_tau_period.period_4x_us[chip->tau_index];
394 else
395 return qpnp_bsi_tau_period.period_8x_us[chip->tau_index];
396}
397
398/* Checks if BSI is in an error state and clears the error if it is. */
399static int qpnp_bsi_clear_bsi_error(struct qpnp_bsi_chip *chip)
400{
401 int rc, delay_us;
402 u8 reg;
403
404 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
405 if (rc) {
406 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
407 __func__, rc);
408 return rc;
409 }
410
411 if (reg > 0) {
412 /*
413 * Delay before clearing the BSI error in case a transaction is
414 * still in flight.
415 */
416 delay_us = QPNP_BSI_MAX_TRANSMIT_CYCLES
417 * qpnp_bsi_get_tau_us(chip);
418 udelay(delay_us);
419
420 pr_info("PMIC BSI module in error state, error=%d\n", reg);
421
422 reg = QPNP_BSI_BSI_ERROR_CLEAR;
423 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_CLEAR_ERROR, &reg, 1);
424 if (rc)
425 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
426 __func__, rc);
427 }
428
429 return rc;
430}
431
432static int qpnp_bsi_get_bsi_error(struct qpnp_bsi_chip *chip)
433{
434 int rc;
435 u8 reg;
436
437 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
438 if (rc) {
439 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
440 __func__, rc);
441 return rc;
442 }
443
444 return reg;
445}
446
447static int qpnp_bsi_wait_for_tx(struct qpnp_bsi_chip *chip, int timeout)
448{
449 int rc = 0;
450
451 /* Wait for TX or ERR IRQ. */
452 while (timeout > 0) {
453 if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
454 dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
455 __func__, qpnp_bsi_get_bsi_error(chip));
456 return -EIO;
457 }
458
459 if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_TX))
460 break;
461
462 udelay(1);
463 timeout--;
464 }
465
466 if (timeout == 0) {
467 rc = -ETIMEDOUT;
468 dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, no interrupts received, rc=%d\n",
469 __func__, rc);
470 return rc;
471 }
472
473 return rc;
474}
475
476static int qpnp_bsi_issue_transaction(struct qpnp_bsi_chip *chip,
477 int transaction, u8 data)
478{
479 int rc;
480 u8 buf[4];
481
482 /* MIPI_BIF_DATA_TX_0 = BIF word bits 7 to 0 */
483 buf[0] = data;
484 /* MIPI_BIF_DATA_TX_1 = BIF word BCF, bits 9 to 8 */
485 buf[1] = transaction & QPNP_BSI_TX_DATA_HIGH_MASK;
486 /* MIPI_BIF_DATA_TX_2 ignored */
487 buf[2] = 0x00;
488 /* MIPI_BIF_TX_CTL bit 0 written to start the transaction. */
489 buf[3] = QPNP_BSI_TX_CTRL_GO;
490
491 /* Write the TX_DATA bytes and initiate the transaction. */
492 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_DATA_LOW, buf, 4);
493 if (rc)
494 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
495 __func__, rc);
496 return rc;
497}
498
499static int qpnp_bsi_issue_transaction_wait_for_tx(struct qpnp_bsi_chip *chip,
500 int transaction, u8 data)
501{
502 int rc, timeout;
503
504 rc = qpnp_bsi_issue_transaction(chip, transaction, data);
505 if (rc)
506 return rc;
507
508 timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
509 + QPNP_BSI_MAX_IRQ_LATENCY_US;
510
511 rc = qpnp_bsi_wait_for_tx(chip, timeout);
512
513 return rc;
514}
515
516static int qpnp_bsi_wait_for_rx(struct qpnp_bsi_chip *chip, int timeout)
517{
518 int rc = 0;
519
520 /* Wait for RX IRQ to indicate that data is ready to read. */
521 while (timeout > 0) {
522 if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
523 dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
524 __func__, qpnp_bsi_get_bsi_error(chip));
525 return -EIO;
526 }
527
528 if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_RX))
529 break;
530
531 udelay(1);
532 timeout--;
533 }
534
535 if (timeout == 0)
536 rc = -ETIMEDOUT;
537
538 return rc;
539}
540
541static int qpnp_bsi_bus_transaction(struct bif_ctrl_dev *bdev, int transaction,
542 u8 data)
543{
544 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
545 int rc;
546
David Collins5fa19932013-01-23 14:10:28 -0800547 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
548
549 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
550 if (rc) {
551 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
552 __func__, rc);
553 return rc;
554 }
555
556 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
557 if (rc)
558 return rc;
559
David Collins29901022013-05-03 17:08:30 -0700560 rc = qpnp_bsi_clear_bsi_error(chip);
561 if (rc)
562 return rc;
563
564 qpnp_bsi_clear_irq_flags(chip);
565
David Collins5fa19932013-01-23 14:10:28 -0800566 rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
567 if (rc)
568 return rc;
569
570 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
571
572 return rc;
573}
574
575static int qpnp_bsi_bus_transaction_query(struct bif_ctrl_dev *bdev,
576 int transaction, u8 data, bool *query_response)
577{
578 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
579 int rc, timeout;
580
David Collins5fa19932013-01-23 14:10:28 -0800581 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
582
583 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
584 if (rc) {
585 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
586 __func__, rc);
587 return rc;
588 }
589
590 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
591 if (rc)
592 return rc;
593
David Collins29901022013-05-03 17:08:30 -0700594 rc = qpnp_bsi_clear_bsi_error(chip);
595 if (rc)
596 return rc;
597
598 qpnp_bsi_clear_irq_flags(chip);
599
David Collins5fa19932013-01-23 14:10:28 -0800600 rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
601 if (rc)
602 return rc;
603
604 timeout = QPNP_BSI_MAX_BUS_QUERY_CYCLES * qpnp_bsi_get_tau_us(chip)
605 + QPNP_BSI_MAX_IRQ_LATENCY_US;
606
607 rc = qpnp_bsi_wait_for_rx(chip, timeout);
608 if (rc == 0) {
609 *query_response = true;
610 } else if (rc == -ETIMEDOUT) {
611 *query_response = false;
612 rc = 0;
613 }
614
615 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
616
617 return rc;
618}
619
620static int qpnp_bsi_bus_transaction_read(struct bif_ctrl_dev *bdev,
621 int transaction, u8 data, int *response)
622{
623 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
624 int rc, timeout;
625 u8 buf[3];
626
David Collins5fa19932013-01-23 14:10:28 -0800627 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
628
629 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
630 if (rc) {
631 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
632 __func__, rc);
633 return rc;
634 }
635
636 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
637 if (rc)
638 return rc;
639
David Collins29901022013-05-03 17:08:30 -0700640 rc = qpnp_bsi_clear_bsi_error(chip);
641 if (rc)
642 return rc;
643
644 qpnp_bsi_clear_irq_flags(chip);
645
David Collins5fa19932013-01-23 14:10:28 -0800646 rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
647 if (rc)
648 return rc;
649
650 timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
651 + QPNP_BSI_MAX_IRQ_LATENCY_US;
652
653 rc = qpnp_bsi_wait_for_rx(chip, timeout);
654 if (rc) {
655 if (rc == -ETIMEDOUT) {
656 /*
657 * No error message is printed in this case in order
658 * to provide silent operation when checking if a slave
659 * is selected using the transaction query bus command.
660 */
661 dev_dbg(&chip->spmi_dev->dev, "%s: transaction timed out, no interrupts received, rc=%d\n",
662 __func__, rc);
663 }
664 return rc;
665 }
666
667 /* Read the RX_DATA bytes. */
668 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf, 3);
669 if (rc) {
670 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
671 __func__, rc);
672 return rc;
673 }
674
675 if (buf[2] & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
676 rc = -EIO;
677 dev_err(&chip->spmi_dev->dev, "%s: unexpected loopback data read, rc=%d\n",
678 __func__, rc);
679 return rc;
680 }
681
682 *response = ((int)(buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8) | buf[0];
683
684 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
685
686 return 0;
687}
688
689/*
690 * Wait for RX_FLOW_STATUS to be set to 1 which indicates that another BIF word
691 * can be read from PMIC registers.
692 */
693static int qpnp_bsi_wait_for_rx_data(struct qpnp_bsi_chip *chip)
694{
695 int rc = 0;
696 int timeout;
697 u8 reg;
698
699 timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
700
701 /* Wait for RX_FLOW_STATUS == 1 or ERR_FLAG == 1. */
702 while (timeout > 0) {
703 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
704 if (rc) {
705 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
706 __func__, rc);
707 return rc;
708 }
709
710 if (reg & QPNP_BSI_STATUS_ERROR) {
711 dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
712 __func__, qpnp_bsi_get_bsi_error(chip));
713 return -EIO;
714 }
715
716 if (reg & QPNP_BSI_STATUS_RX_DATA_READY) {
717 /* BSI RX has data word latched. */
718 return 0;
719 }
720
721 udelay(1);
722 timeout--;
723 }
724
725 rc = -ETIMEDOUT;
726 dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, RX_FLOW_STATUS never set to 1, rc=%d\n",
727 __func__, rc);
728
729 return rc;
730}
731
732/*
733 * Wait for TX_GO_STATUS to be set to 0 which indicates that another BIF word
734 * can be enqueued.
735 */
736static int qpnp_bsi_wait_for_tx_go(struct qpnp_bsi_chip *chip)
737{
738 int rc = 0;
739 int timeout;
740 u8 reg;
741
742 timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
743
744 /* Wait for TX_GO_STATUS == 0 or ERR_FLAG == 1. */
745 while (timeout > 0) {
746 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
747 if (rc) {
748 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
749 __func__, rc);
750 return rc;
751 }
752
753 if (reg & QPNP_BSI_STATUS_ERROR) {
754 dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
755 __func__, qpnp_bsi_get_bsi_error(chip));
756 return -EIO;
757 }
758
759 if (!(reg & QPNP_BSI_STATUS_TX_GO_BUSY)) {
760 /* BSI TX is ready to accept the next word. */
761 return 0;
762 }
763
764 udelay(1);
765 timeout--;
766 }
767
768 rc = -ETIMEDOUT;
769 dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, TX_GO_STATUS never set to 0, rc=%d\n",
770 __func__, rc);
771
772 return rc;
773}
774
775/*
776 * Wait for TX_BUSY to be set to 0 which indicates that the TX data has been
777 * successfully transmitted.
778 */
779static int qpnp_bsi_wait_for_tx_idle(struct qpnp_bsi_chip *chip)
780{
781 int rc = 0;
782 int timeout;
783 u8 reg;
784
785 timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
786
787 /* Wait for TX_BUSY == 0 or ERR_FLAG == 1. */
788 while (timeout > 0) {
789 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
790 if (rc) {
791 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
792 __func__, rc);
793 return rc;
794 }
795
796 if (reg & QPNP_BSI_STATUS_ERROR) {
797 dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
798 __func__, qpnp_bsi_get_bsi_error(chip));
799 return -EIO;
800 }
801
802 if (!(reg & QPNP_BSI_STATUS_TX_BUSY)) {
803 /* BSI TX is idle. */
804 return 0;
805 }
806
807 udelay(1);
808 timeout--;
809 }
810
811 rc = -ETIMEDOUT;
812 dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, TX_BUSY never set to 0, rc=%d\n",
813 __func__, rc);
814
815 return rc;
816}
817
818/*
819 * For burst read length greater than 1, send necessary RBL and RBE BIF bus
820 * commands.
821 */
822static int qpnp_bsi_send_burst_length(struct qpnp_bsi_chip *chip, int burst_len)
823{
824 int rc = 0;
825
826 /*
827 * Send burst read length bus commands according to the following:
828 *
David Collins5aefcd92013-03-08 13:36:34 -0800829 * 1 --> No RBE or RBL
830 * 2 - 15 = x --> RBLx
831 * 16 - 255 = 16 * y + x --> RBEy and RBLx (RBL0 not sent)
832 * 256 --> RBL0
David Collins5fa19932013-01-23 14:10:28 -0800833 */
834 if (burst_len == 256) {
835 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
836 BIF_CMD_RBL);
837 if (rc)
838 return rc;
839
840 rc = qpnp_bsi_wait_for_tx_go(chip);
841 if (rc)
842 return rc;
843 } else if (burst_len >= 16) {
844 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
845 BIF_CMD_RBE + (burst_len / 16));
846 if (rc)
847 return rc;
848
849 rc = qpnp_bsi_wait_for_tx_go(chip);
850 if (rc)
851 return rc;
852 }
853
David Collins5aefcd92013-03-08 13:36:34 -0800854 if (burst_len % 16 && burst_len > 1) {
David Collins5fa19932013-01-23 14:10:28 -0800855 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
856 BIF_CMD_RBL + (burst_len % 16));
857 if (rc)
858 return rc;
859
860 rc = qpnp_bsi_wait_for_tx_go(chip);
861 if (rc)
862 return rc;
863 }
864
865 return rc;
866}
867
868/* Perform validation steps on received BIF data. */
869static int qpnp_bsi_validate_rx_data(struct qpnp_bsi_chip *chip, int response,
870 u8 rx2_data, bool last_word)
871{
872 int err = -EIO;
873
874 if (rx2_data & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
875 dev_err(&chip->spmi_dev->dev, "%s: unexpected loopback data read, rc=%d\n",
876 __func__, err);
877 return err;
878 }
879
880 if (!(response & BIF_SLAVE_RD_ACK)) {
881 dev_err(&chip->spmi_dev->dev, "%s: BIF register read error=0x%02X\n",
882 __func__, response & BIF_SLAVE_RD_ERR);
883 return err;
884 }
885
886 if (last_word && !(response & BIF_SLAVE_RD_EOT)) {
887 dev_err(&chip->spmi_dev->dev, "%s: BIF register read error, last RD packet has EOT=0\n",
888 __func__);
889 return err;
890 } else if (!last_word && (response & BIF_SLAVE_RD_EOT)) {
891 dev_err(&chip->spmi_dev->dev, "%s: BIF register read error, RD packet other than last has EOT=1\n",
892 __func__);
893 return err;
894 }
895
896 return 0;
897}
898
899/* Performs all BIF transactions in order to utilize burst reads. */
900static int qpnp_bsi_read_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
901 u8 *data, int len)
902{
903 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
904 int response = 0;
905 unsigned long flags;
906 int rc, rc2, i, burst_len;
907 u8 buf[3];
908
David Collins5fa19932013-01-23 14:10:28 -0800909 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);
910
911 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
912 if (rc) {
913 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
914 __func__, rc);
915 return rc;
916 }
917
918 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
919 if (rc)
920 return rc;
921
David Collins29901022013-05-03 17:08:30 -0700922 rc = qpnp_bsi_clear_bsi_error(chip);
923 if (rc)
924 return rc;
925
926 qpnp_bsi_clear_irq_flags(chip);
927
David Collins5fa19932013-01-23 14:10:28 -0800928 while (len > 0) {
929 burst_len = min(len, 256);
930
931 rc = qpnp_bsi_send_burst_length(chip, burst_len);
932 if (rc)
933 return rc;
934
935 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
936 if (rc)
937 return rc;
938
939 rc = qpnp_bsi_wait_for_tx_go(chip);
940 if (rc)
941 return rc;
942
943 /* Perform burst read in atomic context. */
944 local_irq_save(flags);
945
946 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_RRA,
947 addr & 0xFF);
948 if (rc)
949 goto burst_err;
950
951 for (i = 0; i < burst_len; i++) {
952 rc = qpnp_bsi_wait_for_rx_data(chip);
953 if (rc)
954 goto burst_err;
955
956 /* Read the RX_DATA bytes. */
957 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf,
958 3);
959 if (rc) {
960 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
961 __func__, rc);
962 goto burst_err;
963 }
964
965 response = ((buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8)
966 | buf[0];
967
968 rc = qpnp_bsi_validate_rx_data(chip, response, buf[2],
969 i == burst_len - 1);
970 if (rc)
971 goto burst_err;
972
973 data[i] = buf[0];
974 }
975 local_irq_restore(flags);
976
977 addr += burst_len;
978 data += burst_len;
979 len -= burst_len;
980 }
981
982 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
983
984 return rc;
985
986burst_err:
987 local_irq_restore(flags);
988
989 rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
990 if (rc2 < 0)
991 rc = rc2;
992
993 return rc;
994}
995
996/* Performs all BIF transactions in order to utilize burst writes. */
997static int qpnp_bsi_write_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
998 const u8 *data, int len)
999{
1000 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1001 unsigned long flags;
1002 int rc, rc2, i;
1003
David Collins5fa19932013-01-23 14:10:28 -08001004 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);
1005
1006 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
1007 if (rc) {
1008 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
1009 __func__, rc);
1010 return rc;
1011 }
1012
1013 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
1014 if (rc)
1015 return rc;
1016
David Collins29901022013-05-03 17:08:30 -07001017 rc = qpnp_bsi_clear_bsi_error(chip);
1018 if (rc)
1019 return rc;
1020
1021 qpnp_bsi_clear_irq_flags(chip);
1022
David Collins5fa19932013-01-23 14:10:28 -08001023 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
1024 if (rc)
1025 return rc;
1026
1027 rc = qpnp_bsi_wait_for_tx_go(chip);
1028 if (rc)
1029 return rc;
1030
1031 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WRA, addr & 0xFF);
1032 if (rc)
1033 return rc;
1034
1035 rc = qpnp_bsi_wait_for_tx_go(chip);
1036 if (rc)
1037 return rc;
1038
1039 /* Perform burst write in atomic context. */
1040 local_irq_save(flags);
1041
1042 for (i = 0; i < len; i++) {
1043 rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WD, data[i]);
1044 if (rc)
1045 goto burst_err;
1046
1047 rc = qpnp_bsi_wait_for_tx_go(chip);
1048 if (rc)
1049 goto burst_err;
1050 }
1051
1052 rc = qpnp_bsi_wait_for_tx_idle(chip);
1053 if (rc)
1054 goto burst_err;
1055
1056 local_irq_restore(flags);
1057
1058 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
1059
1060 return rc;
1061
1062burst_err:
1063 local_irq_restore(flags);
1064
1065 rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
1066 if (rc2 < 0)
1067 rc = rc2;
1068
1069 return rc;
1070}
1071
1072
1073static int qpnp_bsi_bus_set_interrupt_mode(struct bif_ctrl_dev *bdev)
1074{
1075 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1076 int rc;
1077
David Collins5fa19932013-01-23 14:10:28 -08001078 qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
1079
1080 /*
1081 * Temporarily change the bus to active state so that the EINT command
1082 * can be issued.
1083 */
1084 rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
1085 if (rc) {
1086 dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
1087 __func__, rc);
1088 return rc;
1089 }
1090
1091 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
1092 if (rc)
1093 return rc;
1094
1095 /*
1096 * Set the bus state to interrupt mode so that an RX interrupt which
1097 * occurs immediately after issuing the EINT command is handled
1098 * properly.
1099 */
1100 chip->state = BIF_BUS_STATE_INTERRUPT;
1101
David Collins29901022013-05-03 17:08:30 -07001102 rc = qpnp_bsi_clear_bsi_error(chip);
1103 if (rc)
1104 return rc;
1105
1106 qpnp_bsi_clear_irq_flags(chip);
1107
David Collins5fa19932013-01-23 14:10:28 -08001108 /* Send EINT bus command. */
1109 rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, BIF_TRANS_BC,
1110 BIF_CMD_EINT);
1111 if (rc)
1112 return rc;
1113
1114 rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF);
1115
1116 return rc;
1117}
1118
1119static int qpnp_bsi_bus_set_active_mode(struct bif_ctrl_dev *bdev,
1120 int prev_state)
1121{
1122 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1123 int rc;
1124 u8 buf[2];
1125
1126 rc = qpnp_bsi_clear_bsi_error(chip);
1127 if (rc)
1128 return rc;
1129
1130 buf[0] = QPNP_BSI_MODE_TX_PULSE_INT |
1131 QPNP_BSI_MODE_RX_PULSE_DATA;
1132 buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
1133
1134 if (prev_state == BIF_BUS_STATE_INTERRUPT)
1135 buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_1_TAU;
1136 else
1137 buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_WAKE;
1138
1139 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
1140 if (rc) {
1141 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
1142 __func__, rc);
1143 return rc;
1144 }
1145
1146 buf[0] = QPNP_BSI_TX_CTRL_GO;
1147 /* Initiate BCL low pulse. */
1148 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_CTRL, buf, 1);
1149 if (rc) {
1150 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
1151 __func__, rc);
1152 return rc;
1153 }
1154
1155 switch (prev_state) {
1156 case BIF_BUS_STATE_INTERRUPT:
1157 udelay(qpnp_bsi_get_tau_us(chip) * 4);
1158 break;
1159 case BIF_BUS_STATE_STANDBY:
1160 udelay(qpnp_bsi_get_tau_us(chip)
1161 + QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US
1162 + QPNP_BSI_POWER_UP_LOW_DELAY_US);
1163 break;
1164 case BIF_BUS_STATE_POWER_DOWN:
David Collins2b2a76d2013-09-12 13:13:22 -07001165 case BIF_BUS_STATE_MASTER_DISABLED:
David Collins5fa19932013-01-23 14:10:28 -08001166 msleep(QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS);
1167 break;
1168 }
1169
1170 return rc;
1171}
1172
1173static int qpnp_bsi_get_bus_state(struct bif_ctrl_dev *bdev)
1174{
1175 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1176
1177 return chip->state;
1178}
1179
1180static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state)
1181{
1182 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1183 int rc = 0;
David Collins2b2a76d2013-09-12 13:13:22 -07001184 u8 reg;
David Collins5fa19932013-01-23 14:10:28 -08001185
1186 if (state == chip->state)
1187 return 0;
1188
David Collins2b2a76d2013-09-12 13:13:22 -07001189 if (chip->state == BIF_BUS_STATE_MASTER_DISABLED) {
1190 /*
1191 * Enable the BSI peripheral when transitioning from a disabled
1192 * bus state to any of the active bus states so that BIF
1193 * transactions can take place.
1194 */
1195 reg = QPNP_BSI_ENABLE;
1196 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
1197 if (rc) {
1198 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
1199 __func__, rc);
1200 return rc;
1201 }
1202 }
1203
David Collins5fa19932013-01-23 14:10:28 -08001204 switch (state) {
1205 case BIF_BUS_STATE_MASTER_DISABLED:
David Collins2b2a76d2013-09-12 13:13:22 -07001206 /* Disable the BSI peripheral. */
1207 reg = QPNP_BSI_DISABLE;
1208 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
1209 if (rc)
1210 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
1211 __func__, rc);
David Collins5fa19932013-01-23 14:10:28 -08001212 break;
1213 case BIF_BUS_STATE_POWER_DOWN:
1214 rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_PDWN);
1215 if (rc)
1216 dev_err(&chip->spmi_dev->dev, "%s: failed to enable power down mode, rc=%d\n",
1217 __func__, rc);
1218 break;
1219 case BIF_BUS_STATE_STANDBY:
1220 rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_STBY);
1221 if (rc)
1222 dev_err(&chip->spmi_dev->dev, "%s: failed to enable standby mode, rc=%d\n",
1223 __func__, rc);
1224 break;
1225 case BIF_BUS_STATE_ACTIVE:
1226 rc = qpnp_bsi_bus_set_active_mode(bdev, chip->state);
1227 if (rc)
1228 dev_err(&chip->spmi_dev->dev, "%s: failed to enable active mode, rc=%d\n",
1229 __func__, rc);
1230 break;
1231 case BIF_BUS_STATE_INTERRUPT:
1232 /*
1233 * qpnp_bsi_bus_set_interrupt_mode() internally sets
1234 * chip->state = BIF_BUS_STATE_INTERRUPT immediately before
1235 * issuing the EINT command.
1236 */
1237 rc = qpnp_bsi_bus_set_interrupt_mode(bdev);
1238 if (rc) {
1239 dev_err(&chip->spmi_dev->dev, "%s: failed to enable interrupt mode, rc=%d\n",
1240 __func__, rc);
1241 } else if (chip->state == BIF_BUS_STATE_ACTIVE) {
1242 /*
1243 * A slave interrupt was received immediately after
1244 * issuing the EINT command. Therefore, stay in active
1245 * communication mode.
1246 */
1247 state = BIF_BUS_STATE_ACTIVE;
1248 }
1249 break;
1250 default:
1251 rc = -EINVAL;
1252 dev_err(&chip->spmi_dev->dev, "%s: invalid state=%d\n",
1253 __func__, state);
1254 }
1255
1256 if (!rc)
1257 chip->state = state;
1258
1259 return rc;
1260}
1261
1262/* Returns the smallest tau_bif that is greater than or equal to period_ns. */
1263static int qpnp_bsi_tau_bif_higher(int period_ns, int sample_mask)
1264{
1265 const int *supported_period_ns =
1266 (sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
1267 qpnp_bsi_tau_period.period_4x_ns :
1268 qpnp_bsi_tau_period.period_8x_ns);
1269 int smallest_tau_bif = INT_MAX;
1270 int i;
1271
1272 for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
1273 if (period_ns <= supported_period_ns[i]) {
1274 smallest_tau_bif = supported_period_ns[i];
1275 break;
1276 }
1277 }
1278
1279 return smallest_tau_bif;
1280}
1281
1282/* Returns the largest tau_bif that is less than or equal to period_ns. */
1283static int qpnp_bsi_tau_bif_lower(int period_ns, int sample_mask)
1284{
1285 const int *supported_period_ns =
1286 (sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
1287 qpnp_bsi_tau_period.period_4x_ns :
1288 qpnp_bsi_tau_period.period_8x_ns);
1289 int largest_tau_bif = 0;
1290 int i;
1291
1292 for (i = 0; i < QPNP_BSI_NUM_CLOCK_PERIODS; i++) {
1293 if (period_ns >= supported_period_ns[i]) {
1294 largest_tau_bif = supported_period_ns[i];
1295 break;
1296 }
1297 }
1298
1299 return largest_tau_bif;
1300}
1301
1302/*
1303 * Moves period_ns into allowed range and then sets tau bif to the period that
1304 * is greater than or equal to period_ns.
1305 */
1306static int qpnp_bsi_set_tau_bif(struct qpnp_bsi_chip *chip, int period_ns)
1307{
1308 const int *supported_period_ns =
1309 (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
1310 qpnp_bsi_tau_period.period_4x_ns :
1311 qpnp_bsi_tau_period.period_8x_ns);
1312 int idx = 0;
1313 int i, rc;
1314 u8 reg;
1315
1316 if (period_ns < chip->bdesc.bus_clock_min_ns)
1317 period_ns = chip->bdesc.bus_clock_min_ns;
1318 else if (period_ns > chip->bdesc.bus_clock_max_ns)
1319 period_ns = chip->bdesc.bus_clock_max_ns;
1320
1321 for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
1322 if (period_ns <= supported_period_ns[i]) {
1323 idx = i;
1324 break;
1325 }
1326 }
1327
1328 /* Set the tau BIF clock period and sampling rate. */
1329 reg = chip->tau_sampling_mask | idx;
1330 rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TAU_CONFIG, &reg, 1);
1331 if (rc) {
1332 dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
1333 __func__, rc);
1334 return rc;
1335 }
1336
1337 chip->tau_index = idx;
1338
1339 return 0;
1340}
1341
1342static int qpnp_bsi_get_bus_period(struct bif_ctrl_dev *bdev)
1343{
1344 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1345
1346 return qpnp_bsi_get_tau_ns(chip);
1347}
1348
1349static int qpnp_bsi_set_bus_period(struct bif_ctrl_dev *bdev, int period_ns)
1350{
1351 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1352
1353 return qpnp_bsi_set_tau_bif(chip, period_ns);
1354}
1355
1356static int qpnp_bsi_get_battery_rid(struct bif_ctrl_dev *bdev)
1357{
1358 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1359 struct qpnp_vadc_result adc_result;
1360 int rid_ohm, vid_uV, rc;
1361 s64 temp;
1362
1363 if (chip->batt_id_adc_channel >= ADC_MAX_NUM) {
1364 dev_err(&chip->spmi_dev->dev, "%s: no ADC channel specified for Rid measurement\n",
1365 __func__);
1366 return -ENXIO;
1367 }
1368
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001369 rc = qpnp_vadc_read(chip->vadc_dev, chip->batt_id_adc_channel,
1370 &adc_result);
David Collins5fa19932013-01-23 14:10:28 -08001371 if (!rc) {
1372 vid_uV = adc_result.physical;
1373
1374 if (chip->vid_ref_uV - vid_uV <= 0) {
1375 rid_ohm = INT_MAX;
1376 } else {
1377 temp = (s64)chip->r_pullup_ohm * (s64)vid_uV;
1378 do_div(temp, chip->vid_ref_uV - vid_uV);
1379 if (temp > INT_MAX)
1380 rid_ohm = INT_MAX;
1381 else
1382 rid_ohm = temp;
1383 }
1384 } else {
1385 dev_err(&chip->spmi_dev->dev, "%s: qpnp_vadc_read(%d) failed, rc=%d\n",
1386 __func__, chip->batt_id_adc_channel, rc);
1387 rid_ohm = rc;
1388 }
1389
1390 return rid_ohm;
1391}
1392
1393/*
1394 * Returns 1 if a battery pack is present on the BIF bus, 0 if a battery pack
1395 * is not present, or errno if detection fails.
1396 *
1397 * Battery detection is based upon the idle BCL voltage.
1398 */
1399static int qpnp_bsi_get_battery_presence(struct bif_ctrl_dev *bdev)
1400{
1401 struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
1402 u8 reg = 0x00;
1403 int rc;
1404
1405 rc = spmi_ext_register_readl(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
1406 chip->batt_id_stat_addr, &reg, 1);
1407 if (rc) {
1408 dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed, rc=%d\n",
1409 __func__, rc);
1410 return rc;
1411 }
1412
1413 return !!(reg & QPNP_SMBB_BAT_IF_BATT_PRES_MASK);
1414}
1415
1416static struct bif_ctrl_ops qpnp_bsi_ops = {
1417 .bus_transaction = qpnp_bsi_bus_transaction,
1418 .bus_transaction_query = qpnp_bsi_bus_transaction_query,
1419 .bus_transaction_read = qpnp_bsi_bus_transaction_read,
1420 .get_bus_state = qpnp_bsi_get_bus_state,
1421 .set_bus_state = qpnp_bsi_set_bus_state,
1422 .get_bus_period = qpnp_bsi_get_bus_period,
1423 .set_bus_period = qpnp_bsi_set_bus_period,
1424 .read_slave_registers = qpnp_bsi_read_slave_registers,
1425 .write_slave_registers = qpnp_bsi_write_slave_registers,
1426 .get_battery_rid = qpnp_bsi_get_battery_rid,
1427 .get_battery_presence = qpnp_bsi_get_battery_presence,
1428};
1429
1430/* Load all BSI properties from device tree. */
1431static int __devinit qpnp_bsi_parse_dt(struct qpnp_bsi_chip *chip,
1432 struct spmi_device *spmi)
1433{
1434 struct device *dev = &spmi->dev;
1435 struct device_node *node = spmi->dev.of_node;
1436 struct resource *res;
1437 int rc, temp;
1438
1439 chip->batt_id_adc_channel = ADC_MAX_NUM;
1440 rc = of_property_read_u32(node, "qcom,channel-num",
1441 &chip->batt_id_adc_channel);
1442 if (!rc && (chip->batt_id_adc_channel < 0
1443 || chip->batt_id_adc_channel >= ADC_MAX_NUM)) {
1444 dev_err(dev, "%s: invalid qcom,channel-num=%d specified\n",
1445 __func__, chip->batt_id_adc_channel);
1446 return -EINVAL;
1447 }
1448
1449 chip->r_pullup_ohm = QPNP_BSI_DEFAULT_PULLUP_OHM;
1450 rc = of_property_read_u32(node, "qcom,pullup-ohms",
1451 &chip->r_pullup_ohm);
1452 if (!rc && (chip->r_pullup_ohm < QPNP_BSI_MIN_PULLUP_OHM ||
1453 chip->r_pullup_ohm > QPNP_BSI_MAX_PULLUP_OHM)) {
1454 dev_err(dev, "%s: invalid qcom,pullup-ohms=%d property value\n",
1455 __func__, chip->r_pullup_ohm);
1456 return -EINVAL;
1457 }
1458
1459 chip->vid_ref_uV = QPNP_BSI_DEFAULT_VID_REF_UV;
1460 rc = of_property_read_u32(node, "qcom,vref-microvolts",
1461 &chip->vid_ref_uV);
1462 if (!rc && (chip->vid_ref_uV < QPNP_BSI_MIN_VID_REF_UV ||
1463 chip->vid_ref_uV > QPNP_BSI_MAX_VID_REF_UV)) {
1464 dev_err(dev, "%s: invalid qcom,vref-microvolts=%d property value\n",
1465 __func__, chip->vid_ref_uV);
1466 return -EINVAL;
1467 }
1468
1469 res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM, "bsi-base");
1470 if (!res) {
1471 dev_err(dev, "%s: node is missing BSI base address\n",
1472 __func__);
1473 return -EINVAL;
1474 }
1475 chip->base_addr = res->start;
1476
1477 res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM,
1478 "batt-id-status");
1479 if (!res) {
1480 dev_err(dev, "%s: node is missing BATT_ID status address\n",
1481 __func__);
1482 return -EINVAL;
1483 }
1484 chip->batt_id_stat_addr = res->start;
1485
1486 chip->bdesc.name = spmi_get_primary_dev_name(spmi);
1487 if (!chip->bdesc.name) {
1488 dev_err(dev, "%s: label binding undefined for node %s\n",
1489 __func__, spmi->dev.of_node->full_name);
1490 return -EINVAL;
1491 }
1492
1493 /* Use maximum range by default. */
1494 chip->bdesc.bus_clock_min_ns = QPNP_BSI_MIN_CLOCK_SPEED_NS;
1495 chip->bdesc.bus_clock_max_ns = QPNP_BSI_MAX_CLOCK_SPEED_NS;
1496 chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_4X;
1497
1498 rc = of_property_read_u32(node, "qcom,sample-rate", &temp);
1499 if (rc == 0) {
1500 if (temp == 4) {
1501 chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_4X;
1502 } else if (temp == 8) {
1503 chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_8X;
1504 } else {
1505 dev_err(dev, "%s: invalid qcom,sample-rate=%d. Only values of 4 and 8 are supported.\n",
1506 __func__, temp);
1507 return -EINVAL;
1508 }
1509 }
1510
1511 rc = of_property_read_u32(node, "qcom,min-clock-period", &temp);
1512 if (rc == 0)
1513 chip->bdesc.bus_clock_min_ns = qpnp_bsi_tau_bif_higher(temp,
1514 chip->tau_sampling_mask);
1515
1516 rc = of_property_read_u32(node, "qcom,max-clock-period", &temp);
1517 if (rc == 0)
1518 chip->bdesc.bus_clock_max_ns = qpnp_bsi_tau_bif_lower(temp,
1519 chip->tau_sampling_mask);
1520
1521 if (chip->bdesc.bus_clock_min_ns > chip->bdesc.bus_clock_max_ns) {
1522 dev_err(dev, "%s: invalid qcom,min/max-clock-period.\n",
1523 __func__);
1524 return -EINVAL;
1525 }
1526
1527 chip->irq[QPNP_BSI_IRQ_ERR] = spmi_get_irq_byname(spmi, NULL, "err");
1528 if (chip->irq[QPNP_BSI_IRQ_ERR] < 0) {
1529 dev_err(dev, "%s: node is missing err irq\n", __func__);
1530 return chip->irq[QPNP_BSI_IRQ_ERR];
1531 }
1532
1533 chip->irq[QPNP_BSI_IRQ_RX] = spmi_get_irq_byname(spmi, NULL, "rx");
1534 if (chip->irq[QPNP_BSI_IRQ_RX] < 0) {
1535 dev_err(dev, "%s: node is missing rx irq\n", __func__);
1536 return chip->irq[QPNP_BSI_IRQ_RX];
1537 }
1538
1539 chip->irq[QPNP_BSI_IRQ_TX] = spmi_get_irq_byname(spmi, NULL, "tx");
1540 if (chip->irq[QPNP_BSI_IRQ_TX] < 0) {
1541 dev_err(dev, "%s: node is missing tx irq\n", __func__);
1542 return chip->irq[QPNP_BSI_IRQ_TX];
1543 }
1544
1545 chip->batt_present_irq = spmi_get_irq_byname(spmi, NULL,
1546 "batt-present");
1547 if (chip->batt_present_irq < 0) {
1548 dev_err(dev, "%s: node is missing batt-present irq\n",
1549 __func__);
1550 return chip->batt_present_irq;
1551 }
1552
1553 return rc;
1554}
1555
1556/* Request all BSI and battery presence IRQs and set them as wakeable. */
1557static int __devinit qpnp_bsi_init_irqs(struct qpnp_bsi_chip *chip,
1558 struct device *dev)
1559{
1560 int rc;
1561
1562 rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_ERR],
David Collins6067ef52013-11-21 11:23:14 -08001563 qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-err", chip);
David Collins5fa19932013-01-23 14:10:28 -08001564 if (rc < 0) {
1565 dev_err(dev, "%s: request for bsi-err irq %d failed, rc=%d\n",
1566 __func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
1567 return rc;
1568 }
1569
1570 rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 1);
1571 if (rc < 0) {
1572 dev_err(dev, "%s: unable to set bsi-err irq %d as wakeable, rc=%d\n",
1573 __func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
1574 return rc;
1575 }
1576
1577 rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_RX],
David Collins6067ef52013-11-21 11:23:14 -08001578 qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-rx", chip);
David Collins5fa19932013-01-23 14:10:28 -08001579 if (rc < 0) {
1580 dev_err(dev, "%s: request for bsi-rx irq %d failed, rc=%d\n",
1581 __func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
1582 goto set_unwakeable_irq_err;
1583 }
1584
1585 rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 1);
1586 if (rc < 0) {
1587 dev_err(dev, "%s: unable to set bsi-rx irq %d as wakeable, rc=%d\n",
1588 __func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
1589 goto set_unwakeable_irq_err;
1590 }
1591
1592 rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_TX],
David Collins6067ef52013-11-21 11:23:14 -08001593 qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-tx", chip);
David Collins5fa19932013-01-23 14:10:28 -08001594 if (rc < 0) {
1595 dev_err(dev, "%s: request for bsi-tx irq %d failed, rc=%d\n",
1596 __func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
1597 goto set_unwakeable_irq_rx;
1598 }
1599
1600 rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 1);
1601 if (rc < 0) {
1602 dev_err(dev, "%s: unable to set bsi-tx irq %d as wakeable, rc=%d\n",
1603 __func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
1604 goto set_unwakeable_irq_rx;
1605 }
1606
1607 rc = devm_request_threaded_irq(dev, chip->batt_present_irq, NULL,
1608 qpnp_bsi_batt_present_isr,
1609 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED
1610 | IRQF_ONESHOT,
1611 "bsi-batt-present", chip);
1612 if (rc < 0) {
1613 dev_err(dev, "%s: request for bsi-batt-present irq %d failed, rc=%d\n",
1614 __func__, chip->batt_present_irq, rc);
1615 goto set_unwakeable_irq_tx;
1616 }
1617
1618 rc = irq_set_irq_wake(chip->batt_present_irq, 1);
1619 if (rc < 0) {
1620 dev_err(dev, "%s: unable to set bsi-batt-present irq %d as wakeable, rc=%d\n",
1621 __func__, chip->batt_present_irq, rc);
1622 goto set_unwakeable_irq_tx;
1623 }
1624
1625 return rc;
1626
1627set_unwakeable_irq_tx:
1628 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
1629set_unwakeable_irq_rx:
1630 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
1631set_unwakeable_irq_err:
1632 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
1633 return rc;
1634}
1635
1636static void qpnp_bsi_cleanup_irqs(struct qpnp_bsi_chip *chip)
1637{
1638 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
1639 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
1640 irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
1641 irq_set_irq_wake(chip->batt_present_irq, 0);
1642}
1643
1644static int __devinit qpnp_bsi_probe(struct spmi_device *spmi)
1645{
1646 struct device *dev = &spmi->dev;
1647 struct qpnp_bsi_chip *chip;
1648 int rc;
David Collins2b2a76d2013-09-12 13:13:22 -07001649 u8 type[2];
David Collins5fa19932013-01-23 14:10:28 -08001650
1651 if (!spmi->dev.of_node) {
1652 dev_err(dev, "%s: device node missing\n", __func__);
1653 return -ENODEV;
1654 }
1655
1656 chip = devm_kzalloc(dev, sizeof(struct qpnp_bsi_chip), GFP_KERNEL);
1657 if (!chip) {
1658 dev_err(dev, "%s: Can't allocate qpnp_bsi\n", __func__);
1659 return -ENOMEM;
1660 }
1661
1662 rc = qpnp_bsi_parse_dt(chip, spmi);
1663 if (rc) {
1664 dev_err(dev, "%s: device tree parsing failed, rc=%d\n",
1665 __func__, rc);
1666 return rc;
1667 }
1668
1669 INIT_WORK(&chip->slave_irq_work, qpnp_bsi_slave_irq_work);
1670
1671 rc = qpnp_bsi_init_irqs(chip, dev);
1672 if (rc) {
1673 dev_err(dev, "%s: IRQ initialization failed, rc=%d\n",
1674 __func__, rc);
1675 return rc;
1676 }
1677
1678 chip->spmi_dev = spmi;
1679 chip->bdesc.ops = &qpnp_bsi_ops;
David Collins2b2a76d2013-09-12 13:13:22 -07001680 chip->state = BIF_BUS_STATE_MASTER_DISABLED;
David Collins5fa19932013-01-23 14:10:28 -08001681 chip->com_mode = QPNP_BSI_COM_MODE_IRQ;
1682
1683 rc = qpnp_bsi_read(chip, QPNP_BSI_REG_TYPE, type, 2);
1684 if (rc) {
1685 dev_err(dev, "%s: could not read type register, rc=%d\n",
1686 __func__, rc);
1687 goto cleanup_irqs;
1688 }
1689
1690 if (type[0] != QPNP_BSI_TYPE || type[1] != QPNP_BSI_SUBTYPE) {
1691 dev_err(dev, "%s: BSI peripheral is not present; type=0x%02X, subtype=0x%02X\n",
1692 __func__, type[0], type[1]);
1693 rc = -ENODEV;
1694 goto cleanup_irqs;
1695 }
1696
1697 /* Ensure that ADC channel is available if it was specified. */
1698 if (chip->batt_id_adc_channel < ADC_MAX_NUM) {
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001699 chip->vadc_dev = qpnp_get_vadc(dev, "bsi");
1700 if (IS_ERR(chip->vadc_dev)) {
1701 rc = PTR_ERR(chip->vadc_dev);
1702 if (rc != -EPROBE_DEFER)
1703 pr_err("missing vadc property, rc=%d\n", rc);
David Collins5fa19932013-01-23 14:10:28 -08001704 /* Probe retry, do not print an error message */
1705 goto cleanup_irqs;
1706 }
1707 }
1708
1709 rc = qpnp_bsi_set_tau_bif(chip, chip->bdesc.bus_clock_min_ns);
1710 if (rc) {
1711 dev_err(dev, "%s: qpnp_bsi_set_tau_bif() failed, rc=%d\n",
1712 __func__, rc);
1713 goto cleanup_irqs;
1714 }
1715
David Collins5fa19932013-01-23 14:10:28 -08001716 chip->bdev = bif_ctrl_register(&chip->bdesc, dev, chip,
1717 spmi->dev.of_node);
1718 if (IS_ERR(chip->bdev)) {
1719 rc = PTR_ERR(chip->bdev);
1720 dev_err(dev, "%s: bif_ctrl_register failed, rc=%d\n",
1721 __func__, rc);
1722 goto cleanup_irqs;
1723 }
1724
1725 dev_set_drvdata(dev, chip);
1726
1727 return rc;
1728
1729cleanup_irqs:
1730 qpnp_bsi_cleanup_irqs(chip);
1731 return rc;
1732}
1733
1734static int __devexit qpnp_bsi_remove(struct spmi_device *spmi)
1735{
1736 struct qpnp_bsi_chip *chip = dev_get_drvdata(&spmi->dev);
1737 dev_set_drvdata(&spmi->dev, NULL);
1738
1739 if (chip) {
1740 bif_ctrl_unregister(chip->bdev);
1741 qpnp_bsi_cleanup_irqs(chip);
1742 }
1743
1744 return 0;
1745}
1746
1747static struct of_device_id spmi_match_table[] = {
1748 { .compatible = QPNP_BSI_DRIVER_NAME, },
1749 {}
1750};
1751
1752static const struct spmi_device_id qpnp_bsi_id[] = {
1753 { QPNP_BSI_DRIVER_NAME, 0 },
1754 { }
1755};
1756MODULE_DEVICE_TABLE(spmi, qpnp_bsi_id);
1757
1758static struct spmi_driver qpnp_bsi_driver = {
1759 .driver = {
1760 .name = QPNP_BSI_DRIVER_NAME,
1761 .of_match_table = spmi_match_table,
1762 .owner = THIS_MODULE,
1763 },
1764 .probe = qpnp_bsi_probe,
1765 .remove = __devexit_p(qpnp_bsi_remove),
1766 .id_table = qpnp_bsi_id,
1767};
1768
1769static int __init qpnp_bsi_init(void)
1770{
1771 return spmi_driver_register(&qpnp_bsi_driver);
1772}
1773
1774static void __exit qpnp_bsi_exit(void)
1775{
1776 spmi_driver_unregister(&qpnp_bsi_driver);
1777}
1778
1779MODULE_DESCRIPTION("QPNP PMIC BSI driver");
1780MODULE_LICENSE("GPL v2");
1781
1782arch_initcall(qpnp_bsi_init);
1783module_exit(qpnp_bsi_exit);