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

rtc: rtc-max6902 fixes

- no changelogs in code
- no banners
- use local buffers
- fix probe sequence
- fixed style issues
- fix spi_write call
- removed old debug code

replaces http://patchwork.ozlabs.org/patch/9421/
and http://patchwork.ozlabs.org/patch/9455/

Signed-off-by: Alessandro Zummo <a.zummo@towertech.it>
Acked-by: David Brownell <david-b@pacbell.net>
Cc: Raphael Assenat <raph@raphnet.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Alessandro Zummo and committed by
Linus Torvalds
a5771c6c 45fd8a0c

+40 -140
+40 -140
drivers/rtc/rtc-max6902.c
··· 9 9 * 10 10 * Driver for MAX6902 spi RTC 11 11 * 12 - * Changelog: 13 - * 14 - * 24-May-2006: Raphael Assenat <raph@8d.com> 15 - * - Major rework 16 - * Converted to rtc_device and uses the SPI layer. 17 - * 18 - * ??-???-2005: Someone at Compulab 19 - * - Initial driver creation. 20 12 */ 21 13 22 14 #include <linux/module.h> ··· 18 26 #include <linux/rtc.h> 19 27 #include <linux/spi/spi.h> 20 28 #include <linux/bcd.h> 21 - #include <linux/delay.h> 22 29 23 30 #define MAX6902_REG_SECONDS 0x01 24 31 #define MAX6902_REG_MINUTES 0x03 ··· 29 38 #define MAX6902_REG_CONTROL 0x0F 30 39 #define MAX6902_REG_CENTURY 0x13 31 40 32 - #undef MAX6902_DEBUG 33 - 34 - struct max6902 { 35 - struct rtc_device *rtc; 36 - u8 buf[9]; /* Burst read cmd + 8 registers */ 37 - u8 tx_buf[2]; 38 - u8 rx_buf[2]; 39 - }; 40 - 41 - static void max6902_set_reg(struct device *dev, unsigned char address, 41 + static int max6902_set_reg(struct device *dev, unsigned char address, 42 42 unsigned char data) 43 43 { 44 44 struct spi_device *spi = to_spi_device(dev); ··· 39 57 buf[0] = address & 0x7f; 40 58 buf[1] = data; 41 59 42 - spi_write(spi, buf, 2); 60 + return spi_write_then_read(spi, buf, 2, NULL, 0); 43 61 } 44 62 45 63 static int max6902_get_reg(struct device *dev, unsigned char address, 46 64 unsigned char *data) 47 65 { 48 66 struct spi_device *spi = to_spi_device(dev); 49 - struct max6902 *chip = dev_get_drvdata(dev); 50 - struct spi_message message; 51 - struct spi_transfer xfer; 52 - int status; 53 - 54 - if (!data) 55 - return -EINVAL; 56 - 57 - /* Build our spi message */ 58 - spi_message_init(&message); 59 - memset(&xfer, 0, sizeof(xfer)); 60 - xfer.len = 2; 61 - /* Can tx_buf and rx_buf be equal? The doc in spi.h is not sure... */ 62 - xfer.tx_buf = chip->tx_buf; 63 - xfer.rx_buf = chip->rx_buf; 64 67 65 68 /* Set MSB to indicate read */ 66 - chip->tx_buf[0] = address | 0x80; 69 + *data = address | 0x80; 67 70 68 - spi_message_add_tail(&xfer, &message); 69 - 70 - /* do the i/o */ 71 - status = spi_sync(spi, &message); 72 - 73 - if (status == 0) 74 - *data = chip->rx_buf[1]; 75 - return status; 71 + return spi_write_then_read(spi, data, 1, data, 1); 76 72 } 77 73 78 - static int max6902_get_datetime(struct device *dev, struct rtc_time *dt) 74 + static int max6902_read_time(struct device *dev, struct rtc_time *dt) 79 75 { 80 - unsigned char tmp; 81 - int century; 82 - int err; 76 + int err, century; 83 77 struct spi_device *spi = to_spi_device(dev); 84 - struct max6902 *chip = dev_get_drvdata(dev); 85 - struct spi_message message; 86 - struct spi_transfer xfer; 87 - int status; 78 + unsigned char buf[8]; 88 79 89 - err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp); 90 - if (err) 80 + buf[0] = 0xbf; /* Burst read */ 81 + 82 + err = spi_write_then_read(spi, buf, 1, buf, 8); 83 + if (err != 0) 91 84 return err; 92 - 93 - /* build the message */ 94 - spi_message_init(&message); 95 - memset(&xfer, 0, sizeof(xfer)); 96 - xfer.len = 1 + 7; /* Burst read command + 7 registers */ 97 - xfer.tx_buf = chip->buf; 98 - xfer.rx_buf = chip->buf; 99 - chip->buf[0] = 0xbf; /* Burst read */ 100 - spi_message_add_tail(&xfer, &message); 101 - 102 - /* do the i/o */ 103 - status = spi_sync(spi, &message); 104 - if (status) 105 - return status; 106 85 107 86 /* The chip sends data in this order: 108 87 * Seconds, Minutes, Hours, Date, Month, Day, Year */ 109 - dt->tm_sec = bcd2bin(chip->buf[1]); 110 - dt->tm_min = bcd2bin(chip->buf[2]); 111 - dt->tm_hour = bcd2bin(chip->buf[3]); 112 - dt->tm_mday = bcd2bin(chip->buf[4]); 113 - dt->tm_mon = bcd2bin(chip->buf[5]) - 1; 114 - dt->tm_wday = bcd2bin(chip->buf[6]); 115 - dt->tm_year = bcd2bin(chip->buf[7]); 88 + dt->tm_sec = bcd2bin(buf[0]); 89 + dt->tm_min = bcd2bin(buf[1]); 90 + dt->tm_hour = bcd2bin(buf[2]); 91 + dt->tm_mday = bcd2bin(buf[3]); 92 + dt->tm_mon = bcd2bin(buf[4]) - 1; 93 + dt->tm_wday = bcd2bin(buf[5]); 94 + dt->tm_year = bcd2bin(buf[6]); 116 95 117 - century = bcd2bin(tmp) * 100; 96 + /* Read century */ 97 + err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]); 98 + if (err != 0) 99 + return err; 100 + 101 + century = bcd2bin(buf[0]) * 100; 118 102 119 103 dt->tm_year += century; 120 104 dt->tm_year -= 1900; 121 105 122 - #ifdef MAX6902_DEBUG 123 - printk("\n%s : Read RTC values\n",__func__); 124 - printk("tm_hour: %i\n",dt->tm_hour); 125 - printk("tm_min : %i\n",dt->tm_min); 126 - printk("tm_sec : %i\n",dt->tm_sec); 127 - printk("tm_year: %i\n",dt->tm_year); 128 - printk("tm_mon : %i\n",dt->tm_mon); 129 - printk("tm_mday: %i\n",dt->tm_mday); 130 - printk("tm_wday: %i\n",dt->tm_wday); 131 - #endif 132 - 133 - return 0; 106 + return rtc_valid_tm(dt); 134 107 } 135 108 136 - static int max6902_set_datetime(struct device *dev, struct rtc_time *dt) 109 + static int max6902_set_time(struct device *dev, struct rtc_time *dt) 137 110 { 138 - dt->tm_year = dt->tm_year+1900; 139 - 140 - #ifdef MAX6902_DEBUG 141 - printk("\n%s : Setting RTC values\n",__func__); 142 - printk("tm_sec : %i\n",dt->tm_sec); 143 - printk("tm_min : %i\n",dt->tm_min); 144 - printk("tm_hour: %i\n",dt->tm_hour); 145 - printk("tm_mday: %i\n",dt->tm_mday); 146 - printk("tm_wday: %i\n",dt->tm_wday); 147 - printk("tm_year: %i\n",dt->tm_year); 148 - #endif 111 + dt->tm_year = dt->tm_year + 1900; 149 112 150 113 /* Remove write protection */ 151 114 max6902_set_reg(dev, 0xF, 0); ··· 100 173 max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour)); 101 174 102 175 max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday)); 103 - max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon+1)); 176 + max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon + 1)); 104 177 max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday)); 105 - max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year%100)); 106 - max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year/100)); 178 + max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year % 100)); 179 + max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year / 100)); 107 180 108 181 /* Compulab used a delay here. However, the datasheet 109 182 * does not mention a delay being required anywhere... */ ··· 115 188 return 0; 116 189 } 117 190 118 - static int max6902_read_time(struct device *dev, struct rtc_time *tm) 119 - { 120 - return max6902_get_datetime(dev, tm); 121 - } 122 - 123 - static int max6902_set_time(struct device *dev, struct rtc_time *tm) 124 - { 125 - return max6902_set_datetime(dev, tm); 126 - } 127 - 128 191 static const struct rtc_class_ops max6902_rtc_ops = { 129 192 .read_time = max6902_read_time, 130 193 .set_time = max6902_set_time, ··· 124 207 { 125 208 struct rtc_device *rtc; 126 209 unsigned char tmp; 127 - struct max6902 *chip; 128 210 int res; 211 + 212 + spi->mode = SPI_MODE_3; 213 + spi->bits_per_word = 8; 214 + spi_setup(spi); 215 + 216 + res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); 217 + if (res != 0) 218 + return res; 129 219 130 220 rtc = rtc_device_register("max6902", 131 221 &spi->dev, &max6902_rtc_ops, THIS_MODULE); 132 222 if (IS_ERR(rtc)) 133 223 return PTR_ERR(rtc); 134 224 135 - spi->mode = SPI_MODE_3; 136 - spi->bits_per_word = 8; 137 - spi_setup(spi); 138 - 139 - chip = kzalloc(sizeof *chip, GFP_KERNEL); 140 - if (!chip) { 141 - rtc_device_unregister(rtc); 142 - return -ENOMEM; 143 - } 144 - chip->rtc = rtc; 145 - dev_set_drvdata(&spi->dev, chip); 146 - 147 - res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); 148 - if (res) { 149 - rtc_device_unregister(rtc); 150 - return res; 151 - } 152 - 153 225 return 0; 154 226 } 155 227 156 228 static int __devexit max6902_remove(struct spi_device *spi) 157 229 { 158 - struct max6902 *chip = platform_get_drvdata(spi); 159 - struct rtc_device *rtc = chip->rtc; 230 + struct rtc_device *rtc = platform_get_drvdata(spi); 160 231 161 - if (rtc) 162 - rtc_device_unregister(rtc); 163 - 164 - kfree(chip); 165 - 232 + rtc_device_unregister(rtc); 166 233 return 0; 167 234 } 168 235 ··· 162 261 163 262 static __init int max6902_init(void) 164 263 { 165 - printk("max6902 spi driver\n"); 166 264 return spi_register_driver(&max6902_driver); 167 265 } 168 266 module_init(max6902_init);