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