3 * Copyright (C) 2014 IBM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
19 #include <linux/device.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
28 #include <asm/firmware.h>
31 static int i2c_opal_translate_error(int rc
)
38 case OPAL_I2C_ARBT_LOST
:
40 case OPAL_I2C_TIMEOUT
:
42 case OPAL_I2C_NACK_RCVD
:
44 case OPAL_I2C_STOP_ERR
:
51 static int i2c_opal_send_request(u32 bus_id
, struct opal_i2c_request
*req
)
56 token
= opal_async_get_token_interruptible();
58 if (token
!= -ERESTARTSYS
)
59 pr_err("Failed to get the async token\n");
64 rc
= opal_i2c_request(token
, bus_id
, req
);
65 if (rc
!= OPAL_ASYNC_COMPLETION
) {
66 rc
= i2c_opal_translate_error(rc
);
70 rc
= opal_async_wait_response(token
, &msg
);
74 rc
= be64_to_cpu(msg
.params
[1]);
75 if (rc
!= OPAL_SUCCESS
) {
76 rc
= i2c_opal_translate_error(rc
);
81 opal_async_release_token(token
);
85 static int i2c_opal_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
88 unsigned long opal_id
= (unsigned long)adap
->algo_data
;
89 struct opal_i2c_request req
;
92 /* We only support fairly simple combinations here of one
95 memset(&req
, 0, sizeof(req
));
100 req
.type
= (msgs
[0].flags
& I2C_M_RD
) ?
101 OPAL_I2C_RAW_READ
: OPAL_I2C_RAW_WRITE
;
102 req
.addr
= cpu_to_be16(msgs
[0].addr
);
103 req
.size
= cpu_to_be32(msgs
[0].len
);
104 req
.buffer_ra
= cpu_to_be64(__pa(msgs
[0].buf
));
107 req
.type
= (msgs
[1].flags
& I2C_M_RD
) ?
108 OPAL_I2C_SM_READ
: OPAL_I2C_SM_WRITE
;
109 req
.addr
= cpu_to_be16(msgs
[0].addr
);
110 req
.subaddr_sz
= msgs
[0].len
;
111 for (i
= 0; i
< msgs
[0].len
; i
++)
112 req
.subaddr
= (req
.subaddr
<< 8) | msgs
[0].buf
[i
];
113 req
.subaddr
= cpu_to_be32(req
.subaddr
);
114 req
.size
= cpu_to_be32(msgs
[1].len
);
115 req
.buffer_ra
= cpu_to_be64(__pa(msgs
[1].buf
));
121 rc
= i2c_opal_send_request(opal_id
, &req
);
128 static int i2c_opal_smbus_xfer(struct i2c_adapter
*adap
, u16 addr
,
129 unsigned short flags
, char read_write
,
130 u8 command
, int size
, union i2c_smbus_data
*data
)
132 unsigned long opal_id
= (unsigned long)adap
->algo_data
;
133 struct opal_i2c_request req
;
137 memset(&req
, 0, sizeof(req
));
139 req
.addr
= cpu_to_be16(addr
);
142 req
.buffer_ra
= cpu_to_be64(__pa(&data
->byte
));
143 req
.size
= cpu_to_be32(1);
145 case I2C_SMBUS_QUICK
:
146 req
.type
= (read_write
== I2C_SMBUS_READ
) ?
147 OPAL_I2C_RAW_READ
: OPAL_I2C_RAW_WRITE
;
149 case I2C_SMBUS_BYTE_DATA
:
150 req
.buffer_ra
= cpu_to_be64(__pa(&data
->byte
));
151 req
.size
= cpu_to_be32(1);
152 req
.subaddr
= cpu_to_be32(command
);
154 req
.type
= (read_write
== I2C_SMBUS_READ
) ?
155 OPAL_I2C_SM_READ
: OPAL_I2C_SM_WRITE
;
157 case I2C_SMBUS_WORD_DATA
:
159 local
[0] = data
->word
& 0xff;
160 local
[1] = (data
->word
>> 8) & 0xff;
162 req
.buffer_ra
= cpu_to_be64(__pa(local
));
163 req
.size
= cpu_to_be32(2);
164 req
.subaddr
= cpu_to_be32(command
);
166 req
.type
= (read_write
== I2C_SMBUS_READ
) ?
167 OPAL_I2C_SM_READ
: OPAL_I2C_SM_WRITE
;
169 case I2C_SMBUS_I2C_BLOCK_DATA
:
170 req
.buffer_ra
= cpu_to_be64(__pa(&data
->block
[1]));
171 req
.size
= cpu_to_be32(data
->block
[0]);
172 req
.subaddr
= cpu_to_be32(command
);
174 req
.type
= (read_write
== I2C_SMBUS_READ
) ?
175 OPAL_I2C_SM_READ
: OPAL_I2C_SM_WRITE
;
181 rc
= i2c_opal_send_request(opal_id
, &req
);
182 if (!rc
&& read_write
&& size
== I2C_SMBUS_WORD_DATA
) {
183 data
->word
= ((u16
)local
[1]) << 8;
184 data
->word
|= local
[0];
190 static u32
i2c_opal_func(struct i2c_adapter
*adapter
)
192 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
193 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
194 I2C_FUNC_SMBUS_I2C_BLOCK
;
197 static const struct i2c_algorithm i2c_opal_algo
= {
198 .master_xfer
= i2c_opal_master_xfer
,
199 .smbus_xfer
= i2c_opal_smbus_xfer
,
200 .functionality
= i2c_opal_func
,
204 * For two messages, we basically support simple smbus transactions of a
205 * write-then-anything.
207 static struct i2c_adapter_quirks i2c_opal_quirks
= {
208 .flags
= I2C_AQ_COMB
| I2C_AQ_COMB_WRITE_FIRST
| I2C_AQ_COMB_SAME_ADDR
,
209 .max_comb_1st_msg_len
= 4,
212 static int i2c_opal_probe(struct platform_device
*pdev
)
214 struct i2c_adapter
*adapter
;
219 if (!pdev
->dev
.of_node
)
222 rc
= of_property_read_u32(pdev
->dev
.of_node
, "ibm,opal-id", &opal_id
);
224 dev_err(&pdev
->dev
, "Missing ibm,opal-id property !\n");
228 adapter
= devm_kzalloc(&pdev
->dev
, sizeof(*adapter
), GFP_KERNEL
);
232 adapter
->algo
= &i2c_opal_algo
;
233 adapter
->algo_data
= (void *)(unsigned long)opal_id
;
234 adapter
->quirks
= &i2c_opal_quirks
;
235 adapter
->dev
.parent
= &pdev
->dev
;
236 adapter
->dev
.of_node
= of_node_get(pdev
->dev
.of_node
);
237 pname
= of_get_property(pdev
->dev
.of_node
, "ibm,port-name", NULL
);
239 strlcpy(adapter
->name
, pname
, sizeof(adapter
->name
));
241 strlcpy(adapter
->name
, "opal", sizeof(adapter
->name
));
243 platform_set_drvdata(pdev
, adapter
);
244 rc
= i2c_add_adapter(adapter
);
246 dev_err(&pdev
->dev
, "Failed to register the i2c adapter\n");
251 static int i2c_opal_remove(struct platform_device
*pdev
)
253 struct i2c_adapter
*adapter
= platform_get_drvdata(pdev
);
255 i2c_del_adapter(adapter
);
260 static const struct of_device_id i2c_opal_of_match
[] = {
262 .compatible
= "ibm,opal-i2c",
266 MODULE_DEVICE_TABLE(of
, i2c_opal_of_match
);
268 static struct platform_driver i2c_opal_driver
= {
269 .probe
= i2c_opal_probe
,
270 .remove
= i2c_opal_remove
,
273 .of_match_table
= i2c_opal_of_match
,
277 static int __init
i2c_opal_init(void)
279 if (!firmware_has_feature(FW_FEATURE_OPAL
))
282 return platform_driver_register(&i2c_opal_driver
);
284 module_init(i2c_opal_init
);
286 static void __exit
i2c_opal_exit(void)
288 return platform_driver_unregister(&i2c_opal_driver
);
290 module_exit(i2c_opal_exit
);
292 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
293 MODULE_DESCRIPTION("IBM OPAL I2C driver");
294 MODULE_LICENSE("GPL");