Skip to contentSkip to conversation

regulator: new drivers for AD5398 and AD5821

PATCH Superseded sonic zhang 3 participants 9 messages Jun 2, 2010 – Jun 3, 2010

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 regulator: new drivers for AD5398 and AD5821

The AD5398 and AD5821 are single 10-bit DAC with 120 mA output current sink capability. They feature an internal reference and operates from a single 2.7 V to 5.5 V supply.

This driver supports both the AD5398 and the AD5821. It adapts into the voltage and current framework.

---
 drivers/regulator/Kconfig  |    6 +
 drivers/regulator/Makefile |    1 +
 drivers/regulator/ad5398.c |  285 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 292 insertions(+), 0 deletions(-)
 create mode 100644 drivers/regulator/ad5398.c
drivers/regulator/Kconfig +6 -0
index 04f2e08..2ade09c 100644
@@ -201,5 +201,11 @@ config REGULATOR_88PM8607
 	help
 	  This driver supports 88PM8607 voltage regulator chips.
 
+config REGULATOR_AD5398
+	tristate "Ananlog Devices AD5398/AD5821 regulators"
+	depends on I2C
+	help
+	  This driver supports AD5398 and AD5821 current regulator chips.
+
 endif
 
drivers/regulator/Makefile +1 -0
index 4e7feec..c256668 100644
@@ -8,6 +8,7 @@ obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE) += fixed.o
 obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER) += virtual.o
 obj-$(CONFIG_REGULATOR_USERSPACE_CONSUMER) += userspace-consumer.o
 
+obj-$(CONFIG_REGULATOR_AD5398) += ad5398.o
 obj-$(CONFIG_REGULATOR_BQ24022) += bq24022.o
 obj-$(CONFIG_REGULATOR_DUMMY) += dummy.o
 obj-$(CONFIG_REGULATOR_LP3971) += lp3971.o
drivers/regulator/ad5398.c new file +285 -0

285 lines not on this page · raw message

Signature · 1 line
1.6.0
#1 lore raw
Quoted 9 lines · sonic zhang

The AD5398 and AD5821 are single 10-bit DAC with 120 mA output current sink capability. They feature an internal reference and operates from a single 2.7 V to 5.5 V supply.

This driver supports both the AD5398 and the AD5821.  It adapts into the voltage and current framework.

the "From:" doesnt match your s-o-b tag ...

also, this dropped my s-o-b:

Quoted 2 lines

+config REGULATOR_AD5398
+       tristate "Ananlog Devices AD5398/AD5821 regulators"

Analog

Quoted 3 lines

+       depends on I2C
+       help
+         This driver supports AD5398 and AD5821 current regulator chips.

should mention the module name if built as a module

drivers/regulator/ad5398.c line 2

Quoted 5 lines
drivers/regulator/ad5398.c new file +2 -0
@@ -0,0 +1,285 @@
+/*
+ * ad5398.c  --  Voltage and current regulation for AD5398 and AD5821

dont think the filename needs to be here

drivers/regulator/ad5398.c line 10

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+ */
+#include <linux/module.h>

should prob be a newline between these

drivers/regulator/ad5398.c line 114

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+       /* write the new current value back as well as enable bit */
+       ret = ad5398_write_reg(client, (unsigned short)selector);

the prototype of write_reg takes an unsigned short, so this cast is useless

drivers/regulator/ad5398.c line 186

Quoted 9 lines
drivers/regulator/ad5398.c new file +9 -0
+static struct regulator_ops ad5398_ops = {
+       .get_current_limit = ad5398_get_current_limit,
+       .set_current_limit = ad5398_set_current_limit,
+       .enable = ad5398_enable,
+       .disable = ad5398_disable,
+       .is_enabled = ad5398_is_enabled,
+};
+
+static struct regulator_desc ad5398_reg = {

not specific to this driver, but it looks like regulator_ops and regulator_desc really should be constified

drivers/regulator/ad5398.c line 206

Quoted 8 lines
drivers/regulator/ad5398.c new file +8 -0
+static const struct ad5398_current_data_format ad5398_df = {10, 4};
+static const struct ad5398_current_data_format ad5821_df = {10, 4};
+
+static const struct i2c_device_id ad5398_id[] = {
+       { "ad5398", (kernel_ulong_t)&ad5398_df },
+       { "ad5821", (kernel_ulong_t)&ad5821_df },
+       { }
+};

do you really need sep storage for these _df vars ?

drivers/regulator/ad5398.c line 264

Quoted 3 lines
drivers/regulator/ad5398.c new file +3 -0
+static int ad5398_probe(struct i2c_client *client,
+static int ad5398_remove(struct i2c_client *client)
+       .remove = ad5398_remove,

missing hotplug section markings

drivers/regulator/ad5398.c line 216

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+       struct ad5398_current_data_format *df =
+                       (struct ad5398_current_data_format *)id->driver_data;

this too should be const

drivers/regulator/ad5398.c line 222

Quoted 1 line
drivers/regulator/ad5398.c new file +1 -0
+       chip = kzalloc(sizeof(struct ad5398_chip_info), GFP_KERNEL);

sizeof(*chip)

drivers/regulator/ad5398.c line 243

Quoted 1 line
drivers/regulator/ad5398.c new file +1 -0
+       dev_dbg(&client->dev, "%s regulator driver loaded\n", id->name);

does the regulator core take care of displaying a notice ? if not, i'd make this dev_info(). also, this should be "registered", not "loaded".

drivers/regulator/ad5398.c line 285

Quoted 3 lines
drivers/regulator/ad5398.c new file +3 -0
+MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
+MODULE_AUTHOR("Sonic Zhang");
+MODULE_LICENSE("GPL");

should there be a MODULE_ALIAS() ?
-mike

#2 lore raw
Quoted 5 lines · Mike Frysinger
Quoted 4 lines · sonic zhang

The AD5398 and AD5821 are single 10-bit DAC with 120 mA output current sink capability. They feature an internal reference and operates from a single 2.7 V to 5.5 V supply.

Quoted 8 lines
Quoted 5 lines

This driver supports both the AD5398 and the AD5821.  It adapts into the voltage and current framework.

the "From:" doesnt match your s-o-b tag ...

Does this need to be matched? I prefer to discuss via gmail account while keep company email in the patch owner information.

Quoted 45 lines

also, this dropped my s-o-b:

Quoted 2 lines

+config REGULATOR_AD5398
+       tristate "Ananlog Devices AD5398/AD5821 regulators"

Analog

Quoted 3 lines

+       depends on I2C
+       help
+         This driver supports AD5398 and AD5821 current regulator chips.

should mention the module name if built as a module

drivers/regulator/ad5398.c line 2

Quoted 5 lines
drivers/regulator/ad5398.c new file +2 -0
@@ -0,0 +1,285 @@
+/*
+ * ad5398.c  --  Voltage and current regulation for AD5398 and AD5821

dont think the filename needs to be here

drivers/regulator/ad5398.c line 10

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+ */
+#include <linux/module.h>

should prob be a newline between these

drivers/regulator/ad5398.c line 114

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+       /* write the new current value back as well as enable bit */
+       ret = ad5398_write_reg(client, (unsigned short)selector);

the prototype of write_reg takes an unsigned short, so this cast is useless

drivers/regulator/ad5398.c line 186

Quoted 9 lines
drivers/regulator/ad5398.c new file +9 -0
+static struct regulator_ops ad5398_ops = {
+       .get_current_limit = ad5398_get_current_limit,
+       .set_current_limit = ad5398_set_current_limit,
+       .enable = ad5398_enable,
+       .disable = ad5398_disable,
+       .is_enabled = ad5398_is_enabled,
+};
+
+static struct regulator_desc ad5398_reg = {

not specific to this driver, but it looks like regulator_ops and regulator_desc really should be constified

Quoted 11 lines

drivers/regulator/ad5398.c line 206

Quoted 8 lines
drivers/regulator/ad5398.c new file +8 -0
+static const struct ad5398_current_data_format ad5398_df = {10, 4};
+static const struct ad5398_current_data_format ad5821_df = {10, 4};
+
+static const struct i2c_device_id ad5398_id[] = {
+       { "ad5398", (kernel_ulong_t)&ad5398_df },
+       { "ad5821", (kernel_ulong_t)&ad5821_df },
+       { }
+};

do you really need sep storage for these _df vars ?

Yes, this makes probe code simpler.

Quoted 13 lines

drivers/regulator/ad5398.c line 264

Quoted 3 lines
drivers/regulator/ad5398.c new file +3 -0
+static int ad5398_probe(struct i2c_client *client,
+static int ad5398_remove(struct i2c_client *client)
+       .remove = ad5398_remove,

missing hotplug section markings

drivers/regulator/ad5398.c line 216

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+       struct ad5398_current_data_format *df =
+                       (struct ad5398_current_data_format *)id->driver_data;

this too should be const

drivers/regulator/ad5398.c line 222

Quoted 1 line
drivers/regulator/ad5398.c new file +1 -0
+       chip = kzalloc(sizeof(struct ad5398_chip_info), GFP_KERNEL);
Quoted 8 lines

sizeof(*chip)

drivers/regulator/ad5398.c line 243

Quoted 1 line
drivers/regulator/ad5398.c new file +1 -0
+       dev_dbg(&client->dev, "%s regulator driver loaded\n", id->name);

does the regulator core take care of displaying a notice ?  if not, i'd make this dev_info().  also, this should be "registered", not "loaded".

Mark prefers to have it printed only for debugging mode.

Quoted 7 lines

drivers/regulator/ad5398.c line 285

Quoted 3 lines
drivers/regulator/ad5398.c new file +3 -0
+MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
+MODULE_AUTHOR("Sonic Zhang");
+MODULE_LICENSE("GPL");

should there be a MODULE_ALIAS() ?
-mike

#3 lore raw
Quoted 10 lines · Sonic Zhang
Quoted 9 lines · Mike Frysinger
Quoted 8 lines · sonic zhang

The AD5398 and AD5821 are single 10-bit DAC with 120 mA output current sink capability. They feature an internal reference and operates from a single 2.7 V to 5.5 V supply.

This driver supports both the AD5398 and the AD5821.  It adapts into the voltage and current framework.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

the "From:" doesnt match your s-o-b tag ...

Does this need to be matched? I prefer to discuss via gmail account while keep company email in the patch owner information.

so set the From: in the body as the first line like git-send-email does it:
From: <author info>

<commit message>

<other tags>
---
<diffstat>
..........

Quoted 11 lines
Quoted 10 lines

drivers/regulator/ad5398.c line 206

Quoted 8 lines
drivers/regulator/ad5398.c new file +8 -0
+static const struct ad5398_current_data_format ad5398_df = {10, 4};
+static const struct ad5398_current_data_format ad5821_df = {10, 4};
+
+static const struct i2c_device_id ad5398_id[] = {
+       { "ad5398", (kernel_ulong_t)&ad5398_df },
+       { "ad5821", (kernel_ulong_t)&ad5821_df },
+       { }
+};

do you really need sep storage for these _df vars ?

Quoted 1 line

Yes, this makes probe code simpler.

how does it make any difference to the probe code what each id is pointing to ? it isnt comparing the private data pointers to any other storage pointers.

from what i can see, this should give the same exact behavior:
static const struct ad5398_current_data_format df_10_4 = {10, 4};

static const struct i2c_device_id ad5398_id[] = {
       { "ad5398", (kernel_ulong_t)&df_10_4 },
       { "ad5821", (kernel_ulong_t)&df_10_4 },

-mike

#4 lore raw
Quoted 45 lines · Mike Frysinger
Quoted 10 lines · Sonic Zhang
Quoted 9 lines · Mike Frysinger
Quoted 8 lines · sonic zhang

The AD5398 and AD5821 are single 10-bit DAC with 120 mA output current sink capability. They feature an internal reference and operates from a single 2.7 V to 5.5 V supply.

This driver supports both the AD5398 and the AD5821.  It adapts into the voltage and current framework.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

the "From:" doesnt match your s-o-b tag ...

Does this need to be matched? I prefer to discuss via gmail account while keep company email in the patch owner information.

so set the From: in the body as the first line like git-send-email does it:
From: <author info>

<commit message>

<other tags>
---
 <diffstat>
..........

Quoted 11 lines
Quoted 10 lines

drivers/regulator/ad5398.c line 206

Quoted 8 lines
drivers/regulator/ad5398.c new file +8 -0
+static const struct ad5398_current_data_format ad5398_df = {10, 4};
+static const struct ad5398_current_data_format ad5821_df = {10, 4};
+
+static const struct i2c_device_id ad5398_id[] = {
+       { "ad5398", (kernel_ulong_t)&ad5398_df },
+       { "ad5821", (kernel_ulong_t)&ad5821_df },
+       { }
+};

do you really need sep storage for these _df vars ?

Quoted 1 line

Yes, this makes probe code simpler.

how does it make any difference to the probe code what each id is pointing to ?  it isnt comparing the private data pointers to any other storage pointers.

Quoted 7 lines

from what i can see, this should give the same exact behavior:
static const struct ad5398_current_data_format df_10_4 = {10, 4}; static const struct i2c_device_id ad5398_id[] = {
      { "ad5398", (kernel_ulong_t)&df_10_4 },
      { "ad5821", (kernel_ulong_t)&df_10_4 },
-mike

Yes, the behavior is the same for ad5398 and ad5821. But, if more chips are enabled in this driver, they may differ.
This line is used as an example for future chips.

Sonic

#5 lore raw
Quoted 27 lines · Sonic Zhang
Quoted 20 lines · Mike Frysinger
Quoted 13 lines · Sonic Zhang
Quoted 11 lines · Mike Frysinger

drivers/regulator/ad5398.c line 206

Quoted 8 lines · sonic zhang
drivers/regulator/ad5398.c new file +8 -0
+static const struct ad5398_current_data_format ad5398_df = {10, 4};
+static const struct ad5398_current_data_format ad5821_df = {10, 4};
+
+static const struct i2c_device_id ad5398_id[] = {
+       { "ad5398", (kernel_ulong_t)&ad5398_df },
+       { "ad5821", (kernel_ulong_t)&ad5821_df },
+       { }
+};

do you really need sep storage for these _df vars ?

Quoted 1 line

Yes, this makes probe code simpler.

how does it make any difference to the probe code what each id is pointing to ?  it isnt comparing the private data pointers to any other storage pointers.

Quoted 5 lines

from what i can see, this should give the same exact behavior:
static const struct ad5398_current_data_format df_10_4 = {10, 4}; static const struct i2c_device_id ad5398_id[] = {
      { "ad5398", (kernel_ulong_t)&df_10_4 },
      { "ad5821", (kernel_ulong_t)&df_10_4 },

Quoted 3 lines

Yes, the behavior is the same for ad5398 and ad5821. But, if more chips are enabled in this driver, they may differ.
This line is used as an example for future chips.

seems like a weak reason for otherwise useless overhead. especially considering my simpler example should also be pretty obvious to extend for future drivers should the need arise. you're a smart guy and dont need things spelled out explicitly.
-mike

#6 lore raw
In reply to Mike Frysinger
Quoted 5 lines · Mike Frysinger
Quoted 1 line

+ dev_dbg(&client->dev, "%s regulator driver loaded\n", id->name);

does the regulator core take care of displaying a notice ? if not, i'd make this dev_info(). also, this should be "registered", not "loaded".

A message will be displayed if the device actually has any constraints (and is therefore in use) so if it makes any difference to the system there will be some chat.

Quoted 5 lines

drivers/regulator/ad5398.c line 285

Quoted 3 lines
drivers/regulator/ad5398.c new file +3 -0
+MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
+MODULE_AUTHOR("Sonic Zhang");
+MODULE_LICENSE("GPL");

should there be a MODULE_ALIAS() ?

Possibly, though it's unlikely that actual systems will ever build a regulator they're using as a module.

#7 lore raw
In reply to sonic zhang

drivers/regulator/ad5398.c line 48

Quoted 11 lines · sonic zhang
drivers/regulator/ad5398.c new file +11 -0
+static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
+{
+	unsigned short val;
+	int ret;
+
+	ret = i2c_master_recv(client, (char *)&val, 2);
+	if (ret < 0) {
+		dev_err(&client->dev, "I2C read error\n");
+		return ret;
+	}
+	*data = swab16(val);

Should this not be a be16_to_cpu() or similar rather than an unconditional byte swap? Presumably the byte swap is not going to be needed if the CPU has the same endianness as the CPU that the system is using.

drivers/regulator/ad5398.c line 111

Quoted 8 lines
drivers/regulator/ad5398.c new file +7 -0
+	/* read chip enable bit */
+	ret = ad5398_read_reg(client, &data);
+	if (ret < 0)
+		return ret;
 
+	/* prepare register data */
+	selector = (selector << chip->current_offset) & chip->current_mask;
+	selector |= (data & AD5398_CURRENT_EN_MASK);

The reason I was querying this code on the original submission is that it is more normal to write this as something like

    data = selector | (data & ~chip->current_mask);

ie, to write the code as "set these bits" rather than "preserve these bits". This is more clearly robust to the reader since it's clear that there aren't other register bits which should be set.

drivers/regulator/ad5398.c line 229

Quoted 2 lines
drivers/regulator/ad5398.c new file +2 -0
+       chip->min_uA = init_data->constraints.min_uA;
+       chip->max_uA = init_data->constraints.max_uA;

This looks very wrong, especially given that you use the min_uA and max_uA settings to scale the register values being written in to the chip. I would expect that either the limits would be fixed in the silicon or (if they depend on things like the associated passives which can be configured per-board) that there would be some other setting in the platform data which specifies what's actually being changed.

The constraints being specified by the platform should not influence the physical properties of the chip, they control which values are allowed in a particular design (for example, saying that values over a given limit are not allowed due to the limits of the hardware connected to the regulator) and are separate to what the chip is capable of.

#8 lore raw
Quoted 18 lines · Mark Brown

drivers/regulator/ad5398.c line 48

Quoted 11 lines · sonic zhang
drivers/regulator/ad5398.c new file +11 -0
+static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
+{
+     unsigned short val;
+     int ret;
+
+     ret = i2c_master_recv(client, (char *)&val, 2);
+     if (ret < 0) {
+             dev_err(&client->dev, "I2C read error\n");
+             return ret;
+     }
+     *data = swab16(val);

Should this not be a be16_to_cpu() or similar rather than an unconditional byte swap?  Presumably the byte swap is not going to be needed if the CPU has the same endianness as the CPU that the system is using.

I made a mistake to mix simple i2c transfer and smbus i2c transfer here. For smbus i2c transfer, byte swap is unconditional.

Quoted 10 lines
Quoted 9 lines

+     /* read chip enable bit */
+     ret = ad5398_read_reg(client, &data);
+     if (ret < 0)
+             return ret;

+     /* prepare register data */
+     selector = (selector << chip->current_offset) & chip->current_mask;
+     selector |= (data & AD5398_CURRENT_EN_MASK);

Quoted 8 lines

The reason I was querying this code on the original submission is that it is more normal to write this as something like

   data = selector | (data & ~chip->current_mask);

ie, to write the code as "set these bits" rather than "preserve these bits".  This is more clearly robust to the reader since it's clear that there aren't other register bits which should be set.

OK.

Quoted 11 lines
Quoted 3 lines

+       chip->min_uA = init_data->constraints.min_uA;
+       chip->max_uA = init_data->constraints.max_uA;

This looks very wrong, especially given that you use the min_uA and max_uA settings to scale the register values being written in to the chip.  I would expect that either the limits would be fixed in the silicon or (if they depend on things like the associated passives which can be configured per-board) that there would be some other setting in the platform data which specifies what's actually being changed.

Quoted 6 lines

The constraints being specified by the platform should not influence the physical properties of the chip, they control which values are allowed in a particular design (for example, saying that values over a given limit are not allowed due to the limits of the hardware connected to the regulator) and are separate to what the chip is capable of.

I will predefine the chip physical min max values in the driver and add user defined limitation based on both initial constraints and chip spec.

Sonic