Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Google, Inc. |
| 3 | * Copyright (c) 2009-2011 Code Aurora Forum. All rights reserved. |
| 4 | * Author: San Mehat <san@google.com> |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/version.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/android_alarm.h> |
| 25 | |
| 26 | #include <linux/rtc.h> |
| 27 | #include <linux/rtc-msm.h> |
| 28 | #include <linux/msm_rpcrouter.h> |
| 29 | #include <mach/msm_rpcrouter.h> |
| 30 | |
| 31 | #define APP_TIMEREMOTE_PDEV_NAME "rs00000000" |
| 32 | |
| 33 | #define TIMEREMOTE_PROCEEDURE_SET_JULIAN 6 |
| 34 | #define TIMEREMOTE_PROCEEDURE_GET_JULIAN 7 |
| 35 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 36 | #define TIMEREMOTE_PROCEEDURE_GET_SECURE_JULIAN 11 |
| 37 | #define TIMEREMOTE_PROCEEDURE_SET_SECURE_JULIAN 16 |
| 38 | #endif |
| 39 | #define TIMEREMOTE_PROG_NUMBER 0x30000048 |
| 40 | #define TIMEREMOTE_PROG_VER_1 0x00010001 |
| 41 | #define TIMEREMOTE_PROG_VER_2 0x00040001 |
| 42 | |
| 43 | #define RTC_REQUEST_CB_PROC 0x17 |
| 44 | #define RTC_CLIENT_INIT_PROC 0x12 |
| 45 | #define RTC_EVENT_CB_PROC 0x1 |
| 46 | #define RTC_CB_ID 0x1 |
| 47 | |
| 48 | /* Client request errors */ |
| 49 | enum rtc_rpc_err { |
| 50 | ERR_NONE, |
| 51 | ERR_CLIENT_ID_PTR, /* Invalid client ID pointer */ |
| 52 | ERR_CLIENT_TYPE, /* Invalid client type */ |
| 53 | ERR_CLIENT_ID, /* Invalid client ID */ |
| 54 | ERR_TASK_NOT_READY, /* task is not ready for clients */ |
| 55 | ERR_INVALID_PROCESSOR, /* Invalid processor id */ |
| 56 | ERR_UNSUPPORTED, /* Unsupported request */ |
| 57 | ERR_GENERAL, /* Any General Error */ |
| 58 | ERR_RPC, /* Any ONCRPC Error */ |
| 59 | ERR_ALREADY_REG, /* Client already registered */ |
| 60 | ERR_MAX |
| 61 | }; |
| 62 | |
| 63 | enum processor_type { |
| 64 | CLIENT_PROCESSOR_NONE = 0, |
| 65 | CLIENT_PROCESSOR_MODEM, |
| 66 | CLIENT_PROCESSOR_APP1, |
| 67 | CLIENT_PROCESSOR_APP2, |
| 68 | CLIENT_PROCESSOR_MAX |
| 69 | }; |
| 70 | |
| 71 | /* Client types */ |
| 72 | enum client_type { |
| 73 | CLIENT_TYPE_GEN1 = 0, |
| 74 | CLIENT_FLOATING1, |
| 75 | CLIENT_FLOATING2, |
| 76 | CLIENT_TYPE_INTERNAL, |
| 77 | CLIENT_TYPE_GENOFF_UPDATE, |
| 78 | CLIENT_TYPE_MAX |
| 79 | }; |
| 80 | |
| 81 | /* Event types */ |
| 82 | enum event_type { |
| 83 | EVENT_TOD_CHANGE = 0, |
| 84 | EVENT_GENOFF_CHANGE, |
| 85 | EVENT_MAX |
| 86 | }; |
| 87 | |
| 88 | struct tod_update_info { |
| 89 | uint32_t tick; |
| 90 | uint64_t stamp; |
| 91 | uint32_t freq; |
| 92 | }; |
| 93 | |
| 94 | enum time_bases_info { |
| 95 | TIME_RTC = 0, |
| 96 | TIME_TOD, |
| 97 | TIME_USER, |
| 98 | TIME_SECURE, |
| 99 | TIME_INVALID |
| 100 | }; |
| 101 | |
| 102 | struct genoff_update_info { |
| 103 | enum time_bases_info time_base; |
| 104 | uint64_t offset; |
| 105 | }; |
| 106 | |
| 107 | union cb_info { |
| 108 | struct tod_update_info tod_update; |
| 109 | struct genoff_update_info genoff_update; |
| 110 | }; |
| 111 | |
| 112 | struct rtc_cb_recv { |
| 113 | uint32_t client_cb_id; |
| 114 | enum event_type event; |
| 115 | uint32_t cb_info_ptr; |
| 116 | union cb_info cb_info_data; |
| 117 | }; |
| 118 | |
| 119 | struct msm_rtc { |
| 120 | int proc; |
| 121 | struct msm_rpc_client *rpc_client; |
| 122 | u8 client_id; |
| 123 | struct rtc_device *rtc; |
| 124 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 125 | struct rtc_device *rtcsecure; |
| 126 | #endif |
| 127 | unsigned long rtcalarm_time; |
| 128 | }; |
| 129 | |
| 130 | struct rpc_time_julian { |
| 131 | uint32_t year; |
| 132 | uint32_t month; |
| 133 | uint32_t day; |
| 134 | uint32_t hour; |
| 135 | uint32_t minute; |
| 136 | uint32_t second; |
| 137 | uint32_t day_of_week; |
| 138 | }; |
| 139 | |
| 140 | struct rtc_tod_args { |
| 141 | int proc; |
| 142 | struct rtc_time *tm; |
| 143 | }; |
| 144 | |
| 145 | #ifdef CONFIG_PM |
| 146 | struct suspend_state_info { |
| 147 | atomic_t state; |
| 148 | int64_t tick_at_suspend; |
| 149 | }; |
| 150 | |
| 151 | static struct suspend_state_info suspend_state = {ATOMIC_INIT(0), 0}; |
| 152 | |
| 153 | void msmrtc_updateatsuspend(struct timespec *ts) |
| 154 | { |
| 155 | int64_t now, sleep, sclk_max; |
| 156 | |
| 157 | if (atomic_read(&suspend_state.state)) { |
| 158 | now = msm_timer_get_sclk_time(&sclk_max); |
| 159 | |
| 160 | if (now && suspend_state.tick_at_suspend) { |
| 161 | if (now < suspend_state.tick_at_suspend) { |
| 162 | sleep = sclk_max - |
| 163 | suspend_state.tick_at_suspend + now; |
| 164 | } else |
| 165 | sleep = now - suspend_state.tick_at_suspend; |
| 166 | |
| 167 | timespec_add_ns(ts, sleep); |
| 168 | suspend_state.tick_at_suspend = now; |
| 169 | } else |
| 170 | pr_err("%s: Invalid ticks from SCLK now=%lld" |
| 171 | "tick_at_suspend=%lld", __func__, now, |
| 172 | suspend_state.tick_at_suspend); |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | #else |
| 177 | void msmrtc_updateatsuspend(struct timespec *ts) { } |
| 178 | #endif |
| 179 | EXPORT_SYMBOL(msmrtc_updateatsuspend); |
| 180 | |
| 181 | static int msmrtc_tod_proc_args(struct msm_rpc_client *client, void *buff, |
| 182 | void *data) |
| 183 | { |
| 184 | struct rtc_tod_args *rtc_args = data; |
| 185 | |
| 186 | if ((rtc_args->proc == TIMEREMOTE_PROCEEDURE_SET_JULIAN) |
| 187 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 188 | || (rtc_args->proc == TIMEREMOTE_PROCEEDURE_SET_SECURE_JULIAN) |
| 189 | #endif |
| 190 | ) { |
| 191 | struct timeremote_set_julian_req { |
| 192 | uint32_t opt_arg; |
| 193 | struct rpc_time_julian time; |
| 194 | }; |
| 195 | struct timeremote_set_julian_req *set_req = buff; |
| 196 | |
| 197 | set_req->opt_arg = cpu_to_be32(0x1); |
| 198 | set_req->time.year = cpu_to_be32(rtc_args->tm->tm_year); |
| 199 | set_req->time.month = cpu_to_be32(rtc_args->tm->tm_mon + 1); |
| 200 | set_req->time.day = cpu_to_be32(rtc_args->tm->tm_mday); |
| 201 | set_req->time.hour = cpu_to_be32(rtc_args->tm->tm_hour); |
| 202 | set_req->time.minute = cpu_to_be32(rtc_args->tm->tm_min); |
| 203 | set_req->time.second = cpu_to_be32(rtc_args->tm->tm_sec); |
| 204 | set_req->time.day_of_week = cpu_to_be32(rtc_args->tm->tm_wday); |
| 205 | |
| 206 | return sizeof(*set_req); |
| 207 | |
| 208 | } else if ((rtc_args->proc == TIMEREMOTE_PROCEEDURE_GET_JULIAN) |
| 209 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 210 | || (rtc_args->proc == TIMEREMOTE_PROCEEDURE_GET_SECURE_JULIAN) |
| 211 | #endif |
| 212 | ) { |
| 213 | *(uint32_t *)buff = (uint32_t) cpu_to_be32(0x1); |
| 214 | |
| 215 | return sizeof(uint32_t); |
| 216 | } else |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static bool rtc_check_overflow(struct rtc_time *tm) |
| 221 | { |
| 222 | if (tm->tm_year < 138) |
| 223 | return false; |
| 224 | |
| 225 | if (tm->tm_year > 138) |
| 226 | return true; |
| 227 | |
| 228 | if ((tm->tm_year == 138) && (tm->tm_mon == 0) && (tm->tm_mday < 19)) |
| 229 | return false; |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | static int msmrtc_tod_proc_result(struct msm_rpc_client *client, void *buff, |
| 235 | void *data) |
| 236 | { |
| 237 | struct rtc_tod_args *rtc_args = data; |
| 238 | |
| 239 | if ((rtc_args->proc == TIMEREMOTE_PROCEEDURE_GET_JULIAN) |
| 240 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 241 | || (rtc_args->proc == TIMEREMOTE_PROCEEDURE_GET_SECURE_JULIAN) |
| 242 | #endif |
| 243 | ) { |
| 244 | struct timeremote_get_julian_rep { |
| 245 | uint32_t opt_arg; |
| 246 | struct rpc_time_julian time; |
| 247 | }; |
| 248 | struct timeremote_get_julian_rep *result = buff; |
| 249 | |
| 250 | if (be32_to_cpu(result->opt_arg) != 0x1) |
| 251 | return -ENODATA; |
| 252 | |
| 253 | rtc_args->tm->tm_year = be32_to_cpu(result->time.year); |
| 254 | rtc_args->tm->tm_mon = be32_to_cpu(result->time.month); |
| 255 | rtc_args->tm->tm_mday = be32_to_cpu(result->time.day); |
| 256 | rtc_args->tm->tm_hour = be32_to_cpu(result->time.hour); |
| 257 | rtc_args->tm->tm_min = be32_to_cpu(result->time.minute); |
| 258 | rtc_args->tm->tm_sec = be32_to_cpu(result->time.second); |
| 259 | rtc_args->tm->tm_wday = be32_to_cpu(result->time.day_of_week); |
| 260 | |
| 261 | pr_debug("%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n", |
| 262 | __func__, rtc_args->tm->tm_mon, rtc_args->tm->tm_mday, |
| 263 | rtc_args->tm->tm_year, rtc_args->tm->tm_hour, |
| 264 | rtc_args->tm->tm_min, rtc_args->tm->tm_sec, |
| 265 | rtc_args->tm->tm_wday); |
| 266 | |
| 267 | /* RTC layer expects years to start at 1900 */ |
| 268 | rtc_args->tm->tm_year -= 1900; |
| 269 | /* RTC layer expects mons to be 0 based */ |
| 270 | rtc_args->tm->tm_mon--; |
| 271 | |
| 272 | if (rtc_valid_tm(rtc_args->tm) < 0) { |
| 273 | pr_err("%s: Retrieved data/time not valid\n", __func__); |
| 274 | rtc_time_to_tm(0, rtc_args->tm); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Check if the time received is > 01-19-2038, to prevent |
| 279 | * overflow. In such a case, return the EPOCH time. |
| 280 | */ |
| 281 | if (rtc_check_overflow(rtc_args->tm) == true) { |
| 282 | pr_err("Invalid time (year > 2038)\n"); |
| 283 | rtc_time_to_tm(0, rtc_args->tm); |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } else |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | msmrtc_timeremote_set_time(struct device *dev, struct rtc_time *tm) |
| 293 | { |
| 294 | int rc; |
| 295 | struct rtc_tod_args rtc_args; |
| 296 | struct msm_rtc *rtc_pdata = dev_get_drvdata(dev); |
| 297 | |
| 298 | if (tm->tm_year < 1900) |
| 299 | tm->tm_year += 1900; |
| 300 | |
| 301 | if (tm->tm_year < 1970) |
| 302 | return -EINVAL; |
| 303 | |
| 304 | dev_dbg(dev, "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n", |
| 305 | __func__, tm->tm_mon, tm->tm_mday, tm->tm_year, |
| 306 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 307 | |
| 308 | rtc_args.proc = TIMEREMOTE_PROCEEDURE_SET_JULIAN; |
| 309 | rtc_args.tm = tm; |
| 310 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, |
| 311 | TIMEREMOTE_PROCEEDURE_SET_JULIAN, |
| 312 | msmrtc_tod_proc_args, &rtc_args, |
| 313 | NULL, NULL, -1); |
| 314 | if (rc) { |
| 315 | dev_err(dev, "%s: rtc time (TOD) could not be set\n", __func__); |
| 316 | return rc; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int |
| 323 | msmrtc_timeremote_read_time(struct device *dev, struct rtc_time *tm) |
| 324 | { |
| 325 | int rc; |
| 326 | struct rtc_tod_args rtc_args; |
| 327 | struct msm_rtc *rtc_pdata = dev_get_drvdata(dev); |
| 328 | |
| 329 | rtc_args.proc = TIMEREMOTE_PROCEEDURE_GET_JULIAN; |
| 330 | rtc_args.tm = tm; |
| 331 | |
| 332 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, |
| 333 | TIMEREMOTE_PROCEEDURE_GET_JULIAN, |
| 334 | msmrtc_tod_proc_args, &rtc_args, |
| 335 | msmrtc_tod_proc_result, &rtc_args, -1); |
| 336 | |
| 337 | if (rc) { |
| 338 | dev_err(dev, "%s: Error retrieving rtc (TOD) time\n", __func__); |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int |
| 346 | msmrtc_virtual_alarm_set(struct device *dev, struct rtc_wkalrm *a) |
| 347 | { |
| 348 | struct msm_rtc *rtc_pdata = dev_get_drvdata(dev); |
| 349 | unsigned long now = get_seconds(); |
| 350 | |
| 351 | if (!a->enabled) { |
| 352 | rtc_pdata->rtcalarm_time = 0; |
| 353 | return 0; |
| 354 | } else |
| 355 | rtc_tm_to_time(&a->time, &(rtc_pdata->rtcalarm_time)); |
| 356 | |
| 357 | if (now > rtc_pdata->rtcalarm_time) { |
| 358 | dev_err(dev, "%s: Attempt to set alarm in the past\n", |
| 359 | __func__); |
| 360 | rtc_pdata->rtcalarm_time = 0; |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static struct rtc_class_ops msm_rtc_ops = { |
| 368 | .read_time = msmrtc_timeremote_read_time, |
| 369 | .set_time = msmrtc_timeremote_set_time, |
| 370 | .set_alarm = msmrtc_virtual_alarm_set, |
| 371 | }; |
| 372 | |
| 373 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 374 | static int |
| 375 | msmrtc_timeremote_set_time_secure(struct device *dev, struct rtc_time *tm) |
| 376 | { |
| 377 | int rc; |
| 378 | struct rtc_tod_args rtc_args; |
| 379 | struct msm_rtc *rtc_pdata = dev_get_drvdata(dev); |
| 380 | |
| 381 | if (tm->tm_year < 1900) |
| 382 | tm->tm_year += 1900; |
| 383 | |
| 384 | if (tm->tm_year < 1970) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | dev_dbg(dev, "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n", |
| 388 | __func__, tm->tm_mon, tm->tm_mday, tm->tm_year, |
| 389 | tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday); |
| 390 | |
| 391 | rtc_args.proc = TIMEREMOTE_PROCEEDURE_SET_SECURE_JULIAN; |
| 392 | rtc_args.tm = tm; |
| 393 | |
| 394 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, |
| 395 | TIMEREMOTE_PROCEEDURE_SET_SECURE_JULIAN, |
| 396 | msmrtc_tod_proc_args, &rtc_args, |
| 397 | NULL, NULL, -1); |
| 398 | if (rc) { |
| 399 | dev_err(dev, |
| 400 | "%s: rtc secure time could not be set\n", __func__); |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int |
| 408 | msmrtc_timeremote_read_time_secure(struct device *dev, struct rtc_time *tm) |
| 409 | { |
| 410 | int rc; |
| 411 | struct rtc_tod_args rtc_args; |
| 412 | struct msm_rtc *rtc_pdata = dev_get_drvdata(dev); |
| 413 | rtc_args.proc = TIMEREMOTE_PROCEEDURE_GET_SECURE_JULIAN; |
| 414 | rtc_args.tm = tm; |
| 415 | |
| 416 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, |
| 417 | TIMEREMOTE_PROCEEDURE_GET_SECURE_JULIAN, msmrtc_tod_proc_args, |
| 418 | &rtc_args, msmrtc_tod_proc_result, &rtc_args, -1); |
| 419 | |
| 420 | if (rc) { |
| 421 | dev_err(dev, |
| 422 | "%s: Error retrieving secure rtc time\n", __func__); |
| 423 | return rc; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static struct rtc_class_ops msm_rtc_ops_secure = { |
| 430 | .read_time = msmrtc_timeremote_read_time_secure, |
| 431 | .set_time = msmrtc_timeremote_set_time_secure, |
| 432 | }; |
| 433 | #endif |
| 434 | |
| 435 | static void process_cb_request(void *buffer) |
| 436 | { |
| 437 | struct rtc_cb_recv *rtc_cb = buffer; |
| 438 | struct timespec ts, tv; |
| 439 | |
| 440 | rtc_cb->client_cb_id = be32_to_cpu(rtc_cb->client_cb_id); |
| 441 | rtc_cb->event = be32_to_cpu(rtc_cb->event); |
| 442 | rtc_cb->cb_info_ptr = be32_to_cpu(rtc_cb->cb_info_ptr); |
| 443 | |
| 444 | if (rtc_cb->event == EVENT_TOD_CHANGE) { |
| 445 | /* A TOD update has been received from the Modem */ |
| 446 | rtc_cb->cb_info_data.tod_update.tick = |
| 447 | be32_to_cpu(rtc_cb->cb_info_data.tod_update.tick); |
| 448 | rtc_cb->cb_info_data.tod_update.stamp = |
| 449 | be64_to_cpu(rtc_cb->cb_info_data.tod_update.stamp); |
| 450 | rtc_cb->cb_info_data.tod_update.freq = |
| 451 | be32_to_cpu(rtc_cb->cb_info_data.tod_update.freq); |
| 452 | pr_info("RPC CALL -- TOD TIME UPDATE: ttick = %d\n" |
| 453 | "stamp=%lld, freq = %d\n", |
| 454 | rtc_cb->cb_info_data.tod_update.tick, |
| 455 | rtc_cb->cb_info_data.tod_update.stamp, |
| 456 | rtc_cb->cb_info_data.tod_update.freq); |
| 457 | |
| 458 | getnstimeofday(&ts); |
| 459 | msmrtc_updateatsuspend(&ts); |
| 460 | rtc_hctosys(); |
| 461 | getnstimeofday(&tv); |
| 462 | /* Update the alarm information with the new time info. */ |
| 463 | alarm_update_timedelta(ts, tv); |
| 464 | |
| 465 | } else |
| 466 | pr_err("%s: Unknown event EVENT=%x\n", |
| 467 | __func__, rtc_cb->event); |
| 468 | } |
| 469 | |
| 470 | static int msmrtc_cb_func(struct msm_rpc_client *client, void *buffer, int size) |
| 471 | { |
| 472 | int rc = -1; |
| 473 | struct rpc_request_hdr *recv = buffer; |
| 474 | |
| 475 | recv->xid = be32_to_cpu(recv->xid); |
| 476 | recv->type = be32_to_cpu(recv->type); |
| 477 | recv->rpc_vers = be32_to_cpu(recv->rpc_vers); |
| 478 | recv->prog = be32_to_cpu(recv->prog); |
| 479 | recv->vers = be32_to_cpu(recv->vers); |
| 480 | recv->procedure = be32_to_cpu(recv->procedure); |
| 481 | |
| 482 | if (recv->procedure == RTC_EVENT_CB_PROC) |
| 483 | process_cb_request((void *) (recv + 1)); |
| 484 | |
| 485 | msm_rpc_start_accepted_reply(client, recv->xid, |
| 486 | RPC_ACCEPTSTAT_SUCCESS); |
| 487 | |
| 488 | rc = msm_rpc_send_accepted_reply(client, 0); |
| 489 | if (rc) { |
| 490 | pr_debug("%s: sending reply failed: %d\n", __func__, rc); |
| 491 | return rc; |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static int msmrtc_rpc_proc_args(struct msm_rpc_client *client, void *buff, |
| 498 | void *data) |
| 499 | { |
| 500 | struct msm_rtc *rtc_pdata = data; |
| 501 | |
| 502 | if (rtc_pdata->proc == RTC_CLIENT_INIT_PROC) { |
| 503 | /* arguments passed to the client_init function */ |
| 504 | struct rtc_client_init_req { |
| 505 | enum client_type client; |
| 506 | uint32_t client_id_ptr; |
| 507 | u8 client_id; |
| 508 | enum processor_type processor; |
| 509 | }; |
| 510 | struct rtc_client_init_req *req_1 = buff; |
| 511 | |
| 512 | req_1->client = cpu_to_be32(CLIENT_TYPE_INTERNAL); |
| 513 | req_1->client_id_ptr = cpu_to_be32(0x1); |
| 514 | req_1->client_id = (u8) cpu_to_be32(0x1); |
| 515 | req_1->processor = cpu_to_be32(CLIENT_PROCESSOR_APP1); |
| 516 | |
| 517 | return sizeof(*req_1); |
| 518 | |
| 519 | } else if (rtc_pdata->proc == RTC_REQUEST_CB_PROC) { |
| 520 | /* arguments passed to the request_cb function */ |
| 521 | struct rtc_event_req { |
| 522 | u8 client_id; |
| 523 | uint32_t rtc_cb_id; |
| 524 | }; |
| 525 | struct rtc_event_req *req_2 = buff; |
| 526 | |
| 527 | req_2->client_id = (u8) cpu_to_be32(rtc_pdata->client_id); |
| 528 | req_2->rtc_cb_id = cpu_to_be32(RTC_CB_ID); |
| 529 | |
| 530 | return sizeof(*req_2); |
| 531 | } else |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int msmrtc_rpc_proc_result(struct msm_rpc_client *client, void *buff, |
| 536 | void *data) |
| 537 | { |
| 538 | uint32_t result = -EINVAL; |
| 539 | struct msm_rtc *rtc_pdata = data; |
| 540 | |
| 541 | if (rtc_pdata->proc == RTC_CLIENT_INIT_PROC) { |
| 542 | /* process reply received from client_init function */ |
| 543 | uint32_t client_id_ptr; |
| 544 | result = be32_to_cpu(*(uint32_t *)buff); |
| 545 | buff += sizeof(uint32_t); |
| 546 | client_id_ptr = be32_to_cpu(*(uint32_t *)(buff)); |
| 547 | buff += sizeof(uint32_t); |
| 548 | if (client_id_ptr == 1) |
| 549 | rtc_pdata->client_id = (u8) |
| 550 | be32_to_cpu(*(uint32_t *)(buff)); |
| 551 | else { |
| 552 | pr_debug("%s: Client-id not received from Modem\n", |
| 553 | __func__); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | } else if (rtc_pdata->proc == RTC_REQUEST_CB_PROC) { |
| 557 | /* process reply received from request_cb function */ |
| 558 | result = be32_to_cpu(*(uint32_t *)buff); |
| 559 | } |
| 560 | |
| 561 | if (result == ERR_NONE) { |
| 562 | pr_debug("%s: RPC client reply for PROC=%x success\n", |
| 563 | __func__, rtc_pdata->proc); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | pr_debug("%s: RPC client registration failed ERROR=%x\n", |
| 568 | __func__, result); |
| 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | static int msmrtc_setup_cb(struct msm_rtc *rtc_pdata) |
| 573 | { |
| 574 | int rc; |
| 575 | |
| 576 | /* Register with the server with client specific info */ |
| 577 | rtc_pdata->proc = RTC_CLIENT_INIT_PROC; |
| 578 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, RTC_CLIENT_INIT_PROC, |
| 579 | msmrtc_rpc_proc_args, rtc_pdata, |
| 580 | msmrtc_rpc_proc_result, rtc_pdata, -1); |
| 581 | if (rc) { |
| 582 | pr_debug("%s: RPC client registration for PROC:%x failed\n", |
| 583 | __func__, RTC_CLIENT_INIT_PROC); |
| 584 | return rc; |
| 585 | } |
| 586 | |
| 587 | /* Register with server for the callback event */ |
| 588 | rtc_pdata->proc = RTC_REQUEST_CB_PROC; |
| 589 | rc = msm_rpc_client_req(rtc_pdata->rpc_client, RTC_REQUEST_CB_PROC, |
| 590 | msmrtc_rpc_proc_args, rtc_pdata, |
| 591 | msmrtc_rpc_proc_result, rtc_pdata, -1); |
| 592 | if (rc) { |
| 593 | pr_debug("%s: RPC client registration for PROC:%x failed\n", |
| 594 | __func__, RTC_REQUEST_CB_PROC); |
| 595 | } |
| 596 | |
| 597 | return rc; |
| 598 | } |
| 599 | |
| 600 | static int __devinit |
| 601 | msmrtc_probe(struct platform_device *pdev) |
| 602 | { |
| 603 | int rc; |
| 604 | struct msm_rtc *rtc_pdata = NULL; |
| 605 | struct rpcsvr_platform_device *rdev = |
| 606 | container_of(pdev, struct rpcsvr_platform_device, base); |
| 607 | uint32_t prog_version; |
| 608 | |
| 609 | |
| 610 | if (pdev->id == (TIMEREMOTE_PROG_VER_1 & RPC_VERSION_MAJOR_MASK)) |
| 611 | prog_version = TIMEREMOTE_PROG_VER_1; |
| 612 | else if (pdev->id == (TIMEREMOTE_PROG_VER_2 & |
| 613 | RPC_VERSION_MAJOR_MASK)) |
| 614 | prog_version = TIMEREMOTE_PROG_VER_2; |
| 615 | else |
| 616 | return -EINVAL; |
| 617 | |
| 618 | rtc_pdata = kzalloc(sizeof(*rtc_pdata), GFP_KERNEL); |
| 619 | if (rtc_pdata == NULL) { |
| 620 | dev_err(&pdev->dev, |
| 621 | "%s: Unable to allocate memory\n", __func__); |
| 622 | return -ENOMEM; |
| 623 | } |
| 624 | rtc_pdata->rpc_client = msm_rpc_register_client("rtc", rdev->prog, |
| 625 | prog_version, 1, msmrtc_cb_func); |
| 626 | if (IS_ERR(rtc_pdata->rpc_client)) { |
| 627 | dev_err(&pdev->dev, |
| 628 | "%s: init RPC failed! VERS = %x\n", __func__, |
| 629 | prog_version); |
| 630 | rc = PTR_ERR(rtc_pdata->rpc_client); |
| 631 | kfree(rtc_pdata); |
| 632 | return rc; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Set up the callback client. |
| 637 | * For older targets this initialization will fail |
| 638 | */ |
| 639 | rc = msmrtc_setup_cb(rtc_pdata); |
| 640 | if (rc) |
| 641 | dev_dbg(&pdev->dev, "%s: Could not initialize RPC callback\n", |
| 642 | __func__); |
| 643 | |
| 644 | rtc_pdata->rtcalarm_time = 0; |
| 645 | platform_set_drvdata(pdev, rtc_pdata); |
| 646 | |
| 647 | rtc_pdata->rtc = rtc_device_register("msm_rtc", |
| 648 | &pdev->dev, |
| 649 | &msm_rtc_ops, |
| 650 | THIS_MODULE); |
| 651 | if (IS_ERR(rtc_pdata->rtc)) { |
| 652 | dev_err(&pdev->dev, "%s: Can't register RTC device (%ld)\n", |
| 653 | pdev->name, PTR_ERR(rtc_pdata->rtc)); |
| 654 | rc = PTR_ERR(rtc_pdata->rtc); |
| 655 | goto fail_cb_setup; |
| 656 | } |
| 657 | |
| 658 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 659 | rtc_pdata->rtcsecure = rtc_device_register("msm_rtc_secure", |
| 660 | &pdev->dev, |
| 661 | &msm_rtc_ops_secure, |
| 662 | THIS_MODULE); |
| 663 | |
| 664 | if (IS_ERR(rtc_pdata->rtcsecure)) { |
| 665 | dev_err(&pdev->dev, |
| 666 | "%s: Can't register RTC Secure device (%ld)\n", |
| 667 | pdev->name, PTR_ERR(rtc_pdata->rtcsecure)); |
| 668 | rtc_device_unregister(rtc_pdata->rtc); |
| 669 | rc = PTR_ERR(rtc_pdata->rtcsecure); |
| 670 | goto fail_cb_setup; |
| 671 | } |
| 672 | #endif |
| 673 | |
| 674 | #ifdef CONFIG_RTC_ASYNC_MODEM_SUPPORT |
| 675 | rtc_hctosys(); |
| 676 | #endif |
| 677 | |
| 678 | return 0; |
| 679 | |
| 680 | fail_cb_setup: |
| 681 | msm_rpc_unregister_client(rtc_pdata->rpc_client); |
| 682 | kfree(rtc_pdata); |
| 683 | return rc; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | #ifdef CONFIG_PM |
| 688 | |
| 689 | static void |
| 690 | msmrtc_alarmtimer_expired(unsigned long _data, |
| 691 | struct msm_rtc *rtc_pdata) |
| 692 | { |
| 693 | pr_debug("%s: Generating alarm event (src %lu)\n", |
| 694 | rtc_pdata->rtc->name, _data); |
| 695 | |
| 696 | rtc_update_irq(rtc_pdata->rtc, 1, RTC_IRQF | RTC_AF); |
| 697 | rtc_pdata->rtcalarm_time = 0; |
| 698 | } |
| 699 | |
| 700 | static int |
| 701 | msmrtc_suspend(struct platform_device *dev, pm_message_t state) |
| 702 | { |
| 703 | int rc, diff; |
| 704 | struct rtc_time tm; |
| 705 | unsigned long now; |
| 706 | struct msm_rtc *rtc_pdata = platform_get_drvdata(dev); |
| 707 | |
| 708 | suspend_state.tick_at_suspend = msm_timer_get_sclk_time(NULL); |
| 709 | if (rtc_pdata->rtcalarm_time) { |
| 710 | rc = msmrtc_timeremote_read_time(&dev->dev, &tm); |
| 711 | if (rc) { |
| 712 | dev_err(&dev->dev, |
| 713 | "%s: Unable to read from RTC\n", __func__); |
| 714 | return rc; |
| 715 | } |
| 716 | rtc_tm_to_time(&tm, &now); |
| 717 | diff = rtc_pdata->rtcalarm_time - now; |
| 718 | if (diff <= 0) { |
| 719 | msmrtc_alarmtimer_expired(1 , rtc_pdata); |
| 720 | msm_pm_set_max_sleep_time(0); |
| 721 | atomic_inc(&suspend_state.state); |
| 722 | return 0; |
| 723 | } |
| 724 | msm_pm_set_max_sleep_time((int64_t) |
| 725 | ((int64_t) diff * NSEC_PER_SEC)); |
| 726 | } else |
| 727 | msm_pm_set_max_sleep_time(0); |
| 728 | atomic_inc(&suspend_state.state); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | msmrtc_resume(struct platform_device *dev) |
| 734 | { |
| 735 | int rc, diff; |
| 736 | struct rtc_time tm; |
| 737 | unsigned long now; |
| 738 | struct msm_rtc *rtc_pdata = platform_get_drvdata(dev); |
| 739 | |
| 740 | if (rtc_pdata->rtcalarm_time) { |
| 741 | rc = msmrtc_timeremote_read_time(&dev->dev, &tm); |
| 742 | if (rc) { |
| 743 | dev_err(&dev->dev, |
| 744 | "%s: Unable to read from RTC\n", __func__); |
| 745 | return rc; |
| 746 | } |
| 747 | rtc_tm_to_time(&tm, &now); |
| 748 | diff = rtc_pdata->rtcalarm_time - now; |
| 749 | if (diff <= 0) |
| 750 | msmrtc_alarmtimer_expired(2 , rtc_pdata); |
| 751 | } |
| 752 | suspend_state.tick_at_suspend = 0; |
| 753 | atomic_dec(&suspend_state.state); |
| 754 | return 0; |
| 755 | } |
| 756 | #else |
| 757 | #define msmrtc_suspend NULL |
| 758 | #define msmrtc_resume NULL |
| 759 | #endif |
| 760 | |
| 761 | static int __devexit msmrtc_remove(struct platform_device *pdev) |
| 762 | { |
| 763 | struct msm_rtc *rtc_pdata = platform_get_drvdata(pdev); |
| 764 | |
| 765 | rtc_device_unregister(rtc_pdata->rtc); |
| 766 | #ifdef CONFIG_RTC_SECURE_TIME_SUPPORT |
| 767 | rtc_device_unregister(rtc_pdata->rtcsecure); |
| 768 | #endif |
| 769 | msm_rpc_unregister_client(rtc_pdata->rpc_client); |
| 770 | kfree(rtc_pdata); |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static struct platform_driver msmrtc_driver = { |
| 776 | .probe = msmrtc_probe, |
| 777 | .suspend = msmrtc_suspend, |
| 778 | .resume = msmrtc_resume, |
| 779 | .remove = __devexit_p(msmrtc_remove), |
| 780 | .driver = { |
| 781 | .name = APP_TIMEREMOTE_PDEV_NAME, |
| 782 | .owner = THIS_MODULE, |
| 783 | }, |
| 784 | }; |
| 785 | |
| 786 | static int __init msmrtc_init(void) |
| 787 | { |
| 788 | int rc; |
| 789 | |
| 790 | /* |
| 791 | * For backward compatibility, register multiple platform |
| 792 | * drivers with the RPC PROG_VERS to be supported. |
| 793 | * |
| 794 | * Explicit cast away of 'constness' for driver.name in order to |
| 795 | * initialize it here. |
| 796 | */ |
| 797 | snprintf((char *)msmrtc_driver.driver.name, |
| 798 | strlen(msmrtc_driver.driver.name)+1, |
| 799 | "rs%08x", TIMEREMOTE_PROG_NUMBER); |
| 800 | pr_debug("RTC Registering with %s\n", msmrtc_driver.driver.name); |
| 801 | |
| 802 | rc = platform_driver_register(&msmrtc_driver); |
| 803 | if (rc) |
| 804 | pr_err("%s: platfrom_driver_register failed\n", __func__); |
| 805 | |
| 806 | return rc; |
| 807 | } |
| 808 | |
| 809 | static void __exit msmrtc_exit(void) |
| 810 | { |
| 811 | platform_driver_unregister(&msmrtc_driver); |
| 812 | } |
| 813 | |
| 814 | module_init(msmrtc_init); |
| 815 | module_exit(msmrtc_exit); |
| 816 | |
| 817 | MODULE_DESCRIPTION("RTC driver for Qualcomm MSM7x00a chipsets"); |
| 818 | MODULE_AUTHOR("San Mehat <san@android.com>"); |
| 819 | MODULE_LICENSE("GPL"); |