Skip to contentSkip to conversation

ASoC: Add a driver for the Iron Device SMA1303 Amp

PATCH In review Kiseok Jo 3 participants 6 messages Sep 21, 2022 – Nov 16, 2022

Subsystems (4)
lore
Share & tools
lore

Kiseok Jo

author maintainer
#0 lore raw

cover ASoC: Add a driver for the Iron Device SMA1303 Amp

This adds basic audio support for the Iron Device SMA1303 amplifier.

Kiseok Jo (2):
ASoC: sma1303: Add driver for Iron Device SMA1303 Amp
ASoC: dt-bindings: sma1303: Iron Device SMA1303 Amp

 .../bindings/sound/irondevice,sma1303.yaml    |   40 +
 .../devicetree/bindings/vendor-prefixes.yaml  |    2 +
 sound/soc/codecs/Kconfig                      |    7 +
 sound/soc/codecs/Makefile                     |    2 +
 sound/soc/codecs/sma1303.c                    | 2119 +++++++++++++++++
 sound/soc/codecs/sma1303.h                    |  609 +++++
 6 files changed, 2779 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/irondevice,sma1303.yaml
 create mode 100644 sound/soc/codecs/sma1303.c
 create mode 100644 sound/soc/codecs/sma1303.h
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/2 ASoC: sma1303: Add driver for Iron Device SMA1303 Amp

The Iron Device SMA1303 is a boosted Class-D audio amplifier.

---
 sound/soc/codecs/Kconfig   |    7 +
 sound/soc/codecs/Makefile  |    2 +
 sound/soc/codecs/sma1303.c | 2119 ++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/sma1303.h |  609 +++++++++++
 4 files changed, 2737 insertions(+)
 create mode 100644 sound/soc/codecs/sma1303.c
 create mode 100644 sound/soc/codecs/sma1303.h
sound/soc/codecs/Kconfig +7 -0
index 444cee829a26..79c28d663509 100644
@@ -205,6 +205,7 @@ config SND_SOC_ALL_CODECS
 	imply SND_SOC_SI476X
 	imply SND_SOC_SIMPLE_AMPLIFIER
 	imply SND_SOC_SIMPLE_MUX
+	imply SND_SOC_SMA1303
 	imply SND_SOC_SPDIF
 	imply SND_SOC_SRC4XXX_I2C
 	imply SND_SOC_SSM2305
@@ -1486,6 +1487,12 @@ config SND_SOC_SIMPLE_MUX
 	tristate "Simple Audio Mux"
 	depends on GPIOLIB
 
+config SND_SOC_SMA1303
+	tristate "Iron Device SMA1303 Audio Amplifier"
+	depends on I2C
+	help
+	  Enable support for Iron Device SMA1303 Boosted Class-D amplifier
+
 config SND_SOC_SPDIF
 	tristate "S/PDIF CODEC"
 
sound/soc/codecs/Makefile +2 -0
index 9170ee1447dd..e185b8669e79 100644
@@ -232,6 +232,7 @@ snd-soc-sigmadsp-objs := sigmadsp.o
 snd-soc-sigmadsp-i2c-objs := sigmadsp-i2c.o
 snd-soc-sigmadsp-regmap-objs := sigmadsp-regmap.o
 snd-soc-si476x-objs := si476x.o
+snd-soc-sma1303-objs := sma1303.o
 snd-soc-spdif-tx-objs := spdif_transmitter.o
 snd-soc-spdif-rx-objs := spdif_receiver.o
 snd-soc-src4xxx-objs := src4xxx.o
@@ -586,6 +587,7 @@ obj-$(CONFIG_SND_SOC_SIGMADSP)	+= snd-soc-sigmadsp.o
 obj-$(CONFIG_SND_SOC_SIGMADSP_I2C)	+= snd-soc-sigmadsp-i2c.o
 obj-$(CONFIG_SND_SOC_SIGMADSP_REGMAP)	+= snd-soc-sigmadsp-regmap.o
 obj-$(CONFIG_SND_SOC_SI476X)	+= snd-soc-si476x.o
+obj-$(CONFIG_SND_SOC_SMA1303)	+= snd-soc-sma1303.o
 obj-$(CONFIG_SND_SOC_SPDIF)	+= snd-soc-spdif-rx.o snd-soc-spdif-tx.o
 obj-$(CONFIG_SND_SOC_SRC4XXX)	+= snd-soc-src4xxx.o
 obj-$(CONFIG_SND_SOC_SRC4XXX_I2C)	+= snd-soc-src4xxx-i2c.o
sound/soc/codecs/sma1303.c new file +2119 -0

2,119 lines not on this page · raw message

sound/soc/codecs/sma1303.h new file +609 -0

609 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] ASoC: sma1303: Add driver for Iron Device SMA1303 Amp

sound/soc/codecs/sma1303.c line 4

Quoted 13 lines
sound/soc/codecs/sma1303.c new file +4 -0
new file mode 100644
index 000000000000..03c969a4891e
@@ -0,0 +1,2119 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* sma1303.c -- sma1303 ALSA SoC Audio driver
+ *
+ * Copyright 2022 Iron Device Corporation

Missing Copyright (c) ?

sound/soc/codecs/sma1303.c line 8

Quoted 4 lines
sound/soc/codecs/sma1303.c new file +4 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.

this is redundant with the SPDX line above.

sound/soc/codecs/sma1303.c line 15

Quoted 7 lines
sound/soc/codecs/sma1303.c new file +7 -0
+ */
+
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/delay.h>

it's recommanded to list headers by alphabetical order.

Quoted 9 lines
sound/soc/codecs/sma1303.c new file +9 -0
+#include <linux/pm.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+#include <linux/version.h>
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/initval.h>

sound/soc/codecs/sma1303.c line 48

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +9 -0
+#include <sound/tlv.h>
+#include <linux/of_device.h>
+#include <linux/slab.h>
+#include <asm/div64.h>
+
+#include "sma1303.h"
 
+enum sma1303_type {
+	SMA1303,
+};

is this necessary?

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+
+struct sma1303_pll_match {
+	char *input_clk_name;
+	char *output_clk_name;
+	unsigned int input_clk;
+	unsigned int post_n;

sound/soc/codecs/sma1303.c line 64

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+	unsigned int n;
+	unsigned int vco;
+	unsigned int p_cp;
+};
+
+struct sma1303_priv {
+	enum sma1303_type devtype;
+	struct device *dev;
+	struct attribute_group *attr_grp;
+	struct kobject *kobj;

Usually it's not recommended to muck with kobj in drivers. If this is for sysfs support there are better and safer ways.

Quoted 23 lines
sound/soc/codecs/sma1303.c new file +23 -0
+	struct regmap *regmap;
+	struct sma1303_pll_match *pll_matches;
+	struct mutex lock;
+	struct delayed_work check_fault_work;
+	bool amp_power_status;
+	bool usage_status;
+	int num_of_pll_matches;
+	unsigned int sys_clk_id;
+	unsigned int init_vol;
+	unsigned int cur_vol;
+	unsigned int tdm_slot_rx;
+	unsigned int tdm_slot_tx;
+	unsigned int tsdw_cnt;
+	unsigned int format;
+	unsigned int rev_num;
+	unsigned int last_over_temp;
+	unsigned int last_ocp_val;
+	unsigned int last_bclk;
+	unsigned int frame_size;
+	unsigned int amp_mode;
+	long check_fault_period;
+	long check_fault_status;
+};

sound/soc/codecs/sma1303.c line 97

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+
+static struct sma1303_pll_match sma1303_pll_matches[] = {
+PLL_MATCH("1.411MHz",  "24.595MHz", 1411200,  0x07, 0xF4, 0x8B, 0x03),
+PLL_MATCH("1.536MHz",  "24.576MHz", 1536000,  0x07, 0xE0, 0x8B, 0x03),
+PLL_MATCH("3.072MHz",  "24.576MHz", 3072000,  0x07, 0x70, 0x8B, 0x03),
+PLL_MATCH("6.144MHz",  "24.576MHz", 6144000,  0x07, 0x70, 0x8B, 0x07),
+PLL_MATCH("12.288MHz", "24.576MHz", 12288000, 0x07, 0x70, 0x8B, 0x0B),
+PLL_MATCH("19.2MHz",   "24.343MHz", 19200000, 0x07, 0x47, 0x8B, 0x0A),
+PLL_MATCH("24.576MHz", "24.576MHz", 24576000, 0x07, 0x70, 0x8B, 0x0F),
+};

Any reason to use strings instead of actual integer values for frequencies?

sound/soc/codecs/sma1303.c line 239

Quoted 4 lines
sound/soc/codecs/sma1303.c new file +4 -0
+static int sma1303_regmap_write(struct regmap *map, struct device *dev,
+				unsigned int reg, unsigned int val)
+{
+	int ret = 0;

unnecessary init, same comment for all the code.

Quoted 7 lines
sound/soc/codecs/sma1303.c new file +7 -0
+
+	ret = regmap_write(map, reg, val);
+	if (ret < 0) {
+		dev_err(dev, "Failed to write, register: 0x%02X, ret: %d\n",
+			       reg, ret);
+	}
+	return ret;

sound/soc/codecs/sma1303.c line 256

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+}
+
+static int sma1303_regmap_update_bits(struct regmap *map, struct device *dev,
+		unsigned int reg, unsigned int mask, unsigned int val)
+{
+	int ret = 0;
+
+	ret = regmap_update_bits(map, reg, mask, val);
+	if (ret < 0) {
+		dev_err(dev, "Failed to write, register: 0x%02X, ret: %d\n",

Failed to update?

Quoted 7 lines
sound/soc/codecs/sma1303.c new file +7 -0
+			       reg, ret);
+	}
+	return ret;
+}
+
+static int bytes_ext_get(struct snd_kcontrol *kcontrol,
+			struct snd_ctl_elem_value *ucontrol, int reg)

sound/soc/codecs/sma1303.c line 273

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
+	unsigned int i, reg_val;
+	u8 *val;
+	int ret = -1;
+
+	if (component == NULL) {

if (!component)

sound/soc/codecs/sma1303.c

Quoted 1 line
sound/soc/codecs/sma1303.c new file +1 -0
+		pr_err("%s:component is NULL\n", __func__);

can pr_err be avoided?

sound/soc/codecs/sma1303.c line 277

Quoted 3 lines
sound/soc/codecs/sma1303.c new file +3 -0
+		return ret;
+	}
+	if (sma1303 == NULL) {

if (!sma1303)

sound/soc/codecs/sma1303.c

Quoted 1 line
sound/soc/codecs/sma1303.c new file +1 -0
+		pr_err("%s:sma1303 is NULL\n", __func__);

can pr_err be avoided?

Same comments for all functions using this pattern.

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+		return ret;
+	}
+	val = (u8 *)ucontrol->value.bytes.data;
+	for (i = 0; i < params->max; i++) {
+		ret = regmap_read(sma1303->regmap, reg + i, &reg_val);
+		if (ret < 0) {

sound/soc/codecs/sma1303.c line 294

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+			dev_err(component->dev,
+				"Failed to read, register: %x ret: %d\n",
+				reg + i, ret);
+			return ret;
+		}
+		if (sizeof(reg_val) > 2)
+			reg_val = cpu_to_le32(reg_val);
+		else
+			reg_val = cpu_to_le16(reg_val);
+		memcpy(val + i, &reg_val, sizeof(u8));

I wasn't able to figure out what this code does. sizeof(reg_val) is a constant so the second branch is never taken, and you end-up using memcpy to copy one byte, so what is the issue with endianness?

Quoted 32 lines
sound/soc/codecs/sma1303.c new file +32 -0
+	}
+
+	return 0;
+}
+
+static int bytes_ext_put(struct snd_kcontrol *kcontrol,
+			struct snd_ctl_elem_value *ucontrol, int reg)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	struct soc_bytes_ext *params = (void *)kcontrol->private_value;
+	void *data;
+	u8 *val;
+	int i, ret = -1;
+
+	if (component == NULL) {
+		pr_err("%s:component is NULL\n", __func__);
+		return ret;
+	}
+	if (sma1303 == NULL) {
+		pr_err("%s:sma1303 is NULL\n", __func__);
+		return ret;
+	}
+	data = kmemdup(ucontrol->value.bytes.data,
+			params->max, GFP_KERNEL | GFP_DMA);
+	if (!data)
+		return -ENOMEM;
+
+	val = (u8 *)data;
+	for (i = 0; i < params->max; i++) {
+		ret = sma1303_regmap_write(

sound/soc/codecs/sma1303.c line 336

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+				sma1303->regmap, component->dev,
+				reg + i, *(val + i));
+		if (ret < 0) {
+			kfree(data);
+			return ret;
+		}
+	}
+	kfree(data);
+
+	return 0;

ret = 0;

for (i = 0; i < params->max; i++) {
	ret = sma1303_regmap_write(
			sma1303->regmap, component->dev,
			reg + i, *(val + i));
	if (ret < 0)
		break;
}

kfree(data);
return ret;

Quoted 152 lines
sound/soc/codecs/sma1303.c new file +152 -0
+static int amp_usage_status_get(struct snd_kcontrol *kcontrol,
+		struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	int ret = -1;
+
+	if (component == NULL) {
+		pr_err("%s:component is NULL\n", __func__);
+		return ret;
+	}
+	if (sma1303 == NULL) {
+		pr_err("%s:sma1303 is NULL\n", __func__);
+		return ret;
+	}
+	ucontrol->value.integer.value[0] = sma1303->usage_status;
+
+	if (sma1303->usage_status)
+		dev_info(component->dev, "Amplifier Power Control Enabled\n");
+	else
+		dev_info(component->dev, "Amplifier Power Control Disabled\n");
+
+	return 0;
+}
+
+static int amp_usage_status_put(struct snd_kcontrol *kcontrol,
+		struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	int ret = -1;
+
+	if (component == NULL) {
+		pr_err("%s:component is NULL\n", __func__);
+		return ret;
+	}
+	if (sma1303 == NULL) {
+		pr_err("%s:sma1303 is NULL\n", __func__);
+		return ret;
+	}
+
+	if ((sma1303->usage_status
+			!= ucontrol->value.integer.value[0])
+			&& (!ucontrol->value.integer.value[0])) {
+		dev_info(component->dev, "%s\n", "Force AMP Power Down");
+		ret = sma1303_shutdown(component);
+		if (ret < 0) {
+			ucontrol->value.integer.value[0]
+			       = sma1303->usage_status;
+			return ret;
+		}
+
+	}
+	sma1303->usage_status = ucontrol->value.integer.value[0];
+
+	return 0;
+}
+
+static const char * const sma1303_amp_mode_text[] = {
+	"1 Chip", "Mono on 2 chips", "Left in 2 chips", "Right in 2chips"};
+
+static const struct soc_enum sma1303_amp_mode_enum =
+	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(sma1303_amp_mode_text),
+			sma1303_amp_mode_text);
+static int sma1303_amp_mode_get(struct snd_kcontrol *kcontrol,
+				struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	int ret = -1;
+
+	if (component == NULL) {
+		pr_err("%s:component is NULL\n", __func__);
+		return ret;
+	}
+	if (sma1303 == NULL) {
+		pr_err("%s:sma1303 is NULL\n", __func__);
+		return ret;
+	}
+	ucontrol->value.integer.value[0] = sma1303->amp_mode;
+
+	switch (sma1303->amp_mode) {
+	case ONE_CHIP_SOLUTION:
+		dev_info(component->dev, "Amplifier 1 Chip Solution\n");
+		break;
+	case MONO_TWO_CHIP_SOLUTION:
+		dev_info(component->dev, "Amplifier Mono 2 Chips Solution\n");
+		break;
+	case LEFT_TWO_CHIP_SOLUTION:
+		dev_info(component->dev, "Amplifier Stereo(Left) 2 Chips Solution\n");
+		break;
+	case RIGHT_TWO_CHIP_SOLUTION:
+		dev_info(component->dev, "Amplifier Stereo(Right) 2 Chips Solution\n");
+		break;
+	default:
+		dev_err(component->dev, "Invalid Value");
+		return ret;
+	}
+	return 0;
+
+}
+
+static int sma1303_amp_mode_put(struct snd_kcontrol *kcontrol,
+		struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
+	int ret = 0;
+
+	if (component == NULL) {
+		pr_err("%s:component is NULL\n", __func__);
+		return -1;
+	}
+	if (sma1303 == NULL) {
+		pr_err("%s:sma1303 is NULL\n", __func__);
+		return -1;
+	}
+
+	sma1303->amp_mode = ucontrol->value.integer.value[0];
+
+	switch (sma1303->amp_mode) {
+	case ONE_CHIP_SOLUTION:
+	case MONO_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_ON);
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_NORMAL);
+		break;
+	case LEFT_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_OFF);
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_NORMAL);
+		break;
+	case RIGHT_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_OFF);
+		ret += sma1303_regmap_update_bits(

sound/soc/codecs/sma1303.c line 500

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_SWAP);
+		break;
+	default:
+		dev_err(component->dev, "Invalid Value");
+		ret += -1;
+	}
+
+	return ret;

Not sure I understand your arithmetic on combining error codes.

If one transaction fails, is there any point in trying another regmap_update_bits()?

{skipping all the way to the probe which has a lot of issues}

Quoted 15 lines
sound/soc/codecs/sma1303.c new file +15 -0
+static int sma1303_i2c_probe(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct sma1303_priv *sma1303;
+	struct device_node *np = client->dev.of_node;
+	int ret;
+	u32 value;
+	unsigned int device_info;
+
+	dev_info(&client->dev, "%s is here. Driver version REV018\n", __func__);
+
+	sma1303 = devm_kzalloc(&client->dev, sizeof(struct sma1303_priv),
+							GFP_KERNEL);
+
+	if (!sma1303)

sound/soc/codecs/sma1303.c line 1946

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+		return -ENOMEM;
+
+	sma1303->regmap = devm_regmap_init_i2c(client, &sma_i2c_regmap);
+
+	if (IS_ERR(sma1303->regmap)) {
+		ret = PTR_ERR(sma1303->regmap);
+		dev_err(&client->dev,
+			"Failed to allocate register map: %d\n", ret);
+
+		devm_kfree(&client->dev, sma1303);

why use devm_ then?

Quoted 83 lines
sound/soc/codecs/sma1303.c new file +83 -0
+
+		return ret;
+	}
+
+	if (np) {
+		if (!of_property_read_u32(np, "tdm-slot-rx", &value)) {
+			dev_info(&client->dev,
+				"tdm slot rx is '%d' from DT\n", value);
+			sma1303->tdm_slot_rx = value;
+		} else {
+			dev_info(&client->dev,
+				"Default setting of tdm slot rx is '0'\n");
+			sma1303->tdm_slot_rx = 0;
+		}
+		if (!of_property_read_u32(np, "tdm-slot-tx", &value)) {
+			dev_info(&client->dev,
+				"tdm slot tx is '%d' from DT\n", value);
+			sma1303->tdm_slot_tx = value;
+		} else {
+			dev_info(&client->dev,
+				"Default setting of tdm slot tx is '0'\n");
+			sma1303->tdm_slot_tx = 0;
+		}
+		if (!of_property_read_u32(np, "sys-clk-id", &value)) {
+			switch (value) {
+			case SMA1303_EXTERNAL_CLOCK_19_2:
+			case SMA1303_EXTERNAL_CLOCK_24_576:
+			case SMA1303_PLL_CLKIN_MCLK:
+				dev_info(&client->dev, "MCLK is not supported\n");
+				break;
+			case SMA1303_PLL_CLKIN_BCLK:
+				dev_info(&client->dev,
+				"Take an BCLK(SCK) and covert it to an internal PLL for use\n");
+				break;
+			default:
+				dev_err(&client->dev,
+					"Invalid sys-clk-id: %d\n", value);
+				return -EINVAL;
+			}
+			sma1303->sys_clk_id = value;
+		} else {
+			dev_info(&client->dev, "Use the internal PLL clock by default\n");
+			sma1303->sys_clk_id = SMA1303_PLL_CLKIN_BCLK;
+		}
+	} else {
+		dev_err(&client->dev,
+			"device node initialization error\n");
+		devm_kfree(&client->dev, sma1303);
+		return -ENODEV;
+	}
+
+	ret = regmap_read(sma1303->regmap,
+		SMA1303_FF_DEVICE_INDEX, &device_info);
+
+	if ((ret != 0) || ((device_info & 0xF8) != DEVICE_ID)) {
+		dev_err(&client->dev, "device initialization error (%d 0x%02X)",
+				ret, device_info);
+	}
+	dev_info(&client->dev, "chip version 0x%02X\n", device_info);
+
+	sma1303->last_over_temp = 0xC0;
+	sma1303->last_ocp_val = 0x0A;
+	sma1303->tsdw_cnt = 0;
+	sma1303->init_vol = 0x31;
+	sma1303->cur_vol = sma1303->init_vol;
+	sma1303->last_bclk = 0;
+
+	INIT_DELAYED_WORK(&sma1303->check_fault_work,
+		sma1303_check_fault_worker);
+
+	mutex_init(&sma1303->lock);
+	sma1303->check_fault_period = CHECK_PERIOD_TIME;
+
+	sma1303->devtype = id->driver_data;
+	sma1303->dev = &client->dev;
+	sma1303->kobj = &client->dev.kobj;
+
+	i2c_set_clientdata(client, sma1303);
+
+	sma1303->amp_mode = ONE_CHIP_SOLUTION;
+	sma1303->usage_status = true;
+	sma1303->amp_power_status = false;
+	sma1303->check_fault_status = true;

sound/soc/codecs/sma1303.c line 2039

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+	sma1303->pll_matches = sma1303_pll_matches;
+	sma1303->num_of_pll_matches =
+		ARRAY_SIZE(sma1303_pll_matches);
+
+	ret = devm_snd_soc_register_component(&client->dev,
+			&sma1303_component, sma1303_dai, 1);
+
+	if (ret) {
+		dev_err(&client->dev, "Failed to register component");
+		snd_soc_unregister_component(&client->dev);

no, this is not needed if you use devm_

Quoted 5 lines
sound/soc/codecs/sma1303.c new file +5 -0
+
+		if (sma1303)
+			devm_kfree(&client->dev, sma1303);
+
+		return ret;

sound/soc/codecs/sma1303.c line 2054

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+	}
+
+	sma1303->attr_grp = &sma1303_attr_group;
+	ret = sysfs_create_group(sma1303->kobj, sma1303->attr_grp);
+
+	if (ret) {
+		dev_err(&client->dev,
+			"failed to create attribute group [%d]\n", ret);
+		sma1303->attr_grp = NULL;
+	}

not clear what you are trying to do with sysfs?

sound/soc/codecs/sma1303.c line 2066

Quoted 12 lines
sound/soc/codecs/sma1303.c new file +12 -0
+
+	return ret;
+}
+
+static int sma1303_i2c_remove(struct i2c_client *client)
+{
+	struct sma1303_priv *sma1303 =
+		(struct sma1303_priv *) i2c_get_clientdata(client);
+
+	cancel_delayed_work_sync(&sma1303->check_fault_work);
+
+	snd_soc_unregister_component(&client->dev);

not needed when you use devm_snd_soc_register_component ?

sound/soc/codecs/sma1303.c line 2042

Quoted 3 lines
sound/soc/codecs/sma1303.c new file +3 -0
+
+	if (sma1303)
+		devm_kfree(&client->dev, sma1303);

is this needed? if you use devm_ the memory will be released when the remove() completes.

Quoted 35 lines
sound/soc/codecs/sma1303.c new file +35 -0
+
+	return 0;
+}
+
+static const struct i2c_device_id sma1303_i2c_id[] = {
+	{"sma1303", 0},
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, sma1303_i2c_id);
+
+static const struct of_device_id sma1303_of_match[] = {
+	{ .compatible = "irondevice,sma1303", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sma1303_of_match);
+
+static struct i2c_driver sma1303_i2c_driver = {
+	.driver = {
+		.name = "sma1303",
+		.of_match_table = sma1303_of_match,
+	},
+	.probe = sma1303_i2c_probe,
+	.remove = sma1303_i2c_remove,
+	.id_table = sma1303_i2c_id,
+};
+
+static int __init sma1303_init(void)
+{
+	int ret;
+
+	ret = i2c_add_driver(&sma1303_i2c_driver);
+
+	if (ret)
+		pr_err("Failed to register sma1303 I2C driver: %d\n", ret);
+

sound/soc/codecs/sma1303.c line 2114

Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+	return ret;
+}
+
+static void __exit sma1303_exit(void)
+{
+	i2c_del_driver(&sma1303_i2c_driver);
+}
+
+module_init(sma1303_init);
+module_exit(sma1303_exit);

use module_i2c_driver() ?

sound/soc/codecs/sma1303.c line 2119

Quoted 5 lines
sound/soc/codecs/sma1303.c new file +5 -0
+
+MODULE_DESCRIPTION("ALSA SoC SMA1303 driver");
+MODULE_AUTHOR("Gyuhwa Park, <[email protected]>");
+MODULE_AUTHOR("Kiseok Jo, <[email protected]>");
+MODULE_LICENSE("GPL v2");

"GPL" is equivalent.

Quoted 6 lines
sound/soc/codecs/sma1303.h new file +0 -0
new file mode 100644
index 000000000000..d1fa2acaba85

sound/soc/codecs/sma1303.h line 8

Quoted 8 lines
sound/soc/codecs/sma1303.h new file +8 -0
@@ -0,0 +1,609 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * sma1303.h -- sma1303 ALSA SoC Audio driver
+ *
+ * Copyright 2022 Iron Device Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.

same, this paragraph is not needed

sound/soc/codecs/sma1303.h line 106

Quoted 2 lines
sound/soc/codecs/sma1303.h new file +2 -0
+
+/* SMA1303 Registers Bit Fields */

use prefixes for all definitions below?

Quoted 10 lines
sound/soc/codecs/sma1303.h new file +9 -0
+/* SYSTEM_CTRL : 0x00 */
+#define RESETBYI2C_MASK (1<<1)
+#define RESETBYI2C_NORMAL (0<<1)
+#define RESETBYI2C_RESET (1<<1)
+
+#define POWER_MASK (1<<0)
+#define POWER_OFF (0<<0)
+#define POWER_ON (1<<0)
+
author maintainer
#3 lore raw

RE: [PATCH 1/2] ASoC: sma1303: Add driver for Iron Device SMA1303 Amp

Thanks your kindly feedback.

I'm correcting the part I missed.
I'd like to take a few hints.
(I revised the other parts that I didn't mention as you told me.)

Quoted 3 lines
Quoted 1 line

+ * Copyright 2022 Iron Device Corporation

Missing Copyright (c) ?

I don't know this part exactly, so when I looked it up.
Most of them use a mix of two cases. Which would be better? Using Symbol or not?

Quoted 8 lines

sound/soc/codecs/sma1303.c line 64

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+
+struct sma1303_priv {
+	enum sma1303_type devtype;
+	struct device *dev;
+	struct attribute_group *attr_grp;
+	struct kobject *kobj;

Usually it's not recommended to muck with kobj in drivers. If this is for sysfs support there are better and safer ways.

Could you tell me which method you usually use?
I used 'sysfs_create_group' with .

Quoted 11 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+static struct sma1303_pll_match sma1303_pll_matches[] = { 
+PLL_MATCH("1.411MHz",  "24.595MHz", 1411200,  0x07, 0xF4, 0x8B, 
+0x03), PLL_MATCH("1.536MHz",  "24.576MHz", 1536000,  0x07, 0xE0, 
+0x8B, 0x03), PLL_MATCH("3.072MHz",  "24.576MHz", 3072000,  0x07, 
+0x70, 0x8B, 0x03), PLL_MATCH("6.144MHz",  "24.576MHz", 6144000,  
+0x07, 0x70, 0x8B, 0x07), PLL_MATCH("12.288MHz", "24.576MHz", 12288000, 0x07, 0x70, 0x8B, 0x0B),
+PLL_MATCH("19.2MHz",   "24.343MHz", 19200000, 0x07, 0x47, 0x8B, 0x0A),
+PLL_MATCH("24.576MHz", "24.576MHz", 24576000, 0x07, 0x70, 0x8B, 
+0x0F), };

Any reason to use strings instead of actual integer values for frequencies?

I added the string to put it simply for the log.

Quoted 10 lines
Quoted 10 lines
sound/soc/codecs/sma1303.c new file +10 -0
+		return ret;
+	}
+	val = (u8 *)ucontrol->value.bytes.data;
+	for (i = 0; i < params->max; i++) {
+		ret = regmap_read(sma1303->regmap, reg + i, &reg_val);
+		if (ret < 0) {
+			dev_err(component->dev,
+				"Failed to read, register: %x ret: %d\n",
+				reg + i, ret);
+			return ret;
Quoted 8 lines

sound/soc/codecs/sma1303.c line 294

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+		}
+		if (sizeof(reg_val) > 2)
+			reg_val = cpu_to_le32(reg_val);
+		else
+			reg_val = cpu_to_le16(reg_val);
+		memcpy(val + i, &reg_val, sizeof(u8));

I wasn't able to figure out what this code does. sizeof(reg_val) is a constant so the second branch is never taken, and you end-up using memcpy to copy one byte, so what is the issue with endianness?

I'm sorry I don't understand this meaning.
In 'regmap_read', the last of the parameters is 'unsigned int' format.
So, I've considered the two format 2bytes or 4bytes according to the complier.
And our chip has only 1 byte data of each register, so I copy the data and cast the size only one byte. Is there anything I thought wrong?

Quoted 34 lines
Quoted 34 lines
sound/soc/codecs/sma1303.c new file +34 -0
+	switch (sma1303->amp_mode) {
+	case ONE_CHIP_SOLUTION:
+	case MONO_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_ON);
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_NORMAL);
+		break;
+	case LEFT_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_OFF);
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_NORMAL);
+		break;
+	case RIGHT_TWO_CHIP_SOLUTION:
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				MONOMIX_MASK, MONOMIX_OFF);
+		ret += sma1303_regmap_update_bits(
+				sma1303->regmap, component->dev,
+				SMA1303_11_SYSTEM_CTRL2,
+				LR_DATA_SW_MASK, LR_DATA_SW_SWAP);
+		break;
+	default:
+		dev_err(component->dev, "Invalid Value");
Quoted 8 lines

sound/soc/codecs/sma1303.c line 500

Quoted 4 lines
sound/soc/codecs/sma1303.c new file +4 -0
+		ret += -1;
+	}
+
+	return ret;

Not sure I understand your arithmetic on combining error codes.
If one transaction fails, is there any point in trying another regmap_update_bits()? {skipping all the way to the probe which has a lot of issues}

I wanted to continue even if it failed in one part. The reason is to check whether it is all a problem or only that part. If there is a problem with each part, it is set to leave a log for each part. So the error code was combined without initializing. If I do this, it will be different from the kernel standard error code, so I would like to ask if I can do this. Or is there a better way?

Quoted 11 lines

sound/soc/codecs/sma1303.c line 2054

Quoted 9 lines
sound/soc/codecs/sma1303.c new file +9 -0
+
+	sma1303->attr_grp = &sma1303_attr_group;
+	ret = sysfs_create_group(sma1303->kobj, sma1303->attr_grp);
+
+	if (ret) {
+		dev_err(&client->dev,
+			"failed to create attribute group [%d]\n", ret);
+		sma1303->attr_grp = NULL;
+	}

not clear what you are trying to do with sysfs?

Using work queue, check the fault status on polling.

Quoted 35 lines
Quoted 35 lines
sound/soc/codecs/sma1303.c new file +35 -0
+
+static const struct i2c_device_id sma1303_i2c_id[] = {
+	{"sma1303", 0},
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, sma1303_i2c_id);
+
+static const struct of_device_id sma1303_of_match[] = {
+	{ .compatible = "irondevice,sma1303", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, sma1303_of_match);
+
+static struct i2c_driver sma1303_i2c_driver = {
+	.driver = {
+		.name = "sma1303",
+		.of_match_table = sma1303_of_match,
+	},
+	.probe = sma1303_i2c_probe,
+	.remove = sma1303_i2c_remove,
+	.id_table = sma1303_i2c_id,
+};
+
+static int __init sma1303_init(void)
+{
+	int ret;
+
+	ret = i2c_add_driver(&sma1303_i2c_driver);
+
+	if (ret)
+		pr_err("Failed to register sma1303 I2C driver: %d\n", ret);
+
+	return ret;
+}
+
Quoted 8 lines

sound/soc/codecs/sma1303.c line 2114

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+static void __exit sma1303_exit(void) {
+	i2c_del_driver(&sma1303_i2c_driver);
+}
+
+module_init(sma1303_init);
+module_exit(sma1303_exit);

use module_i2c_driver() ?

It's the better. I'll modify.
Is there a problem if I make a module and register it with the i2c driver like above?

maintainer
#4 lore raw

Re: [PATCH 1/2] ASoC: sma1303: Add driver for Iron Device SMA1303 Amp

Quoted 6 lines · Ki-Seok Jo
Quoted 3 lines
Quoted 1 line

+ * Copyright 2022 Iron Device Corporation

Missing Copyright (c) ?

I don't know this part exactly, so when I looked it up.
Most of them use a mix of two cases. Which would be better? Using Symbol or not?

The (c) is meaningless, what you have is fine.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
sound/soc/codecs/sma1303.c new file +8 -0
+	val = (u8 *)ucontrol->value.bytes.data;
+	for (i = 0; i < params->max; i++) {
+		ret = regmap_read(sma1303->regmap, reg + i, &reg_val);
+		if (ret < 0) {
+			dev_err(component->dev,
+				"Failed to read, register: %x ret: %d\n",
+				reg + i, ret);
+			return ret;
Quoted 8 lines

sound/soc/codecs/sma1303.c line 294

Quoted 6 lines
sound/soc/codecs/sma1303.c new file +6 -0
+		}
+		if (sizeof(reg_val) > 2)
+			reg_val = cpu_to_le32(reg_val);
+		else
+			reg_val = cpu_to_le16(reg_val);
+		memcpy(val + i, &reg_val, sizeof(u8));

I wasn't able to figure out what this code does. sizeof(reg_val) is a constant so the second branch is never taken, and you end-up using memcpy to copy one byte, so what is the issue with endianness?

Quoted 5 lines

I'm sorry I don't understand this meaning.
In 'regmap_read', the last of the parameters is 'unsigned int' format.
So, I've considered the two format 2bytes or 4bytes according to the complier.
And our chip has only 1 byte data of each register, so I copy the data and cast the size only one byte. Is there anything I thought wrong?

You might be looking for the regmap _raw interfaces if you're trying to send raw byte streams to the device (eg, for firmware or parameter load).

author maintainer
#5 lore raw

PATCH 2/2 ASoC: dt-bindings: sma1303: Iron Device SMA1303 Amp

In reply to Kiseok Jo

This adds the schema binding for the Iron Device SMA1303 amplifier.

---
 .../bindings/sound/irondevice,sma1303.yaml    | 40 +++++++++++++++++++
 .../devicetree/bindings/vendor-prefixes.yaml  |  2 +
 2 files changed, 42 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/irondevice,sma1303.yaml
Documentation/devicetree/bindings/sound/irondevice,sma1303.yaml new file +40 -0
new file mode 100644
index 000000000000..8c3b85f962ff
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/irondevice,sma1303.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Iron Device SMA1303 Audio Amplifier
+
+maintainers:
+  - Kiseok Jo <[email protected]>
+
+description:
+  SMA1303 digital class-D audio amplifier with an integrated boost converter.
+
+allOf:
+  - $ref: name-prefix.yaml#
+
+properties:
+  compatible:
+    const: irondevice,sma1303
+
+  reg:
+    description:
+      I2C address of the amplifier. See the datasheet for possible values.
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: true
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    i2c_bus {
+		sma1303_amp: sma1303@1e {
+			compatible = "irondevice,sma1303";
+			reg = <0x1e>;
+		};
+	};
\ No newline at end of file
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 2f0151e9f6be..00ade1489f62 100644
@@ -621,6 +621,8 @@ patternProperties:
     description: Inverse Path
   "^iom,.*":
     description: Iomega Corporation
+  "^irondevice,.*":
+    description: Iron Device Corporation
   "^isee,.*":
     description: ISEE 2007 S.L.
   "^isil,.*":
Signature · 1 line
2.30.2