linux-next/drivers/i2c/busses/i2c-pasemi-platform.c

114 lines
2.8 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2021 The Asahi Linux Contributors
*
* PA Semi PWRficient SMBus host driver for Apple SoCs
*/
#include <linux/clk.h>
#include <linux/i2c.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include "i2c-pasemi-core.h"
struct pasemi_platform_i2c_data {
struct pasemi_smbus smbus;
struct clk *clk_ref;
};
static int
pasemi_platform_i2c_calc_clk_div(struct pasemi_platform_i2c_data *data,
u32 frequency)
{
unsigned long clk_rate = clk_get_rate(data->clk_ref);
if (!clk_rate)
return -EINVAL;
data->smbus.clk_div = DIV_ROUND_UP(clk_rate, 16 * frequency);
if (data->smbus.clk_div < 4)
return dev_err_probe(data->smbus.dev, -EINVAL,
"Bus frequency %d is too fast.\n",
frequency);
if (data->smbus.clk_div > 0xff)
return dev_err_probe(data->smbus.dev, -EINVAL,
"Bus frequency %d is too slow.\n",
frequency);
return 0;
}
static int pasemi_platform_i2c_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct pasemi_platform_i2c_data *data;
struct pasemi_smbus *smbus;
u32 frequency;
int error;
int irq_num;
data = devm_kzalloc(dev, sizeof(struct pasemi_platform_i2c_data),
GFP_KERNEL);
if (!data)
return -ENOMEM;
smbus = &data->smbus;
smbus->dev = dev;
smbus->ioaddr = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(smbus->ioaddr))
return PTR_ERR(smbus->ioaddr);
if (of_property_read_u32(dev->of_node, "clock-frequency", &frequency))
frequency = I2C_MAX_STANDARD_MODE_FREQ;
data->clk_ref = devm_clk_get_enabled(dev, NULL);
if (IS_ERR(data->clk_ref))
return PTR_ERR(data->clk_ref);
error = pasemi_platform_i2c_calc_clk_div(data, frequency);
if (error)
return error;
smbus->adapter.dev.of_node = pdev->dev.of_node;
error = pasemi_i2c_common_probe(smbus);
if (error)
return error;
irq_num = platform_get_irq(pdev, 0);
error = devm_request_irq(smbus->dev, irq_num, pasemi_irq_handler, 0, "pasemi_apple_i2c", (void *)smbus);
if (!error)
smbus->use_irq = 1;
platform_set_drvdata(pdev, data);
return 0;
}
static void pasemi_platform_i2c_remove(struct platform_device *pdev) { }
static const struct of_device_id pasemi_platform_i2c_of_match[] = {
{ .compatible = "apple,t8103-i2c" },
{ .compatible = "apple,i2c" },
{},
};
MODULE_DEVICE_TABLE(of, pasemi_platform_i2c_of_match);
static struct platform_driver pasemi_platform_i2c_driver = {
.driver = {
.name = "i2c-apple",
.of_match_table = pasemi_platform_i2c_of_match,
},
.probe = pasemi_platform_i2c_probe,
i2c: Convert to platform remove callback returning void The .remove() callback for a platform driver returns an int which makes many driver authors wrongly assume it's possible to do error handling by returning an error code. However the value returned is (mostly) ignored and this typically results in resource leaks. To improve here there is a quest to make the remove callback return void. In the first step of this quest all drivers are converted to .remove_new() which already returns void. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Acked-by: Alain Volmat <alain.volmat@foss.st.com> Acked-by: Ard Biesheuvel <ardb@kernel.org> Acked-by: Baruch Siach <baruch@tkos.co.il> Acked-by: Florian Fainelli <f.fainelli@gmail.com> Acked-by: Heiko Stuebner <heiko@sntech.de> Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com> Acked-by: Jernej Skrabec <jernej.skrabec@gmail.com> Acked-by: Jochen Friedrich <jochen@scram.de> Acked-by: Peter Rosin <peda@axentia.se> Acked-by: Vadim Pasternak <vadimp@nvidia.com> Reviewed-by: Asmaa Mnebhi <asnaa@nvidia.com> Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Reviewed-by: Chris Packham <chris.packham@alliedtelesis.co.nz> Reviewed-by: Chris Pringle <chris.pringle@phabrix.com> Reviewed-by: Claudiu Beznea <claudiu.beznea@microchip.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be> Reviewed-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Jean Delvare <jdelvare@suse.de> Reviewed-by: Konrad Dybcio <konrad.dybcio@linaro.org> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Reviewed-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com> Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com> Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com> Reviewed-by: Tali Perry <tali.perry@nuvoton.com> Reviewed-by: Vignesh Raghavendra <vigneshr@ti.com> Signed-off-by: Wolfram Sang <wsa@kernel.org>
2023-05-08 22:51:38 +02:00
.remove_new = pasemi_platform_i2c_remove,
};
module_platform_driver(pasemi_platform_i2c_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
MODULE_DESCRIPTION("Apple/PASemi SMBus platform driver");