Linux Audio

Check our new training course

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