drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / i2c / busses / i2c-pasemi-core.c
blobdac694a9d781f86afe634ddc4d93dee51bd2edff
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2006-2007 PA Semi, Inc
5 * SMBus host driver for PA Semi PWRficient
6 */
8 #include <linux/module.h>
9 #include <linux/pci.h>
10 #include <linux/kernel.h>
11 #include <linux/stddef.h>
12 #include <linux/sched.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/io.h>
18 #include "i2c-pasemi-core.h"
20 /* Register offsets */
21 #define REG_MTXFIFO 0x00
22 #define REG_MRXFIFO 0x04
23 #define REG_SMSTA 0x14
24 #define REG_IMASK 0x18
25 #define REG_CTL 0x1c
26 #define REG_REV 0x28
28 /* Register defs */
29 #define MTXFIFO_READ 0x00000400
30 #define MTXFIFO_STOP 0x00000200
31 #define MTXFIFO_START 0x00000100
32 #define MTXFIFO_DATA_M 0x000000ff
34 #define MRXFIFO_EMPTY 0x00000100
35 #define MRXFIFO_DATA_M 0x000000ff
37 #define SMSTA_XEN 0x08000000
38 #define SMSTA_MTN 0x00200000
40 #define CTL_MRR 0x00000400
41 #define CTL_MTR 0x00000200
42 #define CTL_EN 0x00000800
43 #define CTL_CLK_M 0x000000ff
45 static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
47 dev_dbg(smbus->dev, "smbus write reg %x val %08x\n", reg, val);
48 iowrite32(val, smbus->ioaddr + reg);
51 static inline int reg_read(struct pasemi_smbus *smbus, int reg)
53 int ret;
54 ret = ioread32(smbus->ioaddr + reg);
55 dev_dbg(smbus->dev, "smbus read reg %x val %08x\n", reg, ret);
56 return ret;
59 #define TXFIFO_WR(smbus, reg) reg_write((smbus), REG_MTXFIFO, (reg))
60 #define RXFIFO_RD(smbus) reg_read((smbus), REG_MRXFIFO)
62 static void pasemi_reset(struct pasemi_smbus *smbus)
64 u32 val = (CTL_MTR | CTL_MRR | (smbus->clk_div & CTL_CLK_M));
66 if (smbus->hw_rev >= 6)
67 val |= CTL_EN;
69 reg_write(smbus, REG_CTL, val);
70 reinit_completion(&smbus->irq_completion);
73 static void pasemi_smb_clear(struct pasemi_smbus *smbus)
75 unsigned int status;
77 status = reg_read(smbus, REG_SMSTA);
78 reg_write(smbus, REG_SMSTA, status);
81 static int pasemi_smb_waitready(struct pasemi_smbus *smbus)
83 int timeout = 100;
84 unsigned int status;
86 if (smbus->use_irq) {
87 reinit_completion(&smbus->irq_completion);
88 reg_write(smbus, REG_IMASK, SMSTA_XEN | SMSTA_MTN);
89 wait_for_completion_timeout(&smbus->irq_completion, msecs_to_jiffies(100));
90 reg_write(smbus, REG_IMASK, 0);
91 status = reg_read(smbus, REG_SMSTA);
92 } else {
93 status = reg_read(smbus, REG_SMSTA);
94 while (!(status & SMSTA_XEN) && timeout--) {
95 msleep(1);
96 status = reg_read(smbus, REG_SMSTA);
100 /* Got NACK? */
101 if (status & SMSTA_MTN)
102 return -ENXIO;
104 if (timeout < 0) {
105 dev_warn(smbus->dev, "Timeout, status 0x%08x\n", status);
106 reg_write(smbus, REG_SMSTA, status);
107 return -ETIME;
110 /* Clear XEN */
111 reg_write(smbus, REG_SMSTA, SMSTA_XEN);
113 return 0;
116 static int pasemi_i2c_xfer_msg(struct i2c_adapter *adapter,
117 struct i2c_msg *msg, int stop)
119 struct pasemi_smbus *smbus = adapter->algo_data;
120 int read, i, err;
121 u32 rd;
123 read = msg->flags & I2C_M_RD ? 1 : 0;
125 TXFIFO_WR(smbus, MTXFIFO_START | i2c_8bit_addr_from_msg(msg));
127 if (read) {
128 TXFIFO_WR(smbus, msg->len | MTXFIFO_READ |
129 (stop ? MTXFIFO_STOP : 0));
131 err = pasemi_smb_waitready(smbus);
132 if (err)
133 goto reset_out;
135 for (i = 0; i < msg->len; i++) {
136 rd = RXFIFO_RD(smbus);
137 if (rd & MRXFIFO_EMPTY) {
138 err = -ENODATA;
139 goto reset_out;
141 msg->buf[i] = rd & MRXFIFO_DATA_M;
143 } else {
144 for (i = 0; i < msg->len - 1; i++)
145 TXFIFO_WR(smbus, msg->buf[i]);
147 TXFIFO_WR(smbus, msg->buf[msg->len-1] |
148 (stop ? MTXFIFO_STOP : 0));
150 if (stop) {
151 err = pasemi_smb_waitready(smbus);
152 if (err)
153 goto reset_out;
157 return 0;
159 reset_out:
160 pasemi_reset(smbus);
161 return err;
164 static int pasemi_i2c_xfer(struct i2c_adapter *adapter,
165 struct i2c_msg *msgs, int num)
167 struct pasemi_smbus *smbus = adapter->algo_data;
168 int ret, i;
170 pasemi_smb_clear(smbus);
172 ret = 0;
174 for (i = 0; i < num && !ret; i++)
175 ret = pasemi_i2c_xfer_msg(adapter, &msgs[i], (i == (num - 1)));
177 return ret ? ret : num;
180 static int pasemi_smb_xfer(struct i2c_adapter *adapter,
181 u16 addr, unsigned short flags, char read_write, u8 command,
182 int size, union i2c_smbus_data *data)
184 struct pasemi_smbus *smbus = adapter->algo_data;
185 unsigned int rd;
186 int read_flag, err;
187 int len = 0, i;
189 /* All our ops take 8-bit shifted addresses */
190 addr <<= 1;
191 read_flag = read_write == I2C_SMBUS_READ;
193 pasemi_smb_clear(smbus);
195 switch (size) {
196 case I2C_SMBUS_QUICK:
197 TXFIFO_WR(smbus, addr | read_flag | MTXFIFO_START |
198 MTXFIFO_STOP);
199 break;
200 case I2C_SMBUS_BYTE:
201 TXFIFO_WR(smbus, addr | read_flag | MTXFIFO_START);
202 if (read_write)
203 TXFIFO_WR(smbus, 1 | MTXFIFO_STOP | MTXFIFO_READ);
204 else
205 TXFIFO_WR(smbus, MTXFIFO_STOP | command);
206 break;
207 case I2C_SMBUS_BYTE_DATA:
208 TXFIFO_WR(smbus, addr | MTXFIFO_START);
209 TXFIFO_WR(smbus, command);
210 if (read_write) {
211 TXFIFO_WR(smbus, addr | I2C_SMBUS_READ | MTXFIFO_START);
212 TXFIFO_WR(smbus, 1 | MTXFIFO_READ | MTXFIFO_STOP);
213 } else {
214 TXFIFO_WR(smbus, MTXFIFO_STOP | data->byte);
216 break;
217 case I2C_SMBUS_WORD_DATA:
218 TXFIFO_WR(smbus, addr | MTXFIFO_START);
219 TXFIFO_WR(smbus, command);
220 if (read_write) {
221 TXFIFO_WR(smbus, addr | I2C_SMBUS_READ | MTXFIFO_START);
222 TXFIFO_WR(smbus, 2 | MTXFIFO_READ | MTXFIFO_STOP);
223 } else {
224 TXFIFO_WR(smbus, data->word & MTXFIFO_DATA_M);
225 TXFIFO_WR(smbus, MTXFIFO_STOP | (data->word >> 8));
227 break;
228 case I2C_SMBUS_BLOCK_DATA:
229 TXFIFO_WR(smbus, addr | MTXFIFO_START);
230 TXFIFO_WR(smbus, command);
231 if (read_write) {
232 TXFIFO_WR(smbus, addr | I2C_SMBUS_READ | MTXFIFO_START);
233 TXFIFO_WR(smbus, 1 | MTXFIFO_READ);
234 rd = RXFIFO_RD(smbus);
235 len = min_t(u8, (rd & MRXFIFO_DATA_M),
236 I2C_SMBUS_BLOCK_MAX);
237 TXFIFO_WR(smbus, len | MTXFIFO_READ |
238 MTXFIFO_STOP);
239 } else {
240 len = min_t(u8, data->block[0], I2C_SMBUS_BLOCK_MAX);
241 TXFIFO_WR(smbus, len);
242 for (i = 1; i < len; i++)
243 TXFIFO_WR(smbus, data->block[i]);
244 TXFIFO_WR(smbus, data->block[len] | MTXFIFO_STOP);
246 break;
247 case I2C_SMBUS_PROC_CALL:
248 read_write = I2C_SMBUS_READ;
249 TXFIFO_WR(smbus, addr | MTXFIFO_START);
250 TXFIFO_WR(smbus, command);
251 TXFIFO_WR(smbus, data->word & MTXFIFO_DATA_M);
252 TXFIFO_WR(smbus, (data->word >> 8) & MTXFIFO_DATA_M);
253 TXFIFO_WR(smbus, addr | I2C_SMBUS_READ | MTXFIFO_START);
254 TXFIFO_WR(smbus, 2 | MTXFIFO_STOP | MTXFIFO_READ);
255 break;
256 case I2C_SMBUS_BLOCK_PROC_CALL:
257 len = min_t(u8, data->block[0], I2C_SMBUS_BLOCK_MAX - 1);
258 read_write = I2C_SMBUS_READ;
259 TXFIFO_WR(smbus, addr | MTXFIFO_START);
260 TXFIFO_WR(smbus, command);
261 TXFIFO_WR(smbus, len);
262 for (i = 1; i <= len; i++)
263 TXFIFO_WR(smbus, data->block[i]);
264 TXFIFO_WR(smbus, addr | I2C_SMBUS_READ);
265 TXFIFO_WR(smbus, MTXFIFO_READ | 1);
266 rd = RXFIFO_RD(smbus);
267 len = min_t(u8, (rd & MRXFIFO_DATA_M),
268 I2C_SMBUS_BLOCK_MAX - len);
269 TXFIFO_WR(smbus, len | MTXFIFO_READ | MTXFIFO_STOP);
270 break;
272 default:
273 dev_warn(&adapter->dev, "Unsupported transaction %d\n", size);
274 return -EINVAL;
277 err = pasemi_smb_waitready(smbus);
278 if (err)
279 goto reset_out;
281 if (read_write == I2C_SMBUS_WRITE)
282 return 0;
284 switch (size) {
285 case I2C_SMBUS_BYTE:
286 case I2C_SMBUS_BYTE_DATA:
287 rd = RXFIFO_RD(smbus);
288 if (rd & MRXFIFO_EMPTY) {
289 err = -ENODATA;
290 goto reset_out;
292 data->byte = rd & MRXFIFO_DATA_M;
293 break;
294 case I2C_SMBUS_WORD_DATA:
295 case I2C_SMBUS_PROC_CALL:
296 rd = RXFIFO_RD(smbus);
297 if (rd & MRXFIFO_EMPTY) {
298 err = -ENODATA;
299 goto reset_out;
301 data->word = rd & MRXFIFO_DATA_M;
302 rd = RXFIFO_RD(smbus);
303 if (rd & MRXFIFO_EMPTY) {
304 err = -ENODATA;
305 goto reset_out;
307 data->word |= (rd & MRXFIFO_DATA_M) << 8;
308 break;
309 case I2C_SMBUS_BLOCK_DATA:
310 case I2C_SMBUS_BLOCK_PROC_CALL:
311 data->block[0] = len;
312 for (i = 1; i <= len; i ++) {
313 rd = RXFIFO_RD(smbus);
314 if (rd & MRXFIFO_EMPTY) {
315 err = -ENODATA;
316 goto reset_out;
318 data->block[i] = rd & MRXFIFO_DATA_M;
320 break;
323 return 0;
325 reset_out:
326 pasemi_reset(smbus);
327 return err;
330 static u32 pasemi_smb_func(struct i2c_adapter *adapter)
332 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
333 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
334 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
335 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | I2C_FUNC_I2C;
338 static const struct i2c_algorithm smbus_algorithm = {
339 .xfer = pasemi_i2c_xfer,
340 .smbus_xfer = pasemi_smb_xfer,
341 .functionality = pasemi_smb_func,
344 int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
346 int error;
348 smbus->adapter.owner = THIS_MODULE;
349 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
350 "PA Semi SMBus adapter (%s)", dev_name(smbus->dev));
351 smbus->adapter.algo = &smbus_algorithm;
352 smbus->adapter.algo_data = smbus;
354 /* set up the sysfs linkage to our parent device */
355 smbus->adapter.dev.parent = smbus->dev;
356 smbus->use_irq = 0;
357 init_completion(&smbus->irq_completion);
359 if (smbus->hw_rev != PASEMI_HW_REV_PCI)
360 smbus->hw_rev = reg_read(smbus, REG_REV);
362 reg_write(smbus, REG_IMASK, 0);
364 pasemi_reset(smbus);
366 error = devm_i2c_add_adapter(smbus->dev, &smbus->adapter);
367 if (error)
368 return error;
370 return 0;
372 EXPORT_SYMBOL_GPL(pasemi_i2c_common_probe);
374 irqreturn_t pasemi_irq_handler(int irq, void *dev_id)
376 struct pasemi_smbus *smbus = dev_id;
378 reg_write(smbus, REG_IMASK, 0);
379 complete(&smbus->irq_completion);
380 return IRQ_HANDLED;
382 EXPORT_SYMBOL_GPL(pasemi_irq_handler);
384 MODULE_LICENSE("GPL");
385 MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
386 MODULE_DESCRIPTION("PA Semi PWRficient SMBus driver");