Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: ds2404: Convert to GPIO descriptors

This converts the DS2404 to use GPIO descriptors instead of
hard-coded global GPIO numbers.

The platform data can be deleted because there are no in-tree
users and it only contained GPIO numbers which are now
passed using descriptor tables (or device tree or ACPI).

The driver was rewritten to use a state container for the
device driver state (struct ds2404 *chip) and pass that
around instead of using a global singleton storage for the
GPIO handles.

When declaring GPIO descriptor tables or other hardware
descriptions for the RTC driver, implementers should take care
to flag the RESET line as active low, such as by using the
GPIOD_ACTIVE_LOW flag in the descriptor table.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20230807-descriptors-rtc-v1-1-ce0f9187576e@linaro.org
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

authored by

Linus Walleij and committed by
Alexandre Belloni
d890cfc2 2cf2a1ac

+69 -120
+69 -100
drivers/rtc/rtc-ds2404.c
··· 7 7 #include <linux/rtc.h> 8 8 #include <linux/types.h> 9 9 #include <linux/bcd.h> 10 - #include <linux/platform_data/rtc-ds2404.h> 11 10 #include <linux/delay.h> 12 - #include <linux/gpio.h> 11 + #include <linux/gpio/consumer.h> 13 12 #include <linux/slab.h> 14 13 15 14 #include <linux/io.h> ··· 26 27 #define DS2404_CLK 1 27 28 #define DS2404_DQ 2 28 29 29 - struct ds2404_gpio { 30 - const char *name; 31 - unsigned int gpio; 32 - }; 33 - 34 30 struct ds2404 { 35 - struct ds2404_gpio *gpio; 31 + struct device *dev; 32 + struct gpio_desc *rst_gpiod; 33 + struct gpio_desc *clk_gpiod; 34 + struct gpio_desc *dq_gpiod; 36 35 struct rtc_device *rtc; 37 36 }; 38 37 39 - static struct ds2404_gpio ds2404_gpio[] = { 40 - { "RTC RST", 0 }, 41 - { "RTC CLK", 0 }, 42 - { "RTC DQ", 0 }, 43 - }; 44 - 45 - static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev, 46 - struct ds2404_platform_data *pdata) 38 + static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev) 47 39 { 48 - int i, err; 40 + struct device *dev = &pdev->dev; 49 41 50 - ds2404_gpio[DS2404_RST].gpio = pdata->gpio_rst; 51 - ds2404_gpio[DS2404_CLK].gpio = pdata->gpio_clk; 52 - ds2404_gpio[DS2404_DQ].gpio = pdata->gpio_dq; 42 + /* This will de-assert RESET, declare this GPIO as GPIOD_ACTIVE_LOW */ 43 + chip->rst_gpiod = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW); 44 + if (IS_ERR(chip->rst_gpiod)) 45 + return PTR_ERR(chip->rst_gpiod); 53 46 54 - for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) { 55 - err = gpio_request(ds2404_gpio[i].gpio, ds2404_gpio[i].name); 56 - if (err) { 57 - dev_err(&pdev->dev, "error mapping gpio %s: %d\n", 58 - ds2404_gpio[i].name, err); 59 - goto err_request; 60 - } 61 - if (i != DS2404_DQ) 62 - gpio_direction_output(ds2404_gpio[i].gpio, 1); 63 - } 47 + chip->clk_gpiod = devm_gpiod_get(dev, "clk", GPIOD_OUT_HIGH); 48 + if (IS_ERR(chip->clk_gpiod)) 49 + return PTR_ERR(chip->clk_gpiod); 64 50 65 - chip->gpio = ds2404_gpio; 51 + chip->dq_gpiod = devm_gpiod_get(dev, "dq", GPIOD_ASIS); 52 + if (IS_ERR(chip->dq_gpiod)) 53 + return PTR_ERR(chip->dq_gpiod); 54 + 66 55 return 0; 67 - 68 - err_request: 69 - while (--i >= 0) 70 - gpio_free(ds2404_gpio[i].gpio); 71 - return err; 72 56 } 73 57 74 - static void ds2404_gpio_unmap(void *data) 58 + static void ds2404_reset(struct ds2404 *chip) 75 59 { 76 - int i; 77 - 78 - for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) 79 - gpio_free(ds2404_gpio[i].gpio); 80 - } 81 - 82 - static void ds2404_reset(struct device *dev) 83 - { 84 - gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 0); 60 + gpiod_set_value(chip->rst_gpiod, 1); 85 61 udelay(1000); 86 - gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 1); 87 - gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0); 88 - gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 0); 62 + gpiod_set_value(chip->rst_gpiod, 0); 63 + gpiod_set_value(chip->clk_gpiod, 0); 64 + gpiod_direction_output(chip->dq_gpiod, 0); 89 65 udelay(10); 90 66 } 91 67 92 - static void ds2404_write_byte(struct device *dev, u8 byte) 68 + static void ds2404_write_byte(struct ds2404 *chip, u8 byte) 93 69 { 94 70 int i; 95 71 96 - gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 1); 72 + gpiod_direction_output(chip->dq_gpiod, 1); 97 73 for (i = 0; i < 8; i++) { 98 - gpio_set_value(ds2404_gpio[DS2404_DQ].gpio, byte & (1 << i)); 74 + gpiod_set_value(chip->dq_gpiod, byte & (1 << i)); 99 75 udelay(10); 100 - gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1); 76 + gpiod_set_value(chip->clk_gpiod, 1); 101 77 udelay(10); 102 - gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0); 78 + gpiod_set_value(chip->clk_gpiod, 0); 103 79 udelay(10); 104 80 } 105 81 } 106 82 107 - static u8 ds2404_read_byte(struct device *dev) 83 + static u8 ds2404_read_byte(struct ds2404 *chip) 108 84 { 109 85 int i; 110 86 u8 ret = 0; 111 87 112 - gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio); 88 + gpiod_direction_input(chip->dq_gpiod); 113 89 114 90 for (i = 0; i < 8; i++) { 115 - gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0); 91 + gpiod_set_value(chip->clk_gpiod, 0); 116 92 udelay(10); 117 - if (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio)) 93 + if (gpiod_get_value(chip->dq_gpiod)) 118 94 ret |= 1 << i; 119 - gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1); 95 + gpiod_set_value(chip->clk_gpiod, 1); 120 96 udelay(10); 121 97 } 122 98 return ret; 123 99 } 124 100 125 - static void ds2404_read_memory(struct device *dev, u16 offset, 101 + static void ds2404_read_memory(struct ds2404 *chip, u16 offset, 126 102 int length, u8 *out) 127 103 { 128 - ds2404_reset(dev); 129 - ds2404_write_byte(dev, DS2404_READ_MEMORY_CMD); 130 - ds2404_write_byte(dev, offset & 0xff); 131 - ds2404_write_byte(dev, (offset >> 8) & 0xff); 104 + ds2404_reset(chip); 105 + ds2404_write_byte(chip, DS2404_READ_MEMORY_CMD); 106 + ds2404_write_byte(chip, offset & 0xff); 107 + ds2404_write_byte(chip, (offset >> 8) & 0xff); 132 108 while (length--) 133 - *out++ = ds2404_read_byte(dev); 109 + *out++ = ds2404_read_byte(chip); 134 110 } 135 111 136 - static void ds2404_write_memory(struct device *dev, u16 offset, 112 + static void ds2404_write_memory(struct ds2404 *chip, u16 offset, 137 113 int length, u8 *out) 138 114 { 139 115 int i; 140 116 u8 ta01, ta02, es; 141 117 142 - ds2404_reset(dev); 143 - ds2404_write_byte(dev, DS2404_WRITE_SCRATCHPAD_CMD); 144 - ds2404_write_byte(dev, offset & 0xff); 145 - ds2404_write_byte(dev, (offset >> 8) & 0xff); 118 + ds2404_reset(chip); 119 + ds2404_write_byte(chip, DS2404_WRITE_SCRATCHPAD_CMD); 120 + ds2404_write_byte(chip, offset & 0xff); 121 + ds2404_write_byte(chip, (offset >> 8) & 0xff); 146 122 147 123 for (i = 0; i < length; i++) 148 - ds2404_write_byte(dev, out[i]); 124 + ds2404_write_byte(chip, out[i]); 149 125 150 - ds2404_reset(dev); 151 - ds2404_write_byte(dev, DS2404_READ_SCRATCHPAD_CMD); 126 + ds2404_reset(chip); 127 + ds2404_write_byte(chip, DS2404_READ_SCRATCHPAD_CMD); 152 128 153 - ta01 = ds2404_read_byte(dev); 154 - ta02 = ds2404_read_byte(dev); 155 - es = ds2404_read_byte(dev); 129 + ta01 = ds2404_read_byte(chip); 130 + ta02 = ds2404_read_byte(chip); 131 + es = ds2404_read_byte(chip); 156 132 157 133 for (i = 0; i < length; i++) { 158 - if (out[i] != ds2404_read_byte(dev)) { 159 - dev_err(dev, "read invalid data\n"); 134 + if (out[i] != ds2404_read_byte(chip)) { 135 + dev_err(chip->dev, "read invalid data\n"); 160 136 return; 161 137 } 162 138 } 163 139 164 - ds2404_reset(dev); 165 - ds2404_write_byte(dev, DS2404_COPY_SCRATCHPAD_CMD); 166 - ds2404_write_byte(dev, ta01); 167 - ds2404_write_byte(dev, ta02); 168 - ds2404_write_byte(dev, es); 140 + ds2404_reset(chip); 141 + ds2404_write_byte(chip, DS2404_COPY_SCRATCHPAD_CMD); 142 + ds2404_write_byte(chip, ta01); 143 + ds2404_write_byte(chip, ta02); 144 + ds2404_write_byte(chip, es); 169 145 170 - gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio); 171 - while (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio)) 146 + while (gpiod_get_value(chip->dq_gpiod)) 172 147 ; 173 148 } 174 149 175 - static void ds2404_enable_osc(struct device *dev) 150 + static void ds2404_enable_osc(struct ds2404 *chip) 176 151 { 177 152 u8 in[1] = { 0x10 }; /* enable oscillator */ 178 - ds2404_write_memory(dev, 0x201, 1, in); 153 + 154 + ds2404_write_memory(chip, 0x201, 1, in); 179 155 } 180 156 181 157 static int ds2404_read_time(struct device *dev, struct rtc_time *dt) 182 158 { 159 + struct ds2404 *chip = dev_get_drvdata(dev); 183 160 unsigned long time = 0; 184 161 __le32 hw_time = 0; 185 162 186 - ds2404_read_memory(dev, 0x203, 4, (u8 *)&hw_time); 163 + ds2404_read_memory(chip, 0x203, 4, (u8 *)&hw_time); 187 164 time = le32_to_cpu(hw_time); 188 165 189 166 rtc_time64_to_tm(time, dt); ··· 168 193 169 194 static int ds2404_set_time(struct device *dev, struct rtc_time *dt) 170 195 { 196 + struct ds2404 *chip = dev_get_drvdata(dev); 171 197 u32 time = cpu_to_le32(rtc_tm_to_time64(dt)); 172 - ds2404_write_memory(dev, 0x203, 4, (u8 *)&time); 198 + ds2404_write_memory(chip, 0x203, 4, (u8 *)&time); 173 199 return 0; 174 200 } 175 201 ··· 181 205 182 206 static int rtc_probe(struct platform_device *pdev) 183 207 { 184 - struct ds2404_platform_data *pdata = dev_get_platdata(&pdev->dev); 185 208 struct ds2404 *chip; 186 209 int retval = -EBUSY; 187 210 ··· 188 213 if (!chip) 189 214 return -ENOMEM; 190 215 216 + chip->dev = &pdev->dev; 217 + 191 218 chip->rtc = devm_rtc_allocate_device(&pdev->dev); 192 219 if (IS_ERR(chip->rtc)) 193 220 return PTR_ERR(chip->rtc); 194 221 195 - retval = ds2404_gpio_map(chip, pdev, pdata); 222 + retval = ds2404_gpio_map(chip, pdev); 196 223 if (retval) 197 224 return retval; 198 - 199 - retval = devm_add_action_or_reset(&pdev->dev, ds2404_gpio_unmap, chip); 200 - if (retval) 201 - return retval; 202 - 203 - dev_info(&pdev->dev, "using GPIOs RST:%d, CLK:%d, DQ:%d\n", 204 - chip->gpio[DS2404_RST].gpio, chip->gpio[DS2404_CLK].gpio, 205 - chip->gpio[DS2404_DQ].gpio); 206 225 207 226 platform_set_drvdata(pdev, chip); 208 227 ··· 207 238 if (retval) 208 239 return retval; 209 240 210 - ds2404_enable_osc(&pdev->dev); 241 + ds2404_enable_osc(chip); 211 242 return 0; 212 243 } 213 244
-20
include/linux/platform_data/rtc-ds2404.h
··· 1 - /* 2 - * ds2404.h - platform data structure for the DS2404 RTC. 3 - * 4 - * This file is subject to the terms and conditions of the GNU General Public 5 - * License. See the file "COPYING" in the main directory of this archive 6 - * for more details. 7 - * 8 - * Copyright (C) 2012 Sven Schnelle <svens@stackframe.org> 9 - */ 10 - 11 - #ifndef __LINUX_DS2404_H 12 - #define __LINUX_DS2404_H 13 - 14 - struct ds2404_platform_data { 15 - 16 - unsigned int gpio_rst; 17 - unsigned int gpio_clk; 18 - unsigned int gpio_dq; 19 - }; 20 - #endif