• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * AMD MP2 platform driver
4  *
5  * Setup the I2C adapters enumerated in the ACPI namespace.
6  * MP2 controllers have 2 separate busses, up to 2 I2C adapters may be listed.
7  *
8  * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
9  *          Elie Morisse <syniurge@gmail.com>
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 
19 #include "i2c-amd-mp2.h"
20 
21 #define AMD_MP2_I2C_MAX_RW_LENGTH ((1 << 12) - 1)
22 #define AMD_I2C_TIMEOUT (msecs_to_jiffies(250))
23 
24 /**
25  * struct amd_i2c_dev - MP2 bus/i2c adapter context
26  * @common: shared context with the MP2 PCI driver
27  * @pdev: platform driver node
28  * @adap: i2c adapter
29  * @cmd_complete: xfer completion object
30  */
31 struct amd_i2c_dev {
32 	struct amd_i2c_common common;
33 	struct platform_device *pdev;
34 	struct i2c_adapter adap;
35 	struct completion cmd_complete;
36 };
37 
38 #define amd_i2c_dev_common(__common) \
39 	container_of(__common, struct amd_i2c_dev, common)
40 
i2c_amd_dma_map(struct amd_i2c_common * i2c_common)41 static int i2c_amd_dma_map(struct amd_i2c_common *i2c_common)
42 {
43 	struct device *dev_pci = &i2c_common->mp2_dev->pci_dev->dev;
44 	struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
45 	enum dma_data_direction dma_direction =
46 			i2c_common->msg->flags & I2C_M_RD ?
47 			DMA_FROM_DEVICE : DMA_TO_DEVICE;
48 
49 	i2c_common->dma_buf = i2c_get_dma_safe_msg_buf(i2c_common->msg, 0);
50 	i2c_common->dma_addr = dma_map_single(dev_pci, i2c_common->dma_buf,
51 					      i2c_common->msg->len,
52 					      dma_direction);
53 
54 	if (unlikely(dma_mapping_error(dev_pci, i2c_common->dma_addr))) {
55 		dev_err(&i2c_dev->pdev->dev,
56 			"Error while mapping dma buffer %p\n",
57 			i2c_common->dma_buf);
58 		return -EIO;
59 	}
60 
61 	return 0;
62 }
63 
i2c_amd_dma_unmap(struct amd_i2c_common * i2c_common)64 static void i2c_amd_dma_unmap(struct amd_i2c_common *i2c_common)
65 {
66 	struct device *dev_pci = &i2c_common->mp2_dev->pci_dev->dev;
67 	enum dma_data_direction dma_direction =
68 			i2c_common->msg->flags & I2C_M_RD ?
69 			DMA_FROM_DEVICE : DMA_TO_DEVICE;
70 
71 	dma_unmap_single(dev_pci, i2c_common->dma_addr,
72 			 i2c_common->msg->len, dma_direction);
73 
74 	i2c_put_dma_safe_msg_buf(i2c_common->dma_buf, i2c_common->msg, true);
75 }
76 
i2c_amd_start_cmd(struct amd_i2c_dev * i2c_dev)77 static void i2c_amd_start_cmd(struct amd_i2c_dev *i2c_dev)
78 {
79 	struct amd_i2c_common *i2c_common = &i2c_dev->common;
80 
81 	reinit_completion(&i2c_dev->cmd_complete);
82 	i2c_common->cmd_success = false;
83 }
84 
i2c_amd_cmd_completion(struct amd_i2c_common * i2c_common)85 static void i2c_amd_cmd_completion(struct amd_i2c_common *i2c_common)
86 {
87 	struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
88 	union i2c_event *event = &i2c_common->eventval;
89 
90 	if (event->r.status == i2c_readcomplete_event)
91 		dev_dbg(&i2c_dev->pdev->dev, "%s readdata:%*ph\n",
92 			__func__, event->r.length,
93 			i2c_common->msg->buf);
94 
95 	complete(&i2c_dev->cmd_complete);
96 }
97 
i2c_amd_check_cmd_completion(struct amd_i2c_dev * i2c_dev)98 static int i2c_amd_check_cmd_completion(struct amd_i2c_dev *i2c_dev)
99 {
100 	struct amd_i2c_common *i2c_common = &i2c_dev->common;
101 	unsigned long timeout;
102 
103 	timeout = wait_for_completion_timeout(&i2c_dev->cmd_complete,
104 					      i2c_dev->adap.timeout);
105 
106 	if ((i2c_common->reqcmd == i2c_read ||
107 	     i2c_common->reqcmd == i2c_write) &&
108 	    i2c_common->msg->len > 32)
109 		i2c_amd_dma_unmap(i2c_common);
110 
111 	if (timeout == 0) {
112 		amd_mp2_rw_timeout(i2c_common);
113 		return -ETIMEDOUT;
114 	}
115 
116 	amd_mp2_process_event(i2c_common);
117 
118 	if (!i2c_common->cmd_success)
119 		return -EIO;
120 
121 	return 0;
122 }
123 
i2c_amd_enable_set(struct amd_i2c_dev * i2c_dev,bool enable)124 static int i2c_amd_enable_set(struct amd_i2c_dev *i2c_dev, bool enable)
125 {
126 	struct amd_i2c_common *i2c_common = &i2c_dev->common;
127 
128 	i2c_amd_start_cmd(i2c_dev);
129 	amd_mp2_bus_enable_set(i2c_common, enable);
130 
131 	return i2c_amd_check_cmd_completion(i2c_dev);
132 }
133 
i2c_amd_xfer_msg(struct amd_i2c_dev * i2c_dev,struct i2c_msg * pmsg)134 static int i2c_amd_xfer_msg(struct amd_i2c_dev *i2c_dev, struct i2c_msg *pmsg)
135 {
136 	struct amd_i2c_common *i2c_common = &i2c_dev->common;
137 
138 	i2c_amd_start_cmd(i2c_dev);
139 	i2c_common->msg = pmsg;
140 
141 	if (pmsg->len > 32)
142 		if (i2c_amd_dma_map(i2c_common))
143 			return -EIO;
144 
145 	if (pmsg->flags & I2C_M_RD)
146 		amd_mp2_rw(i2c_common, i2c_read);
147 	else
148 		amd_mp2_rw(i2c_common, i2c_write);
149 
150 	return i2c_amd_check_cmd_completion(i2c_dev);
151 }
152 
i2c_amd_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)153 static int i2c_amd_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
154 {
155 	struct amd_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
156 	int i;
157 	struct i2c_msg *pmsg;
158 	int err = 0;
159 
160 	/* the adapter might have been deleted while waiting for the bus lock */
161 	if (unlikely(!i2c_dev->common.mp2_dev))
162 		return -EINVAL;
163 
164 	amd_mp2_pm_runtime_get(i2c_dev->common.mp2_dev);
165 
166 	for (i = 0; i < num; i++) {
167 		pmsg = &msgs[i];
168 		err = i2c_amd_xfer_msg(i2c_dev, pmsg);
169 		if (err)
170 			break;
171 	}
172 
173 	amd_mp2_pm_runtime_put(i2c_dev->common.mp2_dev);
174 	return err ? err : num;
175 }
176 
i2c_amd_func(struct i2c_adapter * a)177 static u32 i2c_amd_func(struct i2c_adapter *a)
178 {
179 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
180 }
181 
182 static const struct i2c_algorithm i2c_amd_algorithm = {
183 	.master_xfer = i2c_amd_xfer,
184 	.functionality = i2c_amd_func,
185 };
186 
187 #ifdef CONFIG_PM
i2c_amd_suspend(struct amd_i2c_common * i2c_common)188 static int i2c_amd_suspend(struct amd_i2c_common *i2c_common)
189 {
190 	struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
191 
192 	i2c_amd_enable_set(i2c_dev, false);
193 	return 0;
194 }
195 
i2c_amd_resume(struct amd_i2c_common * i2c_common)196 static int i2c_amd_resume(struct amd_i2c_common *i2c_common)
197 {
198 	struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
199 
200 	return i2c_amd_enable_set(i2c_dev, true);
201 }
202 #endif
203 
204 static const u32 supported_speeds[] = {
205 	I2C_MAX_HIGH_SPEED_MODE_FREQ,
206 	I2C_MAX_TURBO_MODE_FREQ,
207 	I2C_MAX_FAST_MODE_PLUS_FREQ,
208 	I2C_MAX_FAST_MODE_FREQ,
209 	I2C_MAX_STANDARD_MODE_FREQ,
210 };
211 
i2c_amd_get_bus_speed(struct platform_device * pdev)212 static enum speed_enum i2c_amd_get_bus_speed(struct platform_device *pdev)
213 {
214 	u32 acpi_speed;
215 	int i;
216 
217 	acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
218 	/* round down to the lowest standard speed */
219 	for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
220 		if (acpi_speed >= supported_speeds[i])
221 			break;
222 	}
223 	acpi_speed = i < ARRAY_SIZE(supported_speeds) ? supported_speeds[i] : 0;
224 
225 	switch (acpi_speed) {
226 	case I2C_MAX_STANDARD_MODE_FREQ:
227 		return speed100k;
228 	case I2C_MAX_FAST_MODE_FREQ:
229 		return speed400k;
230 	case I2C_MAX_FAST_MODE_PLUS_FREQ:
231 		return speed1000k;
232 	case I2C_MAX_TURBO_MODE_FREQ:
233 		return speed1400k;
234 	case I2C_MAX_HIGH_SPEED_MODE_FREQ:
235 		return speed3400k;
236 	default:
237 		return speed400k;
238 	}
239 }
240 
241 static const struct i2c_adapter_quirks amd_i2c_dev_quirks = {
242 	.max_read_len = AMD_MP2_I2C_MAX_RW_LENGTH,
243 	.max_write_len = AMD_MP2_I2C_MAX_RW_LENGTH,
244 };
245 
i2c_amd_probe(struct platform_device * pdev)246 static int i2c_amd_probe(struct platform_device *pdev)
247 {
248 	int ret;
249 	struct amd_i2c_dev *i2c_dev;
250 	acpi_handle handle = ACPI_HANDLE(&pdev->dev);
251 	struct acpi_device *adev;
252 	struct amd_mp2_dev *mp2_dev;
253 	const char *uid;
254 
255 	if (acpi_bus_get_device(handle, &adev))
256 		return -ENODEV;
257 
258 	/* The ACPI namespace doesn't contain information about which MP2 PCI
259 	 * device an AMDI0011 ACPI device is related to, so assume that there's
260 	 * only one MP2 PCI device per system.
261 	 */
262 	mp2_dev = amd_mp2_find_device();
263 	if (!mp2_dev || !mp2_dev->probed)
264 		/* The MP2 PCI device should get probed later */
265 		return -EPROBE_DEFER;
266 
267 	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
268 	if (!i2c_dev)
269 		return -ENOMEM;
270 
271 	i2c_dev->common.mp2_dev = mp2_dev;
272 	i2c_dev->pdev = pdev;
273 	platform_set_drvdata(pdev, i2c_dev);
274 
275 	i2c_dev->common.cmd_completion = &i2c_amd_cmd_completion;
276 #ifdef CONFIG_PM
277 	i2c_dev->common.suspend = &i2c_amd_suspend;
278 	i2c_dev->common.resume = &i2c_amd_resume;
279 #endif
280 
281 	uid = adev->pnp.unique_id;
282 	if (!uid) {
283 		dev_err(&pdev->dev, "missing UID/bus id!\n");
284 		return -EINVAL;
285 	} else if (strcmp(uid, "0") == 0) {
286 		i2c_dev->common.bus_id = 0;
287 	} else if (strcmp(uid, "1") == 0) {
288 		i2c_dev->common.bus_id = 1;
289 	} else {
290 		dev_err(&pdev->dev, "incorrect UID/bus id \"%s\"!\n", uid);
291 		return -EINVAL;
292 	}
293 	dev_dbg(&pdev->dev, "bus id is %u\n", i2c_dev->common.bus_id);
294 
295 	/* Register the adapter */
296 	amd_mp2_pm_runtime_get(mp2_dev);
297 
298 	i2c_dev->common.reqcmd = i2c_none;
299 	if (amd_mp2_register_cb(&i2c_dev->common))
300 		return -EINVAL;
301 	device_link_add(&i2c_dev->pdev->dev, &mp2_dev->pci_dev->dev,
302 			DL_FLAG_AUTOREMOVE_CONSUMER);
303 
304 	i2c_dev->common.i2c_speed = i2c_amd_get_bus_speed(pdev);
305 
306 	/* Setup i2c adapter description */
307 	i2c_dev->adap.owner = THIS_MODULE;
308 	i2c_dev->adap.algo = &i2c_amd_algorithm;
309 	i2c_dev->adap.quirks = &amd_i2c_dev_quirks;
310 	i2c_dev->adap.dev.parent = &pdev->dev;
311 	i2c_dev->adap.algo_data = i2c_dev;
312 	i2c_dev->adap.timeout = AMD_I2C_TIMEOUT;
313 	ACPI_COMPANION_SET(&i2c_dev->adap.dev, ACPI_COMPANION(&pdev->dev));
314 	i2c_dev->adap.dev.of_node = pdev->dev.of_node;
315 	snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
316 		 "AMD MP2 i2c bus %u", i2c_dev->common.bus_id);
317 	i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
318 
319 	init_completion(&i2c_dev->cmd_complete);
320 
321 	/* Enable the bus */
322 	if (i2c_amd_enable_set(i2c_dev, true))
323 		dev_err(&pdev->dev, "initial bus enable failed\n");
324 
325 	/* Attach to the i2c layer */
326 	ret = i2c_add_adapter(&i2c_dev->adap);
327 
328 	amd_mp2_pm_runtime_put(mp2_dev);
329 
330 	if (ret < 0)
331 		dev_err(&pdev->dev, "i2c add adapter failed = %d\n", ret);
332 
333 	return ret;
334 }
335 
i2c_amd_remove(struct platform_device * pdev)336 static int i2c_amd_remove(struct platform_device *pdev)
337 {
338 	struct amd_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
339 	struct amd_i2c_common *i2c_common = &i2c_dev->common;
340 
341 	i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
342 
343 	i2c_amd_enable_set(i2c_dev, false);
344 	amd_mp2_unregister_cb(i2c_common);
345 	i2c_common->mp2_dev = NULL;
346 
347 	i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
348 
349 	i2c_del_adapter(&i2c_dev->adap);
350 	return 0;
351 }
352 
353 static const struct acpi_device_id i2c_amd_acpi_match[] = {
354 	{ "AMDI0011" },
355 	{ },
356 };
357 MODULE_DEVICE_TABLE(acpi, i2c_amd_acpi_match);
358 
359 static struct platform_driver i2c_amd_plat_driver = {
360 	.probe = i2c_amd_probe,
361 	.remove = i2c_amd_remove,
362 	.driver = {
363 		.name = "i2c_amd_mp2",
364 		.acpi_match_table = ACPI_PTR(i2c_amd_acpi_match),
365 	},
366 };
367 module_platform_driver(i2c_amd_plat_driver);
368 
369 MODULE_DESCRIPTION("AMD(R) MP2 I2C Platform Driver");
370 MODULE_AUTHOR("Nehal Shah <nehal-bakulchandra.shah@amd.com>");
371 MODULE_AUTHOR("Elie Morisse <syniurge@gmail.com>");
372 MODULE_LICENSE("Dual BSD/GPL");
373