diff options
Diffstat (limited to 'drivers/rtc/rtc-v3020.c')
| -rw-r--r-- | drivers/rtc/rtc-v3020.c | 230 |
1 files changed, 188 insertions, 42 deletions
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c index 14d4f036a768..ad164056feb6 100644 --- a/drivers/rtc/rtc-v3020.c +++ b/drivers/rtc/rtc-v3020.c | |||
| @@ -27,17 +27,162 @@ | |||
| 27 | #include <linux/bcd.h> | 27 | #include <linux/bcd.h> |
| 28 | #include <linux/rtc-v3020.h> | 28 | #include <linux/rtc-v3020.h> |
| 29 | #include <linux/delay.h> | 29 | #include <linux/delay.h> |
| 30 | #include <linux/gpio.h> | ||
| 30 | 31 | ||
| 31 | #include <asm/io.h> | 32 | #include <linux/io.h> |
| 32 | 33 | ||
| 33 | #undef DEBUG | 34 | #undef DEBUG |
| 34 | 35 | ||
| 36 | struct v3020; | ||
| 37 | |||
| 38 | struct v3020_chip_ops { | ||
| 39 | int (*map_io)(struct v3020 *chip, struct platform_device *pdev, | ||
| 40 | struct v3020_platform_data *pdata); | ||
| 41 | void (*unmap_io)(struct v3020 *chip); | ||
| 42 | unsigned char (*read_bit)(struct v3020 *chip); | ||
| 43 | void (*write_bit)(struct v3020 *chip, unsigned char bit); | ||
| 44 | }; | ||
| 45 | |||
| 46 | #define V3020_CS 0 | ||
| 47 | #define V3020_WR 1 | ||
| 48 | #define V3020_RD 2 | ||
| 49 | #define V3020_IO 3 | ||
| 50 | |||
| 51 | struct v3020_gpio { | ||
| 52 | const char *name; | ||
| 53 | unsigned int gpio; | ||
| 54 | }; | ||
| 55 | |||
| 35 | struct v3020 { | 56 | struct v3020 { |
| 57 | /* MMIO access */ | ||
| 36 | void __iomem *ioaddress; | 58 | void __iomem *ioaddress; |
| 37 | int leftshift; | 59 | int leftshift; |
| 60 | |||
| 61 | /* GPIO access */ | ||
| 62 | struct v3020_gpio *gpio; | ||
| 63 | |||
| 64 | struct v3020_chip_ops *ops; | ||
| 65 | |||
| 38 | struct rtc_device *rtc; | 66 | struct rtc_device *rtc; |
| 39 | }; | 67 | }; |
| 40 | 68 | ||
| 69 | |||
| 70 | static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev, | ||
| 71 | struct v3020_platform_data *pdata) | ||
| 72 | { | ||
| 73 | if (pdev->num_resources != 1) | ||
| 74 | return -EBUSY; | ||
| 75 | |||
| 76 | if (pdev->resource[0].flags != IORESOURCE_MEM) | ||
| 77 | return -EBUSY; | ||
| 78 | |||
| 79 | chip->leftshift = pdata->leftshift; | ||
| 80 | chip->ioaddress = ioremap(pdev->resource[0].start, 1); | ||
| 81 | if (chip->ioaddress == NULL) | ||
| 82 | return -EBUSY; | ||
| 83 | |||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | static void v3020_mmio_unmap(struct v3020 *chip) | ||
| 88 | { | ||
| 89 | iounmap(chip->ioaddress); | ||
| 90 | } | ||
| 91 | |||
| 92 | static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit) | ||
| 93 | { | ||
| 94 | writel(bit << chip->leftshift, chip->ioaddress); | ||
| 95 | } | ||
| 96 | |||
| 97 | static unsigned char v3020_mmio_read_bit(struct v3020 *chip) | ||
| 98 | { | ||
| 99 | return readl(chip->ioaddress) & (1 << chip->leftshift); | ||
| 100 | } | ||
| 101 | |||
| 102 | static struct v3020_chip_ops v3020_mmio_ops = { | ||
| 103 | .map_io = v3020_mmio_map, | ||
| 104 | .unmap_io = v3020_mmio_unmap, | ||
| 105 | .read_bit = v3020_mmio_read_bit, | ||
| 106 | .write_bit = v3020_mmio_write_bit, | ||
| 107 | }; | ||
| 108 | |||
| 109 | static struct v3020_gpio v3020_gpio[] = { | ||
| 110 | { "RTC CS", 0 }, | ||
| 111 | { "RTC WR", 0 }, | ||
| 112 | { "RTC RD", 0 }, | ||
| 113 | { "RTC IO", 0 }, | ||
| 114 | }; | ||
| 115 | |||
| 116 | static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev, | ||
| 117 | struct v3020_platform_data *pdata) | ||
| 118 | { | ||
| 119 | int i, err; | ||
| 120 | |||
| 121 | v3020_gpio[V3020_CS].gpio = pdata->gpio_cs; | ||
| 122 | v3020_gpio[V3020_WR].gpio = pdata->gpio_wr; | ||
| 123 | v3020_gpio[V3020_RD].gpio = pdata->gpio_rd; | ||
| 124 | v3020_gpio[V3020_IO].gpio = pdata->gpio_io; | ||
| 125 | |||
| 126 | for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) { | ||
| 127 | err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name); | ||
| 128 | if (err) | ||
| 129 | goto err_request; | ||
| 130 | |||
| 131 | gpio_direction_output(v3020_gpio[i].gpio, 1); | ||
| 132 | } | ||
| 133 | |||
| 134 | chip->gpio = v3020_gpio; | ||
| 135 | |||
| 136 | return 0; | ||
| 137 | |||
| 138 | err_request: | ||
| 139 | while (--i >= 0) | ||
| 140 | gpio_free(v3020_gpio[i].gpio); | ||
| 141 | |||
| 142 | return err; | ||
| 143 | } | ||
| 144 | |||
| 145 | static void v3020_gpio_unmap(struct v3020 *chip) | ||
| 146 | { | ||
| 147 | int i; | ||
| 148 | |||
| 149 | for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) | ||
| 150 | gpio_free(v3020_gpio[i].gpio); | ||
| 151 | } | ||
| 152 | |||
| 153 | static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit) | ||
| 154 | { | ||
| 155 | gpio_direction_output(chip->gpio[V3020_IO].gpio, bit); | ||
| 156 | gpio_set_value(chip->gpio[V3020_CS].gpio, 0); | ||
| 157 | gpio_set_value(chip->gpio[V3020_WR].gpio, 0); | ||
| 158 | udelay(1); | ||
| 159 | gpio_set_value(chip->gpio[V3020_WR].gpio, 1); | ||
| 160 | gpio_set_value(chip->gpio[V3020_CS].gpio, 1); | ||
| 161 | } | ||
| 162 | |||
| 163 | static unsigned char v3020_gpio_read_bit(struct v3020 *chip) | ||
| 164 | { | ||
| 165 | int bit; | ||
| 166 | |||
| 167 | gpio_direction_input(chip->gpio[V3020_IO].gpio); | ||
| 168 | gpio_set_value(chip->gpio[V3020_CS].gpio, 0); | ||
| 169 | gpio_set_value(chip->gpio[V3020_RD].gpio, 0); | ||
| 170 | udelay(1); | ||
| 171 | bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio); | ||
| 172 | udelay(1); | ||
| 173 | gpio_set_value(chip->gpio[V3020_RD].gpio, 1); | ||
| 174 | gpio_set_value(chip->gpio[V3020_CS].gpio, 1); | ||
| 175 | |||
| 176 | return bit; | ||
| 177 | } | ||
| 178 | |||
| 179 | static struct v3020_chip_ops v3020_gpio_ops = { | ||
| 180 | .map_io = v3020_gpio_map, | ||
| 181 | .unmap_io = v3020_gpio_unmap, | ||
| 182 | .read_bit = v3020_gpio_read_bit, | ||
| 183 | .write_bit = v3020_gpio_write_bit, | ||
| 184 | }; | ||
| 185 | |||
| 41 | static void v3020_set_reg(struct v3020 *chip, unsigned char address, | 186 | static void v3020_set_reg(struct v3020 *chip, unsigned char address, |
| 42 | unsigned char data) | 187 | unsigned char data) |
| 43 | { | 188 | { |
| @@ -46,7 +191,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address, | |||
| 46 | 191 | ||
| 47 | tmp = address; | 192 | tmp = address; |
| 48 | for (i = 0; i < 4; i++) { | 193 | for (i = 0; i < 4; i++) { |
| 49 | writel((tmp & 1) << chip->leftshift, chip->ioaddress); | 194 | chip->ops->write_bit(chip, (tmp & 1)); |
| 50 | tmp >>= 1; | 195 | tmp >>= 1; |
| 51 | udelay(1); | 196 | udelay(1); |
| 52 | } | 197 | } |
| @@ -54,7 +199,7 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address, | |||
| 54 | /* Commands dont have data */ | 199 | /* Commands dont have data */ |
| 55 | if (!V3020_IS_COMMAND(address)) { | 200 | if (!V3020_IS_COMMAND(address)) { |
| 56 | for (i = 0; i < 8; i++) { | 201 | for (i = 0; i < 8; i++) { |
| 57 | writel((data & 1) << chip->leftshift, chip->ioaddress); | 202 | chip->ops->write_bit(chip, (data & 1)); |
| 58 | data >>= 1; | 203 | data >>= 1; |
| 59 | udelay(1); | 204 | udelay(1); |
| 60 | } | 205 | } |
| @@ -63,18 +208,18 @@ static void v3020_set_reg(struct v3020 *chip, unsigned char address, | |||
| 63 | 208 | ||
| 64 | static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address) | 209 | static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address) |
| 65 | { | 210 | { |
| 66 | unsigned int data=0; | 211 | unsigned int data = 0; |
| 67 | int i; | 212 | int i; |
| 68 | 213 | ||
| 69 | for (i = 0; i < 4; i++) { | 214 | for (i = 0; i < 4; i++) { |
| 70 | writel((address & 1) << chip->leftshift, chip->ioaddress); | 215 | chip->ops->write_bit(chip, (address & 1)); |
| 71 | address >>= 1; | 216 | address >>= 1; |
| 72 | udelay(1); | 217 | udelay(1); |
| 73 | } | 218 | } |
| 74 | 219 | ||
| 75 | for (i = 0; i < 8; i++) { | 220 | for (i = 0; i < 8; i++) { |
| 76 | data >>= 1; | 221 | data >>= 1; |
| 77 | if (readl(chip->ioaddress) & (1 << chip->leftshift)) | 222 | if (chip->ops->read_bit(chip)) |
| 78 | data |= 0x80; | 223 | data |= 0x80; |
| 79 | udelay(1); | 224 | udelay(1); |
| 80 | } | 225 | } |
| @@ -106,16 +251,14 @@ static int v3020_read_time(struct device *dev, struct rtc_time *dt) | |||
| 106 | tmp = v3020_get_reg(chip, V3020_YEAR); | 251 | tmp = v3020_get_reg(chip, V3020_YEAR); |
| 107 | dt->tm_year = bcd2bin(tmp)+100; | 252 | dt->tm_year = bcd2bin(tmp)+100; |
| 108 | 253 | ||
| 109 | #ifdef DEBUG | 254 | dev_dbg(dev, "\n%s : Read RTC values\n", __func__); |
| 110 | printk("\n%s : Read RTC values\n",__func__); | 255 | dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour); |
| 111 | printk("tm_hour: %i\n",dt->tm_hour); | 256 | dev_dbg(dev, "tm_min : %i\n", dt->tm_min); |
| 112 | printk("tm_min : %i\n",dt->tm_min); | 257 | dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec); |
| 113 | printk("tm_sec : %i\n",dt->tm_sec); | 258 | dev_dbg(dev, "tm_year: %i\n", dt->tm_year); |
| 114 | printk("tm_year: %i\n",dt->tm_year); | 259 | dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon); |
| 115 | printk("tm_mon : %i\n",dt->tm_mon); | 260 | dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday); |
| 116 | printk("tm_mday: %i\n",dt->tm_mday); | 261 | dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday); |
| 117 | printk("tm_wday: %i\n",dt->tm_wday); | ||
| 118 | #endif | ||
| 119 | 262 | ||
| 120 | return 0; | 263 | return 0; |
| 121 | } | 264 | } |
| @@ -125,15 +268,13 @@ static int v3020_set_time(struct device *dev, struct rtc_time *dt) | |||
| 125 | { | 268 | { |
| 126 | struct v3020 *chip = dev_get_drvdata(dev); | 269 | struct v3020 *chip = dev_get_drvdata(dev); |
| 127 | 270 | ||
| 128 | #ifdef DEBUG | 271 | dev_dbg(dev, "\n%s : Setting RTC values\n", __func__); |
| 129 | printk("\n%s : Setting RTC values\n",__func__); | 272 | dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec); |
| 130 | printk("tm_sec : %i\n",dt->tm_sec); | 273 | dev_dbg(dev, "tm_min : %i\n", dt->tm_min); |
| 131 | printk("tm_min : %i\n",dt->tm_min); | 274 | dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour); |
| 132 | printk("tm_hour: %i\n",dt->tm_hour); | 275 | dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday); |
| 133 | printk("tm_mday: %i\n",dt->tm_mday); | 276 | dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday); |
| 134 | printk("tm_wday: %i\n",dt->tm_wday); | 277 | dev_dbg(dev, "tm_year: %i\n", dt->tm_year); |
| 135 | printk("tm_year: %i\n",dt->tm_year); | ||
| 136 | #endif | ||
| 137 | 278 | ||
| 138 | /* Write all the values to ram... */ | 279 | /* Write all the values to ram... */ |
| 139 | v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec)); | 280 | v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec)); |
| @@ -168,30 +309,28 @@ static int rtc_probe(struct platform_device *pdev) | |||
| 168 | int i; | 309 | int i; |
| 169 | int temp; | 310 | int temp; |
| 170 | 311 | ||
| 171 | if (pdev->num_resources != 1) | ||
| 172 | return -EBUSY; | ||
| 173 | |||
| 174 | if (pdev->resource[0].flags != IORESOURCE_MEM) | ||
| 175 | return -EBUSY; | ||
| 176 | |||
| 177 | chip = kzalloc(sizeof *chip, GFP_KERNEL); | 312 | chip = kzalloc(sizeof *chip, GFP_KERNEL); |
| 178 | if (!chip) | 313 | if (!chip) |
| 179 | return -ENOMEM; | 314 | return -ENOMEM; |
| 180 | 315 | ||
| 181 | chip->leftshift = pdata->leftshift; | 316 | if (pdata->use_gpio) |
| 182 | chip->ioaddress = ioremap(pdev->resource[0].start, 1); | 317 | chip->ops = &v3020_gpio_ops; |
| 183 | if (chip->ioaddress == NULL) | 318 | else |
| 319 | chip->ops = &v3020_mmio_ops; | ||
| 320 | |||
| 321 | retval = chip->ops->map_io(chip, pdev, pdata); | ||
| 322 | if (retval) | ||
| 184 | goto err_chip; | 323 | goto err_chip; |
| 185 | 324 | ||
| 186 | /* Make sure the v3020 expects a communication cycle | 325 | /* Make sure the v3020 expects a communication cycle |
| 187 | * by reading 8 times */ | 326 | * by reading 8 times */ |
| 188 | for (i = 0; i < 8; i++) | 327 | for (i = 0; i < 8; i++) |
| 189 | temp = readl(chip->ioaddress); | 328 | temp = chip->ops->read_bit(chip); |
| 190 | 329 | ||
| 191 | /* Test chip by doing a write/read sequence | 330 | /* Test chip by doing a write/read sequence |
| 192 | * to the chip ram */ | 331 | * to the chip ram */ |
| 193 | v3020_set_reg(chip, V3020_SECONDS, 0x33); | 332 | v3020_set_reg(chip, V3020_SECONDS, 0x33); |
| 194 | if(v3020_get_reg(chip, V3020_SECONDS) != 0x33) { | 333 | if (v3020_get_reg(chip, V3020_SECONDS) != 0x33) { |
| 195 | retval = -ENODEV; | 334 | retval = -ENODEV; |
| 196 | goto err_io; | 335 | goto err_io; |
| 197 | } | 336 | } |
| @@ -200,10 +339,17 @@ static int rtc_probe(struct platform_device *pdev) | |||
| 200 | * are all disabled */ | 339 | * are all disabled */ |
| 201 | v3020_set_reg(chip, V3020_STATUS_0, 0x0); | 340 | v3020_set_reg(chip, V3020_STATUS_0, 0x0); |
| 202 | 341 | ||
| 203 | dev_info(&pdev->dev, "Chip available at physical address 0x%llx," | 342 | if (pdata->use_gpio) |
| 204 | "data connected to D%d\n", | 343 | dev_info(&pdev->dev, "Chip available at GPIOs " |
| 205 | (unsigned long long)pdev->resource[0].start, | 344 | "%d, %d, %d, %d\n", |
| 206 | chip->leftshift); | 345 | chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio, |
| 346 | chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio); | ||
| 347 | else | ||
| 348 | dev_info(&pdev->dev, "Chip available at " | ||
| 349 | "physical address 0x%llx," | ||
| 350 | "data connected to D%d\n", | ||
| 351 | (unsigned long long)pdev->resource[0].start, | ||
| 352 | chip->leftshift); | ||
| 207 | 353 | ||
| 208 | platform_set_drvdata(pdev, chip); | 354 | platform_set_drvdata(pdev, chip); |
| 209 | 355 | ||
| @@ -218,7 +364,7 @@ static int rtc_probe(struct platform_device *pdev) | |||
| 218 | return 0; | 364 | return 0; |
| 219 | 365 | ||
| 220 | err_io: | 366 | err_io: |
| 221 | iounmap(chip->ioaddress); | 367 | chip->ops->unmap_io(chip); |
| 222 | err_chip: | 368 | err_chip: |
| 223 | kfree(chip); | 369 | kfree(chip); |
| 224 | 370 | ||
| @@ -233,7 +379,7 @@ static int rtc_remove(struct platform_device *dev) | |||
| 233 | if (rtc) | 379 | if (rtc) |
| 234 | rtc_device_unregister(rtc); | 380 | rtc_device_unregister(rtc); |
| 235 | 381 | ||
| 236 | iounmap(chip->ioaddress); | 382 | chip->ops->unmap_io(chip); |
| 237 | kfree(chip); | 383 | kfree(chip); |
| 238 | 384 | ||
| 239 | return 0; | 385 | return 0; |
