acpi_apd.c 用于创建I2C device
static const struct acpi_device_id acpi_apd_device_ids[] = {
/* Generic apd devices */
#ifdef CONFIG_ARM64
{ "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
{ "BRCM900D", APD_ADDR(vulcan_spi_desc) },
{ "CAV900D", APD_ADDR(vulcan_spi_desc) },
{ "HISI0A21", APD_ADDR(hip07_i2c_desc) },
{ "HISI0A22", APD_ADDR(hip08_i2c_desc) },
#endif
{ }
};
static struct acpi_scan_handler apd_handler = {
.ids = acpi_apd_device_ids,
.attach = acpi_apd_create_device,
};
void __init acpi_apd_init(void)
{
acpi_scan_add_handler(&apd_handler);
}
如果bios传递过来的apci的dsdt子表中有acpi_apd_device_ids 中定义的id,就会调用apd_handler.attach 函数,这里就是对应的acpi_apd_create_device
以HISI0A21为例
static int acpi_apd_create_device(struct acpi_device *adev,
const struct acpi_device_id *id)
{
//从id中拿到apd_device_desc,这里HISI0A21的desc不为null
const struct apd_device_desc *dev_desc = (void *)id->driver_data;
struct apd_private_data *pdata;
struct platform_device *pdev;
int ret;
if (!dev_desc) {
pdev = acpi_create_platform_device(adev, NULL);
return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
}
pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdata->adev = adev;
pdata->dev_desc = dev_desc;
//调用dev_desc->setup,本例对应的就是acpi_apd_setup
if (dev_desc->setup) {
ret = dev_desc->setup(pdata);
if (ret)
goto err_out;
}
adev->driver_data = pdata;
//新建I2C device
pdev = acpi_create_platform_device(adev, dev_desc->properties);
if (!IS_ERR_OR_NULL(pdev))
return 1;
ret = PTR_ERR(pdev);
adev->driver_data = NULL;
err_out:
kfree(pdata);
return ret;
}
static const struct apd_device_desc hip07_i2c_desc = {
.setup = acpi_apd_setup,
.fixed_clk_rate = 200000000,
};
来看看的desc的setup函数
static int acpi_apd_setup(struct apd_private_data *pdata)
{
const struct apd_device_desc *dev_desc = pdata->dev_desc;
struct clk *clk = ERR_PTR(-ENODEV);
//这里的fixed_clk_rate等于200000000。因此if条件成立
if (dev_desc->fixed_clk_rate) {
首先注册一个clk,
clk = clk_register_fixed_rate(&pdata->adev->dev,
dev_name(&pdata->adev->dev),
NULL, 0, dev_desc->fixed_clk_rate);
用clk注册一个clkdev
clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
保存这个clk,后续就额可以通过pdata->clk得到clk,再通过clk找到clkdev
pdata->clk = clk;
}
return 0;
}
static const struct acpi_device_id acpi_apd_device_ids[] = {
/* Generic apd devices */
#ifdef CONFIG_ARM64
{ "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
{ "BRCM900D", APD_ADDR(vulcan_spi_desc) },
{ "CAV900D", APD_ADDR(vulcan_spi_desc) },
{ "HISI0A21", APD_ADDR(hip07_i2c_desc) },
{ "HISI0A22", APD_ADDR(hip08_i2c_desc) },
#endif
{ }
};
static struct acpi_scan_handler apd_handler = {
.ids = acpi_apd_device_ids,
.attach = acpi_apd_create_device,
};
void __init acpi_apd_init(void)
{
acpi_scan_add_handler(&apd_handler);
}
如果bios传递过来的apci的dsdt子表中有acpi_apd_device_ids 中定义的id,就会调用apd_handler.attach 函数,这里就是对应的acpi_apd_create_device
以HISI0A21为例
static int acpi_apd_create_device(struct acpi_device *adev,
const struct acpi_device_id *id)
{
//从id中拿到apd_device_desc,这里HISI0A21的desc不为null
const struct apd_device_desc *dev_desc = (void *)id->driver_data;
struct apd_private_data *pdata;
struct platform_device *pdev;
int ret;
if (!dev_desc) {
pdev = acpi_create_platform_device(adev, NULL);
return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
}
pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdata->adev = adev;
pdata->dev_desc = dev_desc;
//调用dev_desc->setup,本例对应的就是acpi_apd_setup
if (dev_desc->setup) {
ret = dev_desc->setup(pdata);
if (ret)
goto err_out;
}
adev->driver_data = pdata;
//新建I2C device
pdev = acpi_create_platform_device(adev, dev_desc->properties);
if (!IS_ERR_OR_NULL(pdev))
return 1;
ret = PTR_ERR(pdev);
adev->driver_data = NULL;
err_out:
kfree(pdata);
return ret;
}
static const struct apd_device_desc hip07_i2c_desc = {
.setup = acpi_apd_setup,
.fixed_clk_rate = 200000000,
};
来看看的desc的setup函数
static int acpi_apd_setup(struct apd_private_data *pdata)
{
const struct apd_device_desc *dev_desc = pdata->dev_desc;
struct clk *clk = ERR_PTR(-ENODEV);
//这里的fixed_clk_rate等于200000000。因此if条件成立
if (dev_desc->fixed_clk_rate) {
首先注册一个clk,
clk = clk_register_fixed_rate(&pdata->adev->dev,
dev_name(&pdata->adev->dev),
NULL, 0, dev_desc->fixed_clk_rate);
用clk注册一个clkdev
clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
保存这个clk,后续就额可以通过pdata->clk得到clk,再通过clk找到clkdev
pdata->clk = clk;
}
return 0;
}
例如我们可以通过u32rate=clk_get_rate(pdata->clk)拿到这个clock的rate。本例中就是200000000
unsigned long clk_get_rate(struct clk *clk)
{
if (!clk)
return 0;
return clk_core_get_rate(clk->core);
}