blob: b371d47c70cad11b7040129dab98b530d901bd87 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2009-2011, Code Aurora Forum. 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 * SSBI driver for Qualcomm MSM platforms
14 *
15 */
16#include <linux/kernel.h>
17#include <linux/platform_device.h>
18#include <linux/err.h>
19#include <linux/delay.h>
20#include <linux/io.h>
21#include <linux/i2c.h>
22#include <linux/remote_spinlock.h>
23#include <mach/board.h>
24#include <linux/slab.h>
25
26/* SSBI 2.0 controller registers */
27#define SSBI2_CMD 0x0008
28#define SSBI2_RD 0x0010
29#define SSBI2_STATUS 0x0014
30#define SSBI2_MODE2 0x001C
31
32/* SSBI_CMD fields */
33#define SSBI_CMD_RDWRN (0x01 << 24)
34#define SSBI_CMD_REG_ADDR_SHFT (0x10)
35#define SSBI_CMD_REG_ADDR_MASK (0xFF << SSBI_CMD_REG_ADDR_SHFT)
36#define SSBI_CMD_REG_DATA_SHFT (0x00)
37#define SSBI_CMD_REG_DATA_MASK (0xFF << SSBI_CMD_REG_DATA_SHFT)
38
39/* SSBI_STATUS fields */
40#define SSBI_STATUS_DATA_IN 0x10
41#define SSBI_STATUS_RD_CLOBBERED 0x08
42#define SSBI_STATUS_RD_READY 0x04
43#define SSBI_STATUS_READY 0x02
44#define SSBI_STATUS_MCHN_BUSY 0x01
45
46/* SSBI_RD fields */
47#define SSBI_RD_RDWRN 0x01000000
48#define SSBI_RD_REG_ADDR_SHFT 0x10
49#define SSBI_RD_REG_ADDR_MASK (0xFF << SSBI_RD_REG_ADDR_SHFT)
50#define SSBI_RD_REG_DATA_SHFT (0x00)
51#define SSBI_RD_REG_DATA_MASK (0xFF << SSBI_RD_REG_DATA_SHFT)
52
53/* SSBI_MODE2 fields */
54#define SSBI_MODE2_REG_ADDR_15_8_SHFT 0x04
55#define SSBI_MODE2_REG_ADDR_15_8_MASK (0x7F << SSBI_MODE2_REG_ADDR_15_8_SHFT)
56#define SSBI_MODE2_ADDR_WIDTH_SHFT 0x01
57#define SSBI_MODE2_ADDR_WIDTH_MASK (0x07 << SSBI_MODE2_ADDR_WIDTH_SHFT)
58#define SSBI_MODE2_SSBI2_MODE 0x00000001
59
60#define SSBI_MODE2_REG_ADDR_15_8(MD, AD) \
61 (((MD) & 0x0F) | ((((AD) >> 8) << SSBI_MODE2_REG_ADDR_15_8_SHFT) & \
62 SSBI_MODE2_REG_ADDR_15_8_MASK))
63
64#define SSBI_MODE2_ADDR_WIDTH(N) \
65 ((((N) - 8) << SSBI_MODE2_ADDR_WIDTH_SHFT) & SSBI_MODE2_ADDR_WIDTH_MASK)
66
67#define SSBI_TIMEOUT_US 100
68
69#define SSBI_CMD_READ(AD) \
70 (SSBI_CMD_RDWRN | (((AD) & 0xFF) << SSBI_CMD_REG_ADDR_SHFT))
71
72#define SSBI_CMD_WRITE(AD, DT) \
73 ((((AD) & 0xFF) << SSBI_CMD_REG_ADDR_SHFT) | \
74 (((DT) & 0xFF) << SSBI_CMD_REG_DATA_SHFT))
75
76/* SSBI PMIC Arbiter command registers */
77#define SSBI_PA_CMD 0x0000
78#define SSBI_PA_RD_STATUS 0x0004
79
80/* SSBI_PA_CMD fields */
81#define SSBI_PA_CMD_RDWRN (0x01 << 24)
82#define SSBI_PA_CMD_REG_ADDR_14_8_SHFT (0x10)
83#define SSBI_PA_CMD_REG_ADDR_14_8_MASK (0x7F << SSBI_PA_CMD_REG_ADDR_14_8_SHFT)
84#define SSBI_PA_CMD_REG_ADDR_7_0_SHFT (0x08)
85#define SSBI_PA_CMD_REG_ADDR_7_0_MASK (0xFF << SSBI_PA_CMD_REG_ADDR_7_0_SHFT)
86#define SSBI_PA_CMD_REG_DATA_SHFT (0x00)
87#define SSBI_PA_CMD_REG_DATA_MASK (0xFF << SSBI_PA_CMD_REG_DATA_SHFT)
88
89#define SSBI_PA_CMD_REG_DATA(DT) \
90 (((DT) << SSBI_PA_CMD_REG_DATA_SHFT) & SSBI_PA_CMD_REG_DATA_MASK)
91
92#define SSBI_PA_CMD_REG_ADDR(AD) \
93 (((AD) << SSBI_PA_CMD_REG_ADDR_7_0_SHFT) & \
94 (SSBI_PA_CMD_REG_ADDR_14_8_MASK|SSBI_PA_CMD_REG_ADDR_7_0_MASK))
95
96/* SSBI_PA_RD_STATUS fields */
97#define SSBI_PA_RD_STATUS_TRANS_DONE (0x01 << 27)
98#define SSBI_PA_RD_STATUS_TRANS_DENIED (0x01 << 26)
99#define SSBI_PA_RD_STATUS_REG_DATA_SHFT (0x00)
100#define SSBI_PA_RD_STATUS_REG_DATA_MASK (0xFF << SSBI_PA_CMD_REG_DATA_SHFT)
101#define SSBI_PA_RD_STATUS_TRANS_COMPLETE \
102 (SSBI_PA_RD_STATUS_TRANS_DONE|SSBI_PA_RD_STATUS_TRANS_DENIED)
103
104/* SSBI_FSM Read and Write commands for the FSM9xxx SSBI implementation */
105#define SSBI_FSM_CMD_REG_ADDR_SHFT (0x08)
106
107#define SSBI_FSM_CMD_READ(AD) \
108 (SSBI_CMD_RDWRN | (((AD) & 0xFFFF) << SSBI_FSM_CMD_REG_ADDR_SHFT))
109
110#define SSBI_FSM_CMD_WRITE(AD, DT) \
111 ((((AD) & 0xFFFF) << SSBI_FSM_CMD_REG_ADDR_SHFT) | \
112 (((DT) & 0xFF) << SSBI_CMD_REG_DATA_SHFT))
113
114#define SSBI_MSM_NAME "i2c_ssbi"
115
116MODULE_LICENSE("GPL v2");
117MODULE_VERSION("2.0");
118MODULE_ALIAS("platform:i2c_ssbi");
119
120struct i2c_ssbi_dev {
121 void __iomem *base;
122 struct device *dev;
123 struct i2c_adapter adapter;
124 unsigned long mem_phys_addr;
125 size_t mem_size;
126 bool use_rlock;
127 remote_spinlock_t rspin_lock;
128 enum msm_ssbi_controller_type controller_type;
129 int (*read)(struct i2c_ssbi_dev *, struct i2c_msg *);
130 int (*write)(struct i2c_ssbi_dev *, struct i2c_msg *);
131};
132
133static inline u32 ssbi_readl(struct i2c_ssbi_dev *ssbi, u32 reg)
134{
135 return readl_relaxed(ssbi->base + reg);
136}
137
138static inline void ssbi_writel(struct i2c_ssbi_dev *ssbi, u32 reg, u32 val)
139{
140 writel_relaxed(val, ssbi->base + reg);
141}
142
143static inline int
144i2c_ssbi_poll_for_device_ready(struct i2c_ssbi_dev *ssbi)
145{
146 u32 timeout = SSBI_TIMEOUT_US;
147
148 while (!(ssbi_readl(ssbi, SSBI2_STATUS) & SSBI_STATUS_READY)) {
149 if (--timeout == 0) {
150 dev_err(ssbi->dev, "%s: timeout, status %x\n", __func__,
151 ssbi_readl(ssbi, SSBI2_STATUS));
152 return -ETIMEDOUT;
153 }
154 udelay(1);
155 }
156
157 return 0;
158}
159
160static inline int
161i2c_ssbi_poll_for_read_completed(struct i2c_ssbi_dev *ssbi)
162{
163 u32 timeout = SSBI_TIMEOUT_US;
164
165 while (!(ssbi_readl(ssbi, SSBI2_STATUS) & SSBI_STATUS_RD_READY)) {
166 if (--timeout == 0) {
167 dev_err(ssbi->dev, "%s: timeout, status %x\n", __func__,
168 ssbi_readl(ssbi, SSBI2_STATUS));
169 return -ETIMEDOUT;
170 }
171 udelay(1);
172 }
173
174 return 0;
175}
176
177static inline int
178i2c_ssbi_poll_for_transfer_completed(struct i2c_ssbi_dev *ssbi)
179{
180 u32 timeout = SSBI_TIMEOUT_US;
181
182 while ((ssbi_readl(ssbi, SSBI2_STATUS) & SSBI_STATUS_MCHN_BUSY)) {
183 if (--timeout == 0) {
184 dev_err(ssbi->dev, "%s: timeout, status %x\n", __func__,
185 ssbi_readl(ssbi, SSBI2_STATUS));
186 return -ETIMEDOUT;
187 }
188 udelay(1);
189 }
190
191 return 0;
192}
193
194static int
195i2c_ssbi_read_bytes(struct i2c_ssbi_dev *ssbi, struct i2c_msg *msg)
196{
197 int ret = 0;
198 u8 *buf = msg->buf;
199 u16 len = msg->len;
200 u16 addr = msg->addr;
201 u32 read_cmd;
202
203 if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
204 u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
205 ssbi_writel(ssbi, SSBI2_MODE2,
206 SSBI_MODE2_REG_ADDR_15_8(mode2, addr));
207 }
208
209 if (ssbi->controller_type == FSM_SBI_CTRL_SSBI)
210 read_cmd = SSBI_FSM_CMD_READ(addr);
211 else
212 read_cmd = SSBI_CMD_READ(addr);
213
214 while (len) {
215 ret = i2c_ssbi_poll_for_device_ready(ssbi);
216 if (ret)
217 goto read_failed;
218
219 ssbi_writel(ssbi, SSBI2_CMD, read_cmd);
220
221 ret = i2c_ssbi_poll_for_read_completed(ssbi);
222 if (ret)
223 goto read_failed;
224
225 *buf++ = ssbi_readl(ssbi, SSBI2_RD) & SSBI_RD_REG_DATA_MASK;
226 len--;
227 }
228
229read_failed:
230 return ret;
231}
232
233static int
234i2c_ssbi_write_bytes(struct i2c_ssbi_dev *ssbi, struct i2c_msg *msg)
235{
236 int ret = 0;
237 u8 *buf = msg->buf;
238 u16 len = msg->len;
239 u16 addr = msg->addr;
240
241 if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
242 u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
243 ssbi_writel(ssbi, SSBI2_MODE2,
244 SSBI_MODE2_REG_ADDR_15_8(mode2, addr));
245 }
246
247 while (len) {
248 ret = i2c_ssbi_poll_for_device_ready(ssbi);
249 if (ret)
250 goto write_failed;
251
252 if (ssbi->controller_type == FSM_SBI_CTRL_SSBI)
253 ssbi_writel(ssbi, SSBI2_CMD,
254 SSBI_FSM_CMD_WRITE(addr, *buf++));
255 else
256 ssbi_writel(ssbi, SSBI2_CMD,
257 SSBI_CMD_WRITE(addr, *buf++));
258
259 ret = i2c_ssbi_poll_for_transfer_completed(ssbi);
260 if (ret)
261 goto write_failed;
262
263 len--;
264 }
265
266write_failed:
267 return ret;
268}
269
270static inline int
271i2c_ssbi_pa_transfer(struct i2c_ssbi_dev *ssbi, u32 cmd, u8 *data)
272{
273 u32 rd_status;
274 u32 timeout = SSBI_TIMEOUT_US;
275
276 ssbi_writel(ssbi, SSBI_PA_CMD, cmd);
277 rd_status = ssbi_readl(ssbi, SSBI_PA_RD_STATUS);
278
279 while ((rd_status & (SSBI_PA_RD_STATUS_TRANS_COMPLETE)) == 0) {
280
281 if (--timeout == 0) {
282 dev_err(ssbi->dev, "%s: timeout, status %x\n",
283 __func__, rd_status);
284 return -ETIMEDOUT;
285 }
286 udelay(1);
287 rd_status = ssbi_readl(ssbi, SSBI_PA_RD_STATUS);
288 }
289
290 if (rd_status & SSBI_PA_RD_STATUS_TRANS_DENIED) {
291 dev_err(ssbi->dev, "%s: transaction denied, status %x\n",
292 __func__, rd_status);
293 return -EPERM;
294 }
295
296 if (data)
297 *data = (rd_status & SSBI_PA_RD_STATUS_REG_DATA_MASK) >>
298 SSBI_PA_CMD_REG_DATA_SHFT;
299 return 0;
300}
301
302static int
303i2c_ssbi_pa_read_bytes(struct i2c_ssbi_dev *ssbi, struct i2c_msg *msg)
304{
305 int ret = 0;
306 u8 data;
307 u8 *buf = msg->buf;
308 u16 len = msg->len;
309 u32 read_cmd = (SSBI_PA_CMD_RDWRN | SSBI_PA_CMD_REG_ADDR(msg->addr));
310
311 while (len) {
312
313 ret = i2c_ssbi_pa_transfer(ssbi, read_cmd, &data);
314 if (ret)
315 goto read_failed;
316
317 *buf++ = data;
318 len--;
319 }
320
321read_failed:
322 return ret;
323}
324
325static int
326i2c_ssbi_pa_write_bytes(struct i2c_ssbi_dev *ssbi, struct i2c_msg *msg)
327{
328 int ret = 0;
329 u8 *buf = msg->buf;
330 u16 len = msg->len;
331 u32 addr = SSBI_PA_CMD_REG_ADDR(msg->addr);
332
333 while (len) {
334
335 u32 write_cmd = addr | (*buf++ & SSBI_PA_CMD_REG_DATA_MASK);
336
337 ret = i2c_ssbi_pa_transfer(ssbi, write_cmd, NULL);
338 if (ret)
339 goto write_failed;
340 len--;
341 }
342
343write_failed:
344 return ret;
345}
346
347static int
348i2c_ssbi_transfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
349{
350 int ret = 0;
351 int rem = num;
352 unsigned long flags = 0;
353 struct i2c_ssbi_dev *ssbi = i2c_get_adapdata(adap);
354
355 if (ssbi->use_rlock)
356 remote_spin_lock_irqsave(&ssbi->rspin_lock, flags);
357
358 while (rem) {
359 if (msgs->flags & I2C_M_RD) {
360 ret = ssbi->read(ssbi, msgs);
361 if (ret)
362 goto transfer_failed;
363 } else {
364 ret = ssbi->write(ssbi, msgs);
365 if (ret)
366 goto transfer_failed;
367 }
368
369 msgs++;
370 rem--;
371 }
372
373 if (ssbi->use_rlock)
374 remote_spin_unlock_irqrestore(&ssbi->rspin_lock, flags);
375
376 return num;
377
378transfer_failed:
379 if (ssbi->use_rlock)
380 remote_spin_unlock_irqrestore(&ssbi->rspin_lock, flags);
381 return ret;
382}
383
384static u32 i2c_ssbi_i2c_func(struct i2c_adapter *adap)
385{
386 return I2C_FUNC_I2C;
387}
388
389static const struct i2c_algorithm msm_i2c_algo = {
390 .master_xfer = i2c_ssbi_transfer,
391 .functionality = i2c_ssbi_i2c_func,
392};
393
394static int __init i2c_ssbi_probe(struct platform_device *pdev)
395{
396 int ret = 0;
397 struct resource *ssbi_res;
398 struct i2c_ssbi_dev *ssbi;
399 const struct msm_i2c_ssbi_platform_data *pdata;
400
401 pdata = pdev->dev.platform_data;
402 if (!pdata) {
403 ret = -ENXIO;
404 dev_err(&pdev->dev, "platform data not initialized\n");
405 goto err_probe_exit;
406 }
407
408 ssbi = kzalloc(sizeof(struct i2c_ssbi_dev), GFP_KERNEL);
409 if (!ssbi) {
410 ret = -ENOMEM;
411 dev_err(&pdev->dev, "allocation failed\n");
412 goto err_probe_exit;
413 }
414
415 ssbi_res = platform_get_resource_byname(pdev,
416 IORESOURCE_MEM, "ssbi_base");
417 if (!ssbi_res) {
418 ret = -ENXIO;
419 dev_err(&pdev->dev, "get_resource_byname failed\n");
420 goto err_probe_res;
421 }
422
423 ssbi->mem_phys_addr = ssbi_res->start;
424 ssbi->mem_size = resource_size(ssbi_res);
425 if (!request_mem_region(ssbi->mem_phys_addr, ssbi->mem_size,
426 SSBI_MSM_NAME)) {
427 ret = -ENXIO;
428 dev_err(&pdev->dev, "request_mem_region failed\n");
429 goto err_probe_reqmem;
430 }
431
432 ssbi->base = ioremap(ssbi->mem_phys_addr, ssbi->mem_size);
433 if (!ssbi->base) {
434 dev_err(&pdev->dev, "ioremap failed\n");
435 goto err_probe_ioremap;
436 }
437
438 ssbi->dev = &pdev->dev;
439 platform_set_drvdata(pdev, ssbi);
440
441 ssbi->controller_type = pdata->controller_type;
442 if (ssbi->controller_type == MSM_SBI_CTRL_PMIC_ARBITER) {
443 ssbi->read = i2c_ssbi_pa_read_bytes;
444 ssbi->write = i2c_ssbi_pa_write_bytes;
445 } else {
446 ssbi->read = i2c_ssbi_read_bytes;
447 ssbi->write = i2c_ssbi_write_bytes;
448 }
449
450 i2c_set_adapdata(&ssbi->adapter, ssbi);
451 ssbi->adapter.algo = &msm_i2c_algo;
452 strlcpy(ssbi->adapter.name,
453 "MSM SSBI adapter",
454 sizeof(ssbi->adapter.name));
455
456 if (pdata->rsl_id) {
457 ret = remote_spin_lock_init(&ssbi->rspin_lock, pdata->rsl_id);
458 if (ret) {
459 dev_err(&pdev->dev, "remote spinlock init failed\n");
460 goto err_remote_spinlock_init_failed;
461 }
462 ssbi->use_rlock = 1;
463 }
464
465 ssbi->adapter.nr = pdev->id;
466 ret = i2c_add_numbered_adapter(&ssbi->adapter);
467 if (ret) {
468 dev_err(&pdev->dev, "i2c_add_numbered_adapter failed\n");
469 goto err_add_adapter_failed;
470 }
471 return 0;
472
473err_add_adapter_failed:
474err_remote_spinlock_init_failed:
475 iounmap(ssbi->base);
476 platform_set_drvdata(pdev, NULL);
477err_probe_ioremap:
478 release_mem_region(ssbi->mem_phys_addr, ssbi->mem_size);
479err_probe_reqmem:
480err_probe_res:
481 kfree(ssbi);
482err_probe_exit:
483 return ret;
484}
485
486static int __devexit i2c_ssbi_remove(struct platform_device *pdev)
487{
488 struct i2c_ssbi_dev *ssbi = platform_get_drvdata(pdev);
489
490 platform_set_drvdata(pdev, NULL);
491 i2c_del_adapter(&ssbi->adapter);
492 iounmap(ssbi->base);
493 release_mem_region(ssbi->mem_phys_addr, ssbi->mem_size);
494 kfree(ssbi);
495 return 0;
496}
497
498static struct platform_driver i2c_ssbi_driver = {
499 .driver = {
500 .name = "i2c_ssbi",
501 .owner = THIS_MODULE,
502 },
503 .remove = __exit_p(i2c_ssbi_remove),
504};
505
506static int __init i2c_ssbi_init(void)
507{
508 return platform_driver_probe(&i2c_ssbi_driver, i2c_ssbi_probe);
509}
510arch_initcall(i2c_ssbi_init);
511
512static void __exit i2c_ssbi_exit(void)
513{
514 platform_driver_unregister(&i2c_ssbi_driver);
515}
516module_exit(i2c_ssbi_exit);