1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * AMD MP2 platform driver
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.
8 * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
9 * Elie Morisse <syniurge@gmail.com>
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>
19 #include "i2c-amd-mp2.h"
21 #define AMD_MP2_I2C_MAX_RW_LENGTH ((1 << 12) - 1)
22 #define AMD_I2C_TIMEOUT (msecs_to_jiffies(250))
25 * struct amd_i2c_dev - MP2 bus/i2c adapter context
26 * @common: shared context with the MP2 PCI driver
27 * @pdev: platform driver node
29 * @cmd_complete: xfer completion object
32 struct amd_i2c_common common
;
33 struct platform_device
*pdev
;
34 struct i2c_adapter adap
;
35 struct completion cmd_complete
;
38 #define amd_i2c_dev_common(__common) \
39 container_of(__common, struct amd_i2c_dev, common)
41 static int i2c_amd_dma_map(struct amd_i2c_common
*i2c_common
)
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
;
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
,
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",
64 static void i2c_amd_dma_unmap(struct amd_i2c_common
*i2c_common
)
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
;
71 dma_unmap_single(dev_pci
, i2c_common
->dma_addr
,
72 i2c_common
->msg
->len
, dma_direction
);
74 i2c_put_dma_safe_msg_buf(i2c_common
->dma_buf
, i2c_common
->msg
, true);
77 static void i2c_amd_start_cmd(struct amd_i2c_dev
*i2c_dev
)
79 struct amd_i2c_common
*i2c_common
= &i2c_dev
->common
;
81 reinit_completion(&i2c_dev
->cmd_complete
);
82 i2c_common
->cmd_success
= false;
85 static void i2c_amd_cmd_completion(struct amd_i2c_common
*i2c_common
)
87 struct amd_i2c_dev
*i2c_dev
= amd_i2c_dev_common(i2c_common
);
88 union i2c_event
*event
= &i2c_common
->eventval
;
90 if (event
->r
.status
== i2c_readcomplete_event
)
91 dev_dbg(&i2c_dev
->pdev
->dev
, "readdata:%*ph\n", event
->r
.length
,
92 i2c_common
->msg
->buf
);
94 complete(&i2c_dev
->cmd_complete
);
97 static int i2c_amd_check_cmd_completion(struct amd_i2c_dev
*i2c_dev
)
99 struct amd_i2c_common
*i2c_common
= &i2c_dev
->common
;
100 unsigned long time_left
;
102 time_left
= wait_for_completion_timeout(&i2c_dev
->cmd_complete
,
103 i2c_dev
->adap
.timeout
);
105 if ((i2c_common
->reqcmd
== i2c_read
||
106 i2c_common
->reqcmd
== i2c_write
) &&
107 i2c_common
->msg
->len
> 32)
108 i2c_amd_dma_unmap(i2c_common
);
110 if (time_left
== 0) {
111 amd_mp2_rw_timeout(i2c_common
);
115 amd_mp2_process_event(i2c_common
);
117 if (!i2c_common
->cmd_success
)
123 static int i2c_amd_enable_set(struct amd_i2c_dev
*i2c_dev
, bool enable
)
125 struct amd_i2c_common
*i2c_common
= &i2c_dev
->common
;
127 i2c_amd_start_cmd(i2c_dev
);
128 amd_mp2_bus_enable_set(i2c_common
, enable
);
130 return i2c_amd_check_cmd_completion(i2c_dev
);
133 static int i2c_amd_xfer_msg(struct amd_i2c_dev
*i2c_dev
, struct i2c_msg
*pmsg
)
135 struct amd_i2c_common
*i2c_common
= &i2c_dev
->common
;
137 i2c_amd_start_cmd(i2c_dev
);
138 i2c_common
->msg
= pmsg
;
141 if (i2c_amd_dma_map(i2c_common
))
144 if (pmsg
->flags
& I2C_M_RD
)
145 amd_mp2_rw(i2c_common
, i2c_read
);
147 amd_mp2_rw(i2c_common
, i2c_write
);
149 return i2c_amd_check_cmd_completion(i2c_dev
);
152 static int i2c_amd_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
154 struct amd_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
156 struct i2c_msg
*pmsg
;
159 /* the adapter might have been deleted while waiting for the bus lock */
160 if (unlikely(!i2c_dev
->common
.mp2_dev
))
163 amd_mp2_pm_runtime_get(i2c_dev
->common
.mp2_dev
);
165 for (i
= 0; i
< num
; i
++) {
167 err
= i2c_amd_xfer_msg(i2c_dev
, pmsg
);
172 amd_mp2_pm_runtime_put(i2c_dev
->common
.mp2_dev
);
173 return err
? err
: num
;
176 static u32
i2c_amd_func(struct i2c_adapter
*a
)
178 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
181 static const struct i2c_algorithm i2c_amd_algorithm
= {
182 .master_xfer
= i2c_amd_xfer
,
183 .functionality
= i2c_amd_func
,
187 static int i2c_amd_suspend(struct amd_i2c_common
*i2c_common
)
189 struct amd_i2c_dev
*i2c_dev
= amd_i2c_dev_common(i2c_common
);
191 i2c_amd_enable_set(i2c_dev
, false);
195 static int i2c_amd_resume(struct amd_i2c_common
*i2c_common
)
197 struct amd_i2c_dev
*i2c_dev
= amd_i2c_dev_common(i2c_common
);
199 return i2c_amd_enable_set(i2c_dev
, true);
203 static const u32 supported_speeds
[] = {
204 I2C_MAX_HIGH_SPEED_MODE_FREQ
,
205 I2C_MAX_TURBO_MODE_FREQ
,
206 I2C_MAX_FAST_MODE_PLUS_FREQ
,
207 I2C_MAX_FAST_MODE_FREQ
,
208 I2C_MAX_STANDARD_MODE_FREQ
,
211 static enum speed_enum
i2c_amd_get_bus_speed(struct platform_device
*pdev
)
216 acpi_speed
= i2c_acpi_find_bus_speed(&pdev
->dev
);
217 /* round down to the lowest standard speed */
218 for (i
= 0; i
< ARRAY_SIZE(supported_speeds
); i
++) {
219 if (acpi_speed
>= supported_speeds
[i
])
222 acpi_speed
= i
< ARRAY_SIZE(supported_speeds
) ? supported_speeds
[i
] : 0;
224 switch (acpi_speed
) {
225 case I2C_MAX_STANDARD_MODE_FREQ
:
227 case I2C_MAX_FAST_MODE_FREQ
:
229 case I2C_MAX_FAST_MODE_PLUS_FREQ
:
231 case I2C_MAX_TURBO_MODE_FREQ
:
233 case I2C_MAX_HIGH_SPEED_MODE_FREQ
:
240 static const struct i2c_adapter_quirks amd_i2c_dev_quirks
= {
241 .max_read_len
= AMD_MP2_I2C_MAX_RW_LENGTH
,
242 .max_write_len
= AMD_MP2_I2C_MAX_RW_LENGTH
,
245 static int i2c_amd_probe(struct platform_device
*pdev
)
247 struct device
*dev
= &pdev
->dev
;
249 struct amd_i2c_dev
*i2c_dev
;
250 struct amd_mp2_dev
*mp2_dev
;
253 ret
= acpi_dev_uid_to_integer(ACPI_COMPANION(dev
), &uid
);
255 return dev_err_probe(dev
, ret
, "missing UID/bus id!\n");
257 return dev_err_probe(dev
, -EINVAL
, "incorrect UID/bus id \"%llu\"!\n", uid
);
258 dev_dbg(dev
, "bus id is %llu\n", uid
);
260 /* The ACPI namespace doesn't contain information about which MP2 PCI
261 * device an AMDI0011 ACPI device is related to, so assume that there's
262 * only one MP2 PCI device per system.
264 mp2_dev
= amd_mp2_find_device();
265 if (!mp2_dev
|| !mp2_dev
->probed
)
266 /* The MP2 PCI device should get probed later */
267 return -EPROBE_DEFER
;
269 i2c_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c_dev
), GFP_KERNEL
);
273 i2c_dev
->common
.bus_id
= uid
;
274 i2c_dev
->common
.mp2_dev
= mp2_dev
;
275 i2c_dev
->pdev
= pdev
;
276 platform_set_drvdata(pdev
, i2c_dev
);
278 i2c_dev
->common
.cmd_completion
= &i2c_amd_cmd_completion
;
280 i2c_dev
->common
.suspend
= &i2c_amd_suspend
;
281 i2c_dev
->common
.resume
= &i2c_amd_resume
;
284 /* Register the adapter */
285 amd_mp2_pm_runtime_get(mp2_dev
);
287 i2c_dev
->common
.reqcmd
= i2c_none
;
288 if (amd_mp2_register_cb(&i2c_dev
->common
))
290 device_link_add(&i2c_dev
->pdev
->dev
, &mp2_dev
->pci_dev
->dev
,
291 DL_FLAG_AUTOREMOVE_CONSUMER
);
293 i2c_dev
->common
.i2c_speed
= i2c_amd_get_bus_speed(pdev
);
295 /* Setup i2c adapter description */
296 i2c_dev
->adap
.owner
= THIS_MODULE
;
297 i2c_dev
->adap
.algo
= &i2c_amd_algorithm
;
298 i2c_dev
->adap
.quirks
= &amd_i2c_dev_quirks
;
299 i2c_dev
->adap
.dev
.parent
= &pdev
->dev
;
300 i2c_dev
->adap
.algo_data
= i2c_dev
;
301 i2c_dev
->adap
.timeout
= AMD_I2C_TIMEOUT
;
302 ACPI_COMPANION_SET(&i2c_dev
->adap
.dev
, ACPI_COMPANION(&pdev
->dev
));
303 i2c_dev
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
304 snprintf(i2c_dev
->adap
.name
, sizeof(i2c_dev
->adap
.name
),
305 "AMD MP2 i2c bus %u", i2c_dev
->common
.bus_id
);
306 i2c_set_adapdata(&i2c_dev
->adap
, i2c_dev
);
308 init_completion(&i2c_dev
->cmd_complete
);
311 if (i2c_amd_enable_set(i2c_dev
, true))
312 dev_err(&pdev
->dev
, "initial bus enable failed\n");
314 /* Attach to the i2c layer */
315 ret
= i2c_add_adapter(&i2c_dev
->adap
);
317 amd_mp2_pm_runtime_put(mp2_dev
);
320 dev_err(&pdev
->dev
, "i2c add adapter failed = %d\n", ret
);
325 static void i2c_amd_remove(struct platform_device
*pdev
)
327 struct amd_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
328 struct amd_i2c_common
*i2c_common
= &i2c_dev
->common
;
330 i2c_lock_bus(&i2c_dev
->adap
, I2C_LOCK_ROOT_ADAPTER
);
332 i2c_amd_enable_set(i2c_dev
, false);
333 amd_mp2_unregister_cb(i2c_common
);
334 i2c_common
->mp2_dev
= NULL
;
336 i2c_unlock_bus(&i2c_dev
->adap
, I2C_LOCK_ROOT_ADAPTER
);
338 i2c_del_adapter(&i2c_dev
->adap
);
341 static const struct acpi_device_id i2c_amd_acpi_match
[] = {
345 MODULE_DEVICE_TABLE(acpi
, i2c_amd_acpi_match
);
347 static struct platform_driver i2c_amd_plat_driver
= {
348 .probe
= i2c_amd_probe
,
349 .remove_new
= i2c_amd_remove
,
351 .name
= "i2c_amd_mp2",
352 .acpi_match_table
= ACPI_PTR(i2c_amd_acpi_match
),
355 module_platform_driver(i2c_amd_plat_driver
);
357 MODULE_DESCRIPTION("AMD(R) MP2 I2C Platform Driver");
358 MODULE_AUTHOR("Nehal Shah <nehal-bakulchandra.shah@amd.com>");
359 MODULE_AUTHOR("Elie Morisse <syniurge@gmail.com>");
360 MODULE_LICENSE("Dual BSD/GPL");