xref: /linux/drivers/i2c/busses/i2c-opal.c (revision c0b2826aa02a0747c3016457b9c1917ba14d0655)
1 /*
2  * IBM OPAL I2C driver
3  * Copyright (C) 2014 IBM
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.
17  */
18 
19 #include <linux/device.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 
28 #include <asm/firmware.h>
29 #include <asm/opal.h>
30 
31 static int i2c_opal_translate_error(int rc)
32 {
33 	switch (rc) {
34 	case OPAL_NO_MEM:
35 		return -ENOMEM;
36 	case OPAL_PARAMETER:
37 		return -EINVAL;
38 	case OPAL_I2C_ARBT_LOST:
39 		return -EAGAIN;
40 	case OPAL_I2C_TIMEOUT:
41 		return -ETIMEDOUT;
42 	case OPAL_I2C_NACK_RCVD:
43 		return -ENXIO;
44 	case OPAL_I2C_STOP_ERR:
45 		return -EBUSY;
46 	default:
47 		return -EIO;
48 	}
49 }
50 
51 static int i2c_opal_send_request(u32 bus_id, struct opal_i2c_request *req)
52 {
53 	struct opal_msg msg;
54 	int token, rc;
55 
56 	token = opal_async_get_token_interruptible();
57 	if (token < 0) {
58 		if (token != -ERESTARTSYS)
59 			pr_err("Failed to get the async token\n");
60 
61 		return token;
62 	}
63 
64 	rc = opal_i2c_request(token, bus_id, req);
65 	if (rc != OPAL_ASYNC_COMPLETION) {
66 		rc = i2c_opal_translate_error(rc);
67 		goto exit;
68 	}
69 
70 	rc = opal_async_wait_response(token, &msg);
71 	if (rc)
72 		goto exit;
73 
74 	rc = opal_get_async_rc(msg);
75 	if (rc != OPAL_SUCCESS) {
76 		rc = i2c_opal_translate_error(rc);
77 		goto exit;
78 	}
79 
80 exit:
81 	opal_async_release_token(token);
82 	return rc;
83 }
84 
85 static int i2c_opal_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
86 				int num)
87 {
88 	unsigned long opal_id = (unsigned long)adap->algo_data;
89 	struct opal_i2c_request req;
90 	int rc, i;
91 
92 	/* We only support fairly simple combinations here of one
93 	 * or two messages
94 	 */
95 	memset(&req, 0, sizeof(req));
96 	switch(num) {
97 	case 1:
98 		req.type = (msgs[0].flags & I2C_M_RD) ?
99 			OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
100 		req.addr = cpu_to_be16(msgs[0].addr);
101 		req.size = cpu_to_be32(msgs[0].len);
102 		req.buffer_ra = cpu_to_be64(__pa(msgs[0].buf));
103 		break;
104 	case 2:
105 		req.type = (msgs[1].flags & I2C_M_RD) ?
106 			OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
107 		req.addr = cpu_to_be16(msgs[0].addr);
108 		req.subaddr_sz = msgs[0].len;
109 		for (i = 0; i < msgs[0].len; i++)
110 			req.subaddr = (req.subaddr << 8) | msgs[0].buf[i];
111 		req.subaddr = cpu_to_be32(req.subaddr);
112 		req.size = cpu_to_be32(msgs[1].len);
113 		req.buffer_ra = cpu_to_be64(__pa(msgs[1].buf));
114 		break;
115 	}
116 
117 	rc = i2c_opal_send_request(opal_id, &req);
118 	if (rc)
119 		return rc;
120 
121 	return num;
122 }
123 
124 static int i2c_opal_smbus_xfer(struct i2c_adapter *adap, u16 addr,
125 			       unsigned short flags, char read_write,
126 			       u8 command, int size, union i2c_smbus_data *data)
127 {
128 	unsigned long opal_id = (unsigned long)adap->algo_data;
129 	struct opal_i2c_request req;
130 	u8 local[2];
131 	int rc;
132 
133 	memset(&req, 0, sizeof(req));
134 
135 	req.addr = cpu_to_be16(addr);
136 	switch (size) {
137 	case I2C_SMBUS_BYTE:
138 		req.buffer_ra = cpu_to_be64(__pa(&data->byte));
139 		req.size = cpu_to_be32(1);
140 		/* Fall through */
141 	case I2C_SMBUS_QUICK:
142 		req.type = (read_write == I2C_SMBUS_READ) ?
143 			OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
144 		break;
145 	case I2C_SMBUS_BYTE_DATA:
146 		req.buffer_ra = cpu_to_be64(__pa(&data->byte));
147 		req.size = cpu_to_be32(1);
148 		req.subaddr = cpu_to_be32(command);
149 		req.subaddr_sz = 1;
150 		req.type = (read_write == I2C_SMBUS_READ) ?
151 			OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
152 		break;
153 	case I2C_SMBUS_WORD_DATA:
154 		if (!read_write) {
155 			local[0] = data->word & 0xff;
156 			local[1] = (data->word >> 8) & 0xff;
157 		}
158 		req.buffer_ra = cpu_to_be64(__pa(local));
159 		req.size = cpu_to_be32(2);
160 		req.subaddr = cpu_to_be32(command);
161 		req.subaddr_sz = 1;
162 		req.type = (read_write == I2C_SMBUS_READ) ?
163 			OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
164 		break;
165 	case I2C_SMBUS_I2C_BLOCK_DATA:
166 		req.buffer_ra = cpu_to_be64(__pa(&data->block[1]));
167 		req.size = cpu_to_be32(data->block[0]);
168 		req.subaddr = cpu_to_be32(command);
169 		req.subaddr_sz = 1;
170 		req.type = (read_write == I2C_SMBUS_READ) ?
171 			OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
172 		break;
173 	default:
174 		return -EINVAL;
175 	}
176 
177 	rc = i2c_opal_send_request(opal_id, &req);
178 	if (!rc && read_write && size == I2C_SMBUS_WORD_DATA) {
179 		data->word = ((u16)local[1]) << 8;
180 		data->word |= local[0];
181 	}
182 
183 	return rc;
184 }
185 
186 static u32 i2c_opal_func(struct i2c_adapter *adapter)
187 {
188 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
189 	       I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
190 	       I2C_FUNC_SMBUS_I2C_BLOCK;
191 }
192 
193 static const struct i2c_algorithm i2c_opal_algo = {
194 	.master_xfer	= i2c_opal_master_xfer,
195 	.smbus_xfer	= i2c_opal_smbus_xfer,
196 	.functionality	= i2c_opal_func,
197 };
198 
199 /*
200  * For two messages, we basically support simple smbus transactions of a
201  * write-then-anything.
202  */
203 static const struct i2c_adapter_quirks i2c_opal_quirks = {
204 	.flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
205 	.max_comb_1st_msg_len = 4,
206 };
207 
208 static int i2c_opal_probe(struct platform_device *pdev)
209 {
210 	struct i2c_adapter	*adapter;
211 	const char		*pname;
212 	u32			opal_id;
213 	int			rc;
214 
215 	if (!pdev->dev.of_node)
216 		return -ENODEV;
217 
218 	rc = of_property_read_u32(pdev->dev.of_node, "ibm,opal-id", &opal_id);
219 	if (rc) {
220 		dev_err(&pdev->dev, "Missing ibm,opal-id property !\n");
221 		return -EIO;
222 	}
223 
224 	adapter = devm_kzalloc(&pdev->dev, sizeof(*adapter), GFP_KERNEL);
225 	if (!adapter)
226 		return -ENOMEM;
227 
228 	adapter->algo = &i2c_opal_algo;
229 	adapter->algo_data = (void *)(unsigned long)opal_id;
230 	adapter->quirks = &i2c_opal_quirks;
231 	adapter->dev.parent = &pdev->dev;
232 	adapter->dev.of_node = of_node_get(pdev->dev.of_node);
233 	pname = of_get_property(pdev->dev.of_node, "ibm,port-name", NULL);
234 	if (pname)
235 		strlcpy(adapter->name, pname, sizeof(adapter->name));
236 	else
237 		strlcpy(adapter->name, "opal", sizeof(adapter->name));
238 
239 	platform_set_drvdata(pdev, adapter);
240 	rc = i2c_add_adapter(adapter);
241 	if (rc)
242 		dev_err(&pdev->dev, "Failed to register the i2c adapter\n");
243 
244 	return rc;
245 }
246 
247 static int i2c_opal_remove(struct platform_device *pdev)
248 {
249 	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
250 
251 	i2c_del_adapter(adapter);
252 
253 	return 0;
254 }
255 
256 static const struct of_device_id i2c_opal_of_match[] = {
257 	{
258 		.compatible = "ibm,opal-i2c",
259 	},
260 	{ }
261 };
262 MODULE_DEVICE_TABLE(of, i2c_opal_of_match);
263 
264 static struct platform_driver i2c_opal_driver = {
265 	.probe	= i2c_opal_probe,
266 	.remove	= i2c_opal_remove,
267 	.driver	= {
268 		.name		= "i2c-opal",
269 		.of_match_table	= i2c_opal_of_match,
270 	},
271 };
272 
273 static int __init i2c_opal_init(void)
274 {
275 	if (!firmware_has_feature(FW_FEATURE_OPAL))
276 		return -ENODEV;
277 
278 	return platform_driver_register(&i2c_opal_driver);
279 }
280 module_init(i2c_opal_init);
281 
282 static void __exit i2c_opal_exit(void)
283 {
284 	return platform_driver_unregister(&i2c_opal_driver);
285 }
286 module_exit(i2c_opal_exit);
287 
288 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
289 MODULE_DESCRIPTION("IBM OPAL I2C driver");
290 MODULE_LICENSE("GPL");
291