xref: /linux/drivers/i2c/busses/i2c-pasemi-platform.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 The Asahi Linux Contributors
4  *
5  * PA Semi PWRficient SMBus host driver for Apple SoCs
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/i2c.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/types.h>
15 
16 #include "i2c-pasemi-core.h"
17 
18 struct pasemi_platform_i2c_data {
19 	struct pasemi_smbus smbus;
20 	struct clk *clk_ref;
21 };
22 
23 static int
24 pasemi_platform_i2c_calc_clk_div(struct pasemi_platform_i2c_data *data,
25 				 u32 frequency)
26 {
27 	unsigned long clk_rate = clk_get_rate(data->clk_ref);
28 
29 	if (!clk_rate)
30 		return -EINVAL;
31 
32 	data->smbus.clk_div = DIV_ROUND_UP(clk_rate, 16 * frequency);
33 	if (data->smbus.clk_div < 4)
34 		return dev_err_probe(data->smbus.dev, -EINVAL,
35 				     "Bus frequency %d is too fast.\n",
36 				     frequency);
37 	if (data->smbus.clk_div > 0xff)
38 		return dev_err_probe(data->smbus.dev, -EINVAL,
39 				     "Bus frequency %d is too slow.\n",
40 				     frequency);
41 
42 	return 0;
43 }
44 
45 static int pasemi_platform_i2c_probe(struct platform_device *pdev)
46 {
47 	struct device *dev = &pdev->dev;
48 	struct pasemi_platform_i2c_data *data;
49 	struct pasemi_smbus *smbus;
50 	u32 frequency;
51 	int error;
52 	int irq_num;
53 
54 	data = devm_kzalloc(dev, sizeof(struct pasemi_platform_i2c_data),
55 			    GFP_KERNEL);
56 	if (!data)
57 		return -ENOMEM;
58 
59 	smbus = &data->smbus;
60 	smbus->dev = dev;
61 
62 	smbus->ioaddr = devm_platform_ioremap_resource(pdev, 0);
63 	if (IS_ERR(smbus->ioaddr))
64 		return PTR_ERR(smbus->ioaddr);
65 
66 	if (of_property_read_u32(dev->of_node, "clock-frequency", &frequency))
67 		frequency = I2C_MAX_STANDARD_MODE_FREQ;
68 
69 	data->clk_ref = devm_clk_get_enabled(dev, NULL);
70 	if (IS_ERR(data->clk_ref))
71 		return PTR_ERR(data->clk_ref);
72 
73 	error = pasemi_platform_i2c_calc_clk_div(data, frequency);
74 	if (error)
75 		return error;
76 
77 	smbus->adapter.dev.of_node = pdev->dev.of_node;
78 	error = pasemi_i2c_common_probe(smbus);
79 	if (error)
80 		return error;
81 
82 	irq_num = platform_get_irq(pdev, 0);
83 	error = devm_request_irq(smbus->dev, irq_num, pasemi_irq_handler, 0, "pasemi_apple_i2c", (void *)smbus);
84 
85 	if (!error)
86 		smbus->use_irq = 1;
87 	platform_set_drvdata(pdev, data);
88 
89 	return 0;
90 }
91 
92 static void pasemi_platform_i2c_remove(struct platform_device *pdev) { }
93 
94 static const struct of_device_id pasemi_platform_i2c_of_match[] = {
95 	{ .compatible = "apple,t8103-i2c" },
96 	{ .compatible = "apple,i2c" },
97 	{},
98 };
99 MODULE_DEVICE_TABLE(of, pasemi_platform_i2c_of_match);
100 
101 static struct platform_driver pasemi_platform_i2c_driver = {
102 	.driver	= {
103 		.name			= "i2c-apple",
104 		.of_match_table		= pasemi_platform_i2c_of_match,
105 	},
106 	.probe	= pasemi_platform_i2c_probe,
107 	.remove = pasemi_platform_i2c_remove,
108 };
109 module_platform_driver(pasemi_platform_i2c_driver);
110 
111 MODULE_LICENSE("GPL");
112 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
113 MODULE_DESCRIPTION("Apple/PASemi SMBus platform driver");
114