Skip to content

Commit

Permalink
hwmon: (max6639) Add devicetree support
Browse files Browse the repository at this point in the history
Allow the driver to work with device tree support.

Signed-off-by: Marcello Sylvester Bauer <[email protected]>
  • Loading branch information
sylv-io committed Jan 3, 2022
1 parent df3d2ec commit 18f099a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions drivers/hwmon/max6639.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,38 @@ static int rpm_range_to_reg(int range)
return 1; /* default: 4000 RPM */
}

static struct max6639_platform_data *get_pdata_from_dt_node(struct device *dev)
{
struct max6639_platform_data *pdata;
u32 pol, ppr, rpm;
int ret;

pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return ERR_PTR(-ENOMEM);

pdata->pwm_polarity = true;
ret = of_property_read_u32(dev->of_node, "polarity", &pol);
if (!ret && !pol)
pdata->pwm_polarity = false;

ret = of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
if (ret < 0)
dev_warn(dev, "No pulses-per-revolution property\n");
else
pdata->ppr = ppr;

ret = of_property_read_u32(dev->of_node, "rpm-range", &rpm);
if (ret < 0) {
dev_warn(dev, "no rpm-range property\n");
pdata->rpm_range = 4000;
} else {
pdata->rpm_range = rpm;
}

return pdata;
}

static int max6639_init_client(struct i2c_client *client,
struct max6639_data *data)
{
Expand All @@ -421,6 +453,12 @@ static int max6639_init_client(struct i2c_client *client,
int rpm_range = 1; /* default: 4000 RPM */
int err;

if (!max6639_info && client->dev.of_node) {
max6639_info = get_pdata_from_dt_node(&client->dev);
if (IS_ERR(max6639_info))
return PTR_ERR(max6639_info);
}

/* Reset chip to default values, see below for GCONFIG setup */
err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
MAX6639_GCONFIG_POR);
Expand Down Expand Up @@ -631,13 +669,22 @@ static const struct i2c_device_id max6639_id[] = {

MODULE_DEVICE_TABLE(i2c, max6639_id);

#ifdef CONFIG_OF
static const struct of_device_id maxim_of_platform_match[] = {
{.compatible = "maxim,max6639"},
{},
};
MODULE_DEVICE_TABLE(of, maxim_of_platform_match);
#endif

static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);

static struct i2c_driver max6639_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "max6639",
.pm = &max6639_pm_ops,
.of_match_table = of_match_ptr(maxim_of_platform_match),
},
.probe_new = max6639_probe,
.id_table = max6639_id,
Expand Down

0 comments on commit 18f099a

Please sign in to comment.