rtc-ds1347.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* rtc-ds1347.c
  2. *
  3. * Driver for Dallas Semiconductor DS1347 Low Current, SPI Compatible
  4. * Real Time Clock
  5. *
  6. * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/rtc.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/bcd.h>
  20. #include <linux/regmap.h>
  21. /* Registers in ds1347 rtc */
  22. #define DS1347_SECONDS_REG 0x01
  23. #define DS1347_MINUTES_REG 0x03
  24. #define DS1347_HOURS_REG 0x05
  25. #define DS1347_DATE_REG 0x07
  26. #define DS1347_MONTH_REG 0x09
  27. #define DS1347_DAY_REG 0x0B
  28. #define DS1347_YEAR_REG 0x0D
  29. #define DS1347_CONTROL_REG 0x0F
  30. #define DS1347_STATUS_REG 0x17
  31. #define DS1347_CLOCK_BURST 0x3F
  32. static const struct regmap_range ds1347_ranges[] = {
  33. {
  34. .range_min = DS1347_SECONDS_REG,
  35. .range_max = DS1347_STATUS_REG,
  36. },
  37. };
  38. static const struct regmap_access_table ds1347_access_table = {
  39. .yes_ranges = ds1347_ranges,
  40. .n_yes_ranges = ARRAY_SIZE(ds1347_ranges),
  41. };
  42. static int ds1347_read_time(struct device *dev, struct rtc_time *dt)
  43. {
  44. struct spi_device *spi = to_spi_device(dev);
  45. struct regmap *map;
  46. int err;
  47. unsigned char buf[8];
  48. map = spi_get_drvdata(spi);
  49. err = regmap_bulk_read(map, DS1347_CLOCK_BURST, buf, 8);
  50. if (err)
  51. return err;
  52. dt->tm_sec = bcd2bin(buf[0]);
  53. dt->tm_min = bcd2bin(buf[1]);
  54. dt->tm_hour = bcd2bin(buf[2] & 0x3F);
  55. dt->tm_mday = bcd2bin(buf[3]);
  56. dt->tm_mon = bcd2bin(buf[4]) - 1;
  57. dt->tm_wday = bcd2bin(buf[5]) - 1;
  58. dt->tm_year = bcd2bin(buf[6]) + 100;
  59. return rtc_valid_tm(dt);
  60. }
  61. static int ds1347_set_time(struct device *dev, struct rtc_time *dt)
  62. {
  63. struct spi_device *spi = to_spi_device(dev);
  64. struct regmap *map;
  65. unsigned char buf[8];
  66. map = spi_get_drvdata(spi);
  67. buf[0] = bin2bcd(dt->tm_sec);
  68. buf[1] = bin2bcd(dt->tm_min);
  69. buf[2] = (bin2bcd(dt->tm_hour) & 0x3F);
  70. buf[3] = bin2bcd(dt->tm_mday);
  71. buf[4] = bin2bcd(dt->tm_mon + 1);
  72. buf[5] = bin2bcd(dt->tm_wday + 1);
  73. /* year in linux is from 1900 i.e in range of 100
  74. in rtc it is from 00 to 99 */
  75. dt->tm_year = dt->tm_year % 100;
  76. buf[6] = bin2bcd(dt->tm_year);
  77. buf[7] = bin2bcd(0x00);
  78. /* write the rtc settings */
  79. return regmap_bulk_write(map, DS1347_CLOCK_BURST, buf, 8);
  80. }
  81. static const struct rtc_class_ops ds1347_rtc_ops = {
  82. .read_time = ds1347_read_time,
  83. .set_time = ds1347_set_time,
  84. };
  85. static int ds1347_probe(struct spi_device *spi)
  86. {
  87. struct rtc_device *rtc;
  88. struct regmap_config config;
  89. struct regmap *map;
  90. unsigned int data;
  91. int res;
  92. memset(&config, 0, sizeof(config));
  93. config.reg_bits = 8;
  94. config.val_bits = 8;
  95. config.read_flag_mask = 0x80;
  96. config.max_register = 0x3F;
  97. config.wr_table = &ds1347_access_table;
  98. /* spi setup with ds1347 in mode 3 and bits per word as 8 */
  99. spi->mode = SPI_MODE_3;
  100. spi->bits_per_word = 8;
  101. spi_setup(spi);
  102. map = devm_regmap_init_spi(spi, &config);
  103. if (IS_ERR(map)) {
  104. dev_err(&spi->dev, "ds1347 regmap init spi failed\n");
  105. return PTR_ERR(map);
  106. }
  107. spi_set_drvdata(spi, map);
  108. /* RTC Settings */
  109. res = regmap_read(map, DS1347_SECONDS_REG, &data);
  110. if (res)
  111. return res;
  112. /* Disable the write protect of rtc */
  113. regmap_read(map, DS1347_CONTROL_REG, &data);
  114. data = data & ~(1<<7);
  115. regmap_write(map, DS1347_CONTROL_REG, data);
  116. /* Enable the oscillator , disable the oscillator stop flag,
  117. and glitch filter to reduce current consumption */
  118. regmap_read(map, DS1347_STATUS_REG, &data);
  119. data = data & 0x1B;
  120. regmap_write(map, DS1347_STATUS_REG, data);
  121. /* display the settings */
  122. regmap_read(map, DS1347_CONTROL_REG, &data);
  123. dev_info(&spi->dev, "DS1347 RTC CTRL Reg = 0x%02x\n", data);
  124. regmap_read(map, DS1347_STATUS_REG, &data);
  125. dev_info(&spi->dev, "DS1347 RTC Status Reg = 0x%02x\n", data);
  126. rtc = devm_rtc_device_register(&spi->dev, "ds1347",
  127. &ds1347_rtc_ops, THIS_MODULE);
  128. if (IS_ERR(rtc))
  129. return PTR_ERR(rtc);
  130. return 0;
  131. }
  132. static struct spi_driver ds1347_driver = {
  133. .driver = {
  134. .name = "ds1347",
  135. },
  136. .probe = ds1347_probe,
  137. };
  138. module_spi_driver(ds1347_driver);
  139. MODULE_DESCRIPTION("DS1347 SPI RTC DRIVER");
  140. MODULE_AUTHOR("Raghavendra C Ganiga <ravi23ganiga@gmail.com>");
  141. MODULE_LICENSE("GPL v2");