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

rtc: ds1307: add clock provider support for DS3231

DS3231 has programmable square-wave output signal.
This enables to use this feature as a clock provider of
common clock framework.

Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Reviewed-by: Michael Turquette <mturquette@baylibre.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>

authored by

Akinobu Mita and committed by
Alexandre Belloni
6c6ff145 f3937549

+332 -1
+37
Documentation/devicetree/bindings/rtc/maxim,ds3231.txt
··· 1 + * Maxim DS3231 Real Time Clock 2 + 3 + Required properties: 4 + see: Documentation/devicetree/bindings/i2c/trivial-devices.txt 5 + 6 + Optional property: 7 + - #clock-cells: Should be 1. 8 + - clock-output-names: 9 + overwrite the default clock names "ds3231_clk_sqw" and "ds3231_clk_32khz". 10 + 11 + Each clock is assigned an identifier and client nodes can use this identifier 12 + to specify the clock which they consume. Following indices are allowed: 13 + - 0: square-wave output on the SQW pin 14 + - 1: square-wave output on the 32kHz pin 15 + 16 + - interrupts: rtc alarm/event interrupt. When this property is selected, 17 + clock on the SQW pin cannot be used. 18 + 19 + Example: 20 + 21 + ds3231: ds3231@51 { 22 + compatible = "maxim,ds3231"; 23 + reg = <0x68>; 24 + #clock-cells = <1>; 25 + }; 26 + 27 + device1 { 28 + ... 29 + clocks = <&ds3231 0>; 30 + ... 31 + }; 32 + 33 + device2 { 34 + ... 35 + clocks = <&ds3231 1>; 36 + ... 37 + };
+295 -1
drivers/rtc/rtc-ds1307.c
··· 21 21 #include <linux/string.h> 22 22 #include <linux/hwmon.h> 23 23 #include <linux/hwmon-sysfs.h> 24 + #include <linux/clk-provider.h> 24 25 25 26 /* 26 27 * We can't determine type by probing, but if we expect pre-Linux code ··· 92 91 # define DS1340_BIT_OSF 0x80 93 92 #define DS1337_REG_STATUS 0x0f 94 93 # define DS1337_BIT_OSF 0x80 94 + # define DS3231_BIT_EN32KHZ 0x08 95 95 # define DS1337_BIT_A2I 0x02 96 96 # define DS1337_BIT_A1I 0x01 97 97 #define DS1339_REG_ALARM1_SECS 0x07 ··· 122 120 u8 length, u8 *values); 123 121 s32 (*write_block_data)(const struct i2c_client *client, u8 command, 124 122 u8 length, const u8 *values); 123 + #ifdef CONFIG_COMMON_CLK 124 + struct clk_hw clks[2]; 125 + #endif 125 126 }; 126 127 127 128 struct chip_desc { ··· 931 926 { 932 927 } 933 928 934 - #endif 929 + #endif /* CONFIG_RTC_DRV_DS1307_HWMON */ 930 + 931 + /*----------------------------------------------------------------------*/ 932 + 933 + /* 934 + * Square-wave output support for DS3231 935 + * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf 936 + */ 937 + #ifdef CONFIG_COMMON_CLK 938 + 939 + enum { 940 + DS3231_CLK_SQW = 0, 941 + DS3231_CLK_32KHZ, 942 + }; 943 + 944 + #define clk_sqw_to_ds1307(clk) \ 945 + container_of(clk, struct ds1307, clks[DS3231_CLK_SQW]) 946 + #define clk_32khz_to_ds1307(clk) \ 947 + container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ]) 948 + 949 + static int ds3231_clk_sqw_rates[] = { 950 + 1, 951 + 1024, 952 + 4096, 953 + 8192, 954 + }; 955 + 956 + static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value) 957 + { 958 + struct i2c_client *client = ds1307->client; 959 + struct mutex *lock = &ds1307->rtc->ops_lock; 960 + int control; 961 + int ret; 962 + 963 + mutex_lock(lock); 964 + 965 + control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); 966 + if (control < 0) { 967 + ret = control; 968 + goto out; 969 + } 970 + 971 + control &= ~mask; 972 + control |= value; 973 + 974 + ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control); 975 + out: 976 + mutex_unlock(lock); 977 + 978 + return ret; 979 + } 980 + 981 + static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw, 982 + unsigned long parent_rate) 983 + { 984 + struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); 985 + int control; 986 + int rate_sel = 0; 987 + 988 + control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL); 989 + if (control < 0) 990 + return control; 991 + if (control & DS1337_BIT_RS1) 992 + rate_sel += 1; 993 + if (control & DS1337_BIT_RS2) 994 + rate_sel += 2; 995 + 996 + return ds3231_clk_sqw_rates[rate_sel]; 997 + } 998 + 999 + static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate, 1000 + unsigned long *prate) 1001 + { 1002 + int i; 1003 + 1004 + for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) { 1005 + if (ds3231_clk_sqw_rates[i] <= rate) 1006 + return ds3231_clk_sqw_rates[i]; 1007 + } 1008 + 1009 + return 0; 1010 + } 1011 + 1012 + static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate, 1013 + unsigned long parent_rate) 1014 + { 1015 + struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); 1016 + int control = 0; 1017 + int rate_sel; 1018 + 1019 + for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates); 1020 + rate_sel++) { 1021 + if (ds3231_clk_sqw_rates[rate_sel] == rate) 1022 + break; 1023 + } 1024 + 1025 + if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates)) 1026 + return -EINVAL; 1027 + 1028 + if (rate_sel & 1) 1029 + control |= DS1337_BIT_RS1; 1030 + if (rate_sel & 2) 1031 + control |= DS1337_BIT_RS2; 1032 + 1033 + return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2, 1034 + control); 1035 + } 1036 + 1037 + static int ds3231_clk_sqw_prepare(struct clk_hw *hw) 1038 + { 1039 + struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); 1040 + 1041 + return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0); 1042 + } 1043 + 1044 + static void ds3231_clk_sqw_unprepare(struct clk_hw *hw) 1045 + { 1046 + struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); 1047 + 1048 + ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN); 1049 + } 1050 + 1051 + static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw) 1052 + { 1053 + struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); 1054 + int control; 1055 + 1056 + control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL); 1057 + if (control < 0) 1058 + return control; 1059 + 1060 + return !(control & DS1337_BIT_INTCN); 1061 + } 1062 + 1063 + static const struct clk_ops ds3231_clk_sqw_ops = { 1064 + .prepare = ds3231_clk_sqw_prepare, 1065 + .unprepare = ds3231_clk_sqw_unprepare, 1066 + .is_prepared = ds3231_clk_sqw_is_prepared, 1067 + .recalc_rate = ds3231_clk_sqw_recalc_rate, 1068 + .round_rate = ds3231_clk_sqw_round_rate, 1069 + .set_rate = ds3231_clk_sqw_set_rate, 1070 + }; 1071 + 1072 + static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw, 1073 + unsigned long parent_rate) 1074 + { 1075 + return 32768; 1076 + } 1077 + 1078 + static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable) 1079 + { 1080 + struct i2c_client *client = ds1307->client; 1081 + struct mutex *lock = &ds1307->rtc->ops_lock; 1082 + int status; 1083 + int ret; 1084 + 1085 + mutex_lock(lock); 1086 + 1087 + status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS); 1088 + if (status < 0) { 1089 + ret = status; 1090 + goto out; 1091 + } 1092 + 1093 + if (enable) 1094 + status |= DS3231_BIT_EN32KHZ; 1095 + else 1096 + status &= ~DS3231_BIT_EN32KHZ; 1097 + 1098 + ret = i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, status); 1099 + out: 1100 + mutex_unlock(lock); 1101 + 1102 + return ret; 1103 + } 1104 + 1105 + static int ds3231_clk_32khz_prepare(struct clk_hw *hw) 1106 + { 1107 + struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); 1108 + 1109 + return ds3231_clk_32khz_control(ds1307, true); 1110 + } 1111 + 1112 + static void ds3231_clk_32khz_unprepare(struct clk_hw *hw) 1113 + { 1114 + struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); 1115 + 1116 + ds3231_clk_32khz_control(ds1307, false); 1117 + } 1118 + 1119 + static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw) 1120 + { 1121 + struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); 1122 + int status; 1123 + 1124 + status = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_STATUS); 1125 + if (status < 0) 1126 + return status; 1127 + 1128 + return !!(status & DS3231_BIT_EN32KHZ); 1129 + } 1130 + 1131 + static const struct clk_ops ds3231_clk_32khz_ops = { 1132 + .prepare = ds3231_clk_32khz_prepare, 1133 + .unprepare = ds3231_clk_32khz_unprepare, 1134 + .is_prepared = ds3231_clk_32khz_is_prepared, 1135 + .recalc_rate = ds3231_clk_32khz_recalc_rate, 1136 + }; 1137 + 1138 + static struct clk_init_data ds3231_clks_init[] = { 1139 + [DS3231_CLK_SQW] = { 1140 + .name = "ds3231_clk_sqw", 1141 + .ops = &ds3231_clk_sqw_ops, 1142 + .flags = CLK_IS_ROOT, 1143 + }, 1144 + [DS3231_CLK_32KHZ] = { 1145 + .name = "ds3231_clk_32khz", 1146 + .ops = &ds3231_clk_32khz_ops, 1147 + .flags = CLK_IS_ROOT, 1148 + }, 1149 + }; 1150 + 1151 + static int ds3231_clks_register(struct ds1307 *ds1307) 1152 + { 1153 + struct i2c_client *client = ds1307->client; 1154 + struct device_node *node = client->dev.of_node; 1155 + struct clk_onecell_data *onecell; 1156 + int i; 1157 + 1158 + onecell = devm_kzalloc(&client->dev, sizeof(*onecell), GFP_KERNEL); 1159 + if (!onecell) 1160 + return -ENOMEM; 1161 + 1162 + onecell->clk_num = ARRAY_SIZE(ds3231_clks_init); 1163 + onecell->clks = devm_kcalloc(&client->dev, onecell->clk_num, 1164 + sizeof(onecell->clks[0]), GFP_KERNEL); 1165 + if (!onecell->clks) 1166 + return -ENOMEM; 1167 + 1168 + for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) { 1169 + struct clk_init_data init = ds3231_clks_init[i]; 1170 + 1171 + /* 1172 + * Interrupt signal due to alarm conditions and square-wave 1173 + * output share same pin, so don't initialize both. 1174 + */ 1175 + if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags)) 1176 + continue; 1177 + 1178 + /* optional override of the clockname */ 1179 + of_property_read_string_index(node, "clock-output-names", i, 1180 + &init.name); 1181 + ds1307->clks[i].init = &init; 1182 + 1183 + onecell->clks[i] = devm_clk_register(&client->dev, 1184 + &ds1307->clks[i]); 1185 + if (IS_ERR(onecell->clks[i])) 1186 + return PTR_ERR(onecell->clks[i]); 1187 + } 1188 + 1189 + if (!node) 1190 + return 0; 1191 + 1192 + of_clk_add_provider(node, of_clk_src_onecell_get, onecell); 1193 + 1194 + return 0; 1195 + } 1196 + 1197 + static void ds1307_clks_register(struct ds1307 *ds1307) 1198 + { 1199 + int ret; 1200 + 1201 + if (ds1307->type != ds_3231) 1202 + return; 1203 + 1204 + ret = ds3231_clks_register(ds1307); 1205 + if (ret) { 1206 + dev_warn(&ds1307->client->dev, 1207 + "unable to register clock device %d\n", ret); 1208 + } 1209 + } 1210 + 1211 + #else 1212 + 1213 + static void ds1307_clks_register(struct ds1307 *ds1307) 1214 + { 1215 + } 1216 + 1217 + #endif /* CONFIG_COMMON_CLK */ 935 1218 936 1219 static int ds1307_probe(struct i2c_client *client, 937 1220 const struct i2c_device_id *id) ··· 1587 1294 } 1588 1295 1589 1296 ds1307_hwmon_register(ds1307); 1297 + ds1307_clks_register(ds1307); 1590 1298 1591 1299 return 0; 1592 1300