w1: ds2433: introduce a configuration structure
authorMarc Ferland <marc.ferland@sonatest.com>
Mon, 18 Dec 2023 15:02:28 +0000 (10:02 -0500)
committerKrzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Wed, 20 Dec 2023 08:25:25 +0000 (09:25 +0100)
Add a ds2433_config structure for parameters that are different
between the ds2433 and the ds28ec20. The goal is to reuse the same
code for both chips.

A pointer to this config structure is added to w1_f23_data and the
CONFIG_W1_SLAVE_DS2433_CRC ifdefs are adjusted since now both driver
configurations (with or without crc support) will make use of
w1_f23_data.

Also, the 'memory' buffer is now dynamically allocated based on the
size specififed in the config structure to help support memories of
different sizes.

Signed-off-by: Marc Ferland <marc.ferland@sonatest.com>
Link: https://lore.kernel.org/r/20231218150230.1992448-4-marc.ferland@sonatest.com
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
drivers/w1/slaves/w1_ds2433.c

index cd99eceac1ae513b3c89f987275476d381531fb6..0c67082c8bb73796d468d8f025e8be6ddeb0b9f3 100644 (file)
 #define W1_F23_READ_SCRATCH    0xAA
 #define W1_F23_COPY_SCRATCH    0x55
 
+struct ds2433_config {
+       size_t eeprom_size;             /* eeprom size in bytes */
+       unsigned int page_count;        /* number of 256 bits pages */
+       unsigned int tprog;             /* time in ms for page programming */
+};
+
+static const struct ds2433_config config_f23 = {
+       .eeprom_size = W1_EEPROM_SIZE,
+       .page_count = 16,
+       .tprog = 5,
+};
+
 struct w1_f23_data {
-       u8      memory[W1_EEPROM_SIZE];
+#ifdef CONFIG_W1_SLAVE_DS2433_CRC
+       u8      *memory;
        u32     validcrc;
+#endif
+       const struct ds2433_config *cfg;
 };
 
 /*
@@ -95,7 +110,7 @@ static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
        u8 wrbuf[3];
 #endif
 
-       count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE);
+       count = w1_f23_fix_count(off, count, bin_attr->size);
        if (!count)
                return 0;
 
@@ -150,9 +165,7 @@ out_up:
  */
 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
 {
-#ifdef CONFIG_W1_SLAVE_DS2433_CRC
        struct w1_f23_data *f23 = sl->family_data;
-#endif
        u8 wrbuf[4];
        u8 rdbuf[W1_PAGE_SIZE + 3];
        u8 es = (addr + len - 1) & 0x1f;
@@ -188,8 +201,8 @@ static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
        wrbuf[3] = es;
        w1_write_block(sl->master, wrbuf, 4);
 
-       /* Sleep for 5 ms to wait for the write to complete */
-       msleep(5);
+       /* Sleep for tprog ms to wait for the write to complete */
+       msleep(f23->cfg->tprog);
 
        /* Reset the bus to wake up the EEPROM (this may not be needed) */
        w1_reset_bus(sl->master);
@@ -206,7 +219,7 @@ static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
        struct w1_slave *sl = kobj_to_w1_slave(kobj);
        int addr, len, idx;
 
-       count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE);
+       count = w1_f23_fix_count(off, count, bin_attr->size);
        if (!count)
                return 0;
 
@@ -268,24 +281,34 @@ static const struct attribute_group *w1_f23_groups[] = {
 
 static int w1_f23_add_slave(struct w1_slave *sl)
 {
-#ifdef CONFIG_W1_SLAVE_DS2433_CRC
        struct w1_f23_data *data;
 
        data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
        if (!data)
                return -ENOMEM;
+
+       data->cfg = &config_f23;
+
+#ifdef CONFIG_W1_SLAVE_DS2433_CRC
+       data->memory = kzalloc(data->cfg->eeprom_size, GFP_KERNEL);
+       if (!data->memory) {
+               kfree(data);
+               return -ENOMEM;
+       }
+#endif /* CONFIG_W1_SLAVE_DS2433_CRC */
        sl->family_data = data;
 
-#endif /* CONFIG_W1_SLAVE_DS2433_CRC */
        return 0;
 }
 
 static void w1_f23_remove_slave(struct w1_slave *sl)
 {
-#ifdef CONFIG_W1_SLAVE_DS2433_CRC
-       kfree(sl->family_data);
+       struct w1_f23_data *data = sl->family_data;
        sl->family_data = NULL;
-#endif /* CONFIG_W1_SLAVE_DS2433_CRC */
+#ifdef CONFIG_W1_SLAVE_DS2433_CRC
+       kfree(data->memory);
+#endif /* CONFIG_W1_SLAVE_DS2433_CRC */
+       kfree(data);
 }
 
 static const struct w1_family_ops w1_f23_fops = {