From 3b4798cbc13dd8d1150aa6377f97f0e11450a67d Mon Sep 17 00:00:00 2001 From: KOSAKI Motohiro Date: Tue, 15 Dec 2009 16:45:32 -0800 Subject: oom-kill: show virtual size and rss information of the killed process In a typical oom analysis scenario, we frequently want to know whether the killed process has a memory leak or not at the first step. This patch adds vsz and rss information to the oom log to help this analysis. To save time for the debugging. example: =================================================================== rsyslogd invoked oom-killer: gfp_mask=0x201da, order=0, oom_adj=0 Pid: 1308, comm: rsyslogd Not tainted 2.6.32-rc6 #24 Call Trace: [] ?_spin_unlock+0x2b/0x40 [] oom_kill_process+0xbe/0x2b0 (snip) 492283 pages non-shared Out of memory: kill process 2341 (memhog) score 527276 or a child Killed process 2341 (memhog) vsz:1054552kB, anon-rss:970588kB, file-rss:4kB =========================================================================== ^ | here [rientjes@google.com: fix race, add pid & comm to message] Signed-off-by: KOSAKI Motohiro Signed-off-by: David Rientjes Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 492c986..6bb8a7a 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -352,6 +352,8 @@ static void dump_header(gfp_t gfp_mask, int order, struct mem_cgroup *mem) dump_tasks(mem); } +#define K(x) ((x) << (PAGE_SHIFT-10)) + /* * Send SIGKILL to the selected process irrespective of CAP_SYS_RAW_IO * flag though it's unlikely that we select a process with CAP_SYS_RAW_IO @@ -365,15 +367,23 @@ static void __oom_kill_task(struct task_struct *p, int verbose) return; } + task_lock(p); if (!p->mm) { WARN_ON(1); - printk(KERN_WARNING "tried to kill an mm-less task!\n"); + printk(KERN_WARNING "tried to kill an mm-less task %d (%s)!\n", + task_pid_nr(p), p->comm); + task_unlock(p); return; } if (verbose) - printk(KERN_ERR "Killed process %d (%s)\n", - task_pid_nr(p), p->comm); + printk(KERN_ERR "Killed process %d (%s) " + "vsz:%lukB, anon-rss:%lukB, file-rss:%lukB\n", + task_pid_nr(p), p->comm, + K(p->mm->total_vm), + K(get_mm_counter(p->mm, anon_rss)), + K(get_mm_counter(p->mm, file_rss))); + task_unlock(p); /* * We give our sacrificial lamb high priority and access to -- cgit v0.10.2 From 4365a5676fa3aa1d5ae6c90c22a0044f09ba584e Mon Sep 17 00:00:00 2001 From: KAMEZAWA Hiroyuki Date: Tue, 15 Dec 2009 16:45:33 -0800 Subject: oom-kill: fix NUMA constraint check with nodemask Fix node-oriented allocation handling in oom-kill.c I myself think of this as a bugfix not as an ehnancement. In these days, things are changed as - alloc_pages() eats nodemask as its arguments, __alloc_pages_nodemask(). - mempolicy don't maintain its own private zonelists. (And cpuset doesn't use nodemask for __alloc_pages_nodemask()) So, current oom-killer's check function is wrong. This patch does - check nodemask, if nodemask && nodemask doesn't cover all node_states[N_HIGH_MEMORY], this is CONSTRAINT_MEMORY_POLICY. - Scan all zonelist under nodemask, if it hits cpuset's wall this faiulre is from cpuset. And - modifies the caller of out_of_memory not to call oom if __GFP_THISNODE. This doesn't change "current" behavior. If callers use __GFP_THISNODE it should handle "page allocation failure" by itself. - handle __GFP_NOFAIL+__GFP_THISNODE path. This is something like a FIXME but this gfpmask is not used now. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: KAMEZAWA Hiroyuki Acked-by: David Rientjes Cc: Daisuke Nishimura Cc: KOSAKI Motohiro Cc: Christoph Lameter Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/char/sysrq.c b/drivers/char/sysrq.c index 44203ff..1ae2de7 100644 --- a/drivers/char/sysrq.c +++ b/drivers/char/sysrq.c @@ -339,7 +339,7 @@ static struct sysrq_key_op sysrq_term_op = { static void moom_callback(struct work_struct *ignored) { - out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0); + out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL); } static DECLARE_WORK(moom_work, moom_callback); diff --git a/include/linux/oom.h b/include/linux/oom.h index 6aac5fe..5376623 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -10,6 +10,7 @@ #ifdef __KERNEL__ #include +#include struct zonelist; struct notifier_block; @@ -26,7 +27,8 @@ enum oom_constraint { extern int try_set_zone_oom(struct zonelist *zonelist, gfp_t gfp_flags); extern void clear_zonelist_oom(struct zonelist *zonelist, gfp_t gfp_flags); -extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order); +extern void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, + int order, nodemask_t *mask); extern int register_oom_notifier(struct notifier_block *nb); extern int unregister_oom_notifier(struct notifier_block *nb); diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 6bb8a7a..25c679e 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -196,27 +196,46 @@ unsigned long badness(struct task_struct *p, unsigned long uptime) /* * Determine the type of allocation constraint. */ -static inline enum oom_constraint constrained_alloc(struct zonelist *zonelist, - gfp_t gfp_mask) -{ #ifdef CONFIG_NUMA +static enum oom_constraint constrained_alloc(struct zonelist *zonelist, + gfp_t gfp_mask, nodemask_t *nodemask) +{ struct zone *zone; struct zoneref *z; enum zone_type high_zoneidx = gfp_zone(gfp_mask); - nodemask_t nodes = node_states[N_HIGH_MEMORY]; - for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) - if (cpuset_zone_allowed_softwall(zone, gfp_mask)) - node_clear(zone_to_nid(zone), nodes); - else - return CONSTRAINT_CPUSET; + /* + * Reach here only when __GFP_NOFAIL is used. So, we should avoid + * to kill current.We have to random task kill in this case. + * Hopefully, CONSTRAINT_THISNODE...but no way to handle it, now. + */ + if (gfp_mask & __GFP_THISNODE) + return CONSTRAINT_NONE; - if (!nodes_empty(nodes)) + /* + * The nodemask here is a nodemask passed to alloc_pages(). Now, + * cpuset doesn't use this nodemask for its hardwall/softwall/hierarchy + * feature. mempolicy is an only user of nodemask here. + * check mempolicy's nodemask contains all N_HIGH_MEMORY + */ + if (nodemask && !nodes_subset(node_states[N_HIGH_MEMORY], *nodemask)) return CONSTRAINT_MEMORY_POLICY; -#endif + + /* Check this allocation failure is caused by cpuset's wall function */ + for_each_zone_zonelist_nodemask(zone, z, zonelist, + high_zoneidx, nodemask) + if (!cpuset_zone_allowed_softwall(zone, gfp_mask)) + return CONSTRAINT_CPUSET; return CONSTRAINT_NONE; } +#else +static enum oom_constraint constrained_alloc(struct zonelist *zonelist, + gfp_t gfp_mask, nodemask_t *nodemask) +{ + return CONSTRAINT_NONE; +} +#endif /* * Simple selection loop. We chose the process with the highest @@ -613,7 +632,8 @@ rest_and_return: * OR try to be smart about which process to kill. Note that we * don't have to be perfect here, we just have to be good. */ -void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order) +void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, + int order, nodemask_t *nodemask) { unsigned long freed = 0; enum oom_constraint constraint; @@ -632,7 +652,7 @@ void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask, int order) * Check if there were limitations on the allocation (only relevant for * NUMA) that may require different handling. */ - constraint = constrained_alloc(zonelist, gfp_mask); + constraint = constrained_alloc(zonelist, gfp_mask, nodemask); read_lock(&tasklist_lock); switch (constraint) { diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 59d2e88..850c4a7 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1654,12 +1654,22 @@ __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, if (page) goto out; - /* The OOM killer will not help higher order allocs */ - if (order > PAGE_ALLOC_COSTLY_ORDER && !(gfp_mask & __GFP_NOFAIL)) - goto out; - + if (!(gfp_mask & __GFP_NOFAIL)) { + /* The OOM killer will not help higher order allocs */ + if (order > PAGE_ALLOC_COSTLY_ORDER) + goto out; + /* + * GFP_THISNODE contains __GFP_NORETRY and we never hit this. + * Sanity check for bare calls of __GFP_THISNODE, not real OOM. + * The caller should handle page allocation failure by itself if + * it specifies __GFP_THISNODE. + * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER. + */ + if (gfp_mask & __GFP_THISNODE) + goto out; + } /* Exhausted what can be done so it's blamo time */ - out_of_memory(zonelist, gfp_mask, order); + out_of_memory(zonelist, gfp_mask, order, nodemask); out: clear_zonelist_oom(zonelist, gfp_mask); @@ -3123,7 +3133,7 @@ static int __cpuinit process_zones(int cpu) if (percpu_pagelist_fraction) setup_pagelist_highmark(zone_pcp(zone, cpu), - (zone->present_pages / percpu_pagelist_fraction)); + (zone->present_pages / percpu_pagelist_fraction)); } return 0; -- cgit v0.10.2 From f065f41f48569122b5bcddbd1ba2354f7cc29fdc Mon Sep 17 00:00:00 2001 From: Barry Song <21cnbao@gmail.com> Date: Tue, 15 Dec 2009 16:45:34 -0800 Subject: timecompare: fix half-Y2K38 problem in timecompare_update while calculating offset ktime will overflow from 03:14:07 UTC on Tuesday, 19 January 2038, ktime_add() in timecompare_update() will overflow a half earlier. As a result, wrong offset will be gotten, then cause some strange problems. Signed-off-by: Barry Song <21cnbao@gmail.com> Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Patrick Ohly Cc: David S. Miller Cc: John Stultz Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/kernel/time/timecompare.c b/kernel/time/timecompare.c index 96ff643..12f5c55 100644 --- a/kernel/time/timecompare.c +++ b/kernel/time/timecompare.c @@ -89,7 +89,7 @@ int timecompare_offset(struct timecompare *sync, * source time */ sample.offset = - ktime_to_ns(ktime_add(end, start)) / 2 - + (ktime_to_ns(end) + ktime_to_ns(start)) / 2 - ts; /* simple insertion sort based on duration */ -- cgit v0.10.2 From 135d5655dc58a24eda64e3f6c192d7d605e10050 Mon Sep 17 00:00:00 2001 From: Alexey Dobriyan Date: Tue, 15 Dec 2009 16:45:39 -0800 Subject: proc: rename de_get() to pde_get() and inline it * de_get() is trivial -- make inline, save a few bits of code, drop "refcount is 0" check -- it should be done in some generic refcount code, don't recall it's was helpful * rename GET and PUT functions to pde_get(), pde_put() for cool prefix! * remove obvious and incorrent comments * in remove_proc_entry() use pde_put(), when I fixed PDE refcounting to be normal one, remove_proc_entry() was supposed to do "-1" and code now reflects that. Signed-off-by: Alexey Dobriyan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/proc/generic.c b/fs/proc/generic.c index fa678ab..480cb10 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -429,7 +429,7 @@ struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, unsigned int ino; ino = de->low_ino; - de_get(de); + pde_get(de); spin_unlock(&proc_subdir_lock); error = -EINVAL; inode = proc_get_inode(dir->i_sb, ino, de); @@ -445,7 +445,7 @@ out_unlock: return NULL; } if (de) - de_put(de); + pde_put(de); return ERR_PTR(error); } @@ -509,17 +509,17 @@ int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, struct proc_dir_entry *next; /* filldir passes info to user space */ - de_get(de); + pde_get(de); spin_unlock(&proc_subdir_lock); if (filldir(dirent, de->name, de->namelen, filp->f_pos, de->low_ino, de->mode >> 12) < 0) { - de_put(de); + pde_put(de); goto out; } spin_lock(&proc_subdir_lock); filp->f_pos++; next = de->next; - de_put(de); + pde_put(de); de = next; } while (de); spin_unlock(&proc_subdir_lock); @@ -763,7 +763,7 @@ out: return NULL; } -void free_proc_entry(struct proc_dir_entry *de) +static void free_proc_entry(struct proc_dir_entry *de) { unsigned int ino = de->low_ino; @@ -777,6 +777,12 @@ void free_proc_entry(struct proc_dir_entry *de) kfree(de); } +void pde_put(struct proc_dir_entry *pde) +{ + if (atomic_dec_and_test(&pde->count)) + free_proc_entry(pde); +} + /* * Remove a /proc entry and free it if it's not currently in use. */ @@ -845,6 +851,5 @@ continue_removing: WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory " "'%s/%s', leaking at least '%s'\n", __func__, de->parent->name, de->name, de->subdir->name); - if (atomic_dec_and_test(&de->count)) - free_proc_entry(de); + pde_put(de); } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index d78ade3..445a02b 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -24,29 +24,6 @@ #include "internal.h" -struct proc_dir_entry *de_get(struct proc_dir_entry *de) -{ - atomic_inc(&de->count); - return de; -} - -/* - * Decrements the use count and checks for deferred deletion. - */ -void de_put(struct proc_dir_entry *de) -{ - if (!atomic_read(&de->count)) { - printk("de_put: entry %s already free!\n", de->name); - return; - } - - if (atomic_dec_and_test(&de->count)) - free_proc_entry(de); -} - -/* - * Decrement the use count of the proc_dir_entry. - */ static void proc_delete_inode(struct inode *inode) { struct proc_dir_entry *de; @@ -59,7 +36,7 @@ static void proc_delete_inode(struct inode *inode) /* Let go of any associated proc directory entry */ de = PROC_I(inode)->pde; if (de) - de_put(de); + pde_put(de); if (PROC_I(inode)->sysctl) sysctl_head_put(PROC_I(inode)->sysctl); clear_inode(inode); @@ -480,7 +457,7 @@ struct inode *proc_get_inode(struct super_block *sb, unsigned int ino, } unlock_new_inode(inode); } else - de_put(de); + pde_put(de); return inode; } @@ -495,7 +472,7 @@ int proc_fill_super(struct super_block *s) s->s_op = &proc_sops; s->s_time_gran = 1; - de_get(&proc_root); + pde_get(&proc_root); root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root); if (!root_inode) goto out_no_root; @@ -509,6 +486,6 @@ int proc_fill_super(struct super_block *s) out_no_root: printk("proc_read_super: get root inode failed\n"); iput(root_inode); - de_put(&proc_root); + pde_put(&proc_root); return -ENOMEM; } diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 753ca37..1f24a3e 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -61,8 +61,6 @@ extern const struct file_operations proc_pagemap_operations; extern const struct file_operations proc_net_operations; extern const struct inode_operations proc_net_inode_operations; -void free_proc_entry(struct proc_dir_entry *de); - void proc_init_inodecache(void); static inline struct pid *proc_pid(struct inode *inode) @@ -101,8 +99,12 @@ unsigned long task_vsize(struct mm_struct *); int task_statm(struct mm_struct *, int *, int *, int *, int *); void task_mem(struct seq_file *, struct mm_struct *); -struct proc_dir_entry *de_get(struct proc_dir_entry *de); -void de_put(struct proc_dir_entry *de); +static inline struct proc_dir_entry *pde_get(struct proc_dir_entry *pde) +{ + atomic_inc(&pde->count); + return pde; +} +void pde_put(struct proc_dir_entry *pde); extern struct vfsmount *proc_mnt; int proc_fill_super(struct super_block *); -- cgit v0.10.2 From 9ea9a886b0e8630e12cff515955e7f0f5be32cb1 Mon Sep 17 00:00:00 2001 From: Clemens Ladisch Date: Tue, 15 Dec 2009 16:45:39 -0800 Subject: vt: make the default cursor shape configurable For embedded systems, the blinking cursor at startup time can be annoying and unintended. Add a new kernel parameter to change the default cursor shape. Signed-off-by: Clemens Ladisch Cc: Daniel Mack Acked-by: Pavel Machek Cc: David Newall Cc: Alan Cox Cc: Greg Kroah-Hartman Cc: Geert Uytterhoeven Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index ab95d3a..c309515 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -2729,6 +2729,11 @@ and is between 256 and 4096 characters. It is defined in the file vmpoff= [KNL,S390] Perform z/VM CP command after power off. Format: + vt.cur_default= [VT] Default cursor shape. + Format: 0xCCBBAA, where AA, BB, and CC are the same as + the parameters of the [?A;B;Cc escape sequence; + see VGA-softcursor.txt. Default: 2 = underline. + vt.default_blu= [VT] Format: ,,,..., Change the default blue palette of the console. diff --git a/drivers/char/vt.c b/drivers/char/vt.c index e43fbc6..50faa1f 100644 --- a/drivers/char/vt.c +++ b/drivers/char/vt.c @@ -164,6 +164,9 @@ module_param(default_utf8, int, S_IRUGO | S_IWUSR); int global_cursor_default = -1; module_param(global_cursor_default, int, S_IRUGO | S_IWUSR); +static int cur_default = CUR_DEFAULT; +module_param(cur_default, int, S_IRUGO | S_IWUSR); + /* * ignore_poke: don't unblank the screen when things are typed. This is * mainly for the privacy of braille terminal users. @@ -1636,7 +1639,7 @@ static void reset_terminal(struct vc_data *vc, int do_clear) /* do not do set_leds here because this causes an endless tasklet loop when the keyboard hasn't been initialized yet */ - vc->vc_cursor_type = CUR_DEFAULT; + vc->vc_cursor_type = cur_default; vc->vc_complement_mask = vc->vc_s_complement_mask; default_attr(vc); @@ -1838,7 +1841,7 @@ static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c) if (vc->vc_par[0]) vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16); else - vc->vc_cursor_type = CUR_DEFAULT; + vc->vc_cursor_type = cur_default; return; } break; -- cgit v0.10.2 From 4f8427d1903148f9753eb35c5f51a8a865457329 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:42 -0800 Subject: autofs4: use helper functions for active list handling Define some simple helper functions for adding and deleting entries on the active (and unhashed) dentry list. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h index 8f7cdde..f3cf151 100644 --- a/fs/autofs4/autofs_i.h +++ b/fs/autofs4/autofs_i.h @@ -75,6 +75,8 @@ struct autofs_info { struct completion expire_complete; struct list_head active; + int active_count; + struct list_head expiring; struct autofs_sb_info *sbi; diff --git a/fs/autofs4/inode.c b/fs/autofs4/inode.c index 69c8142..4670a78 100644 --- a/fs/autofs4/inode.c +++ b/fs/autofs4/inode.c @@ -49,6 +49,7 @@ struct autofs_info *autofs4_init_ino(struct autofs_info *ino, ino->dentry = NULL; ino->size = 0; INIT_LIST_HEAD(&ino->active); + ino->active_count = 0; INIT_LIST_HEAD(&ino->expiring); atomic_set(&ino->count, 0); } diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index b96a3c5..67d8d96 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -72,6 +72,38 @@ const struct inode_operations autofs4_dir_inode_operations = { .rmdir = autofs4_dir_rmdir, }; +static void autofs4_add_active(struct dentry *dentry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + if (ino) { + spin_lock(&sbi->lookup_lock); + if (!ino->active_count) { + if (list_empty(&ino->active)) + list_add(&ino->active, &sbi->active_list); + } + ino->active_count++; + spin_unlock(&sbi->lookup_lock); + } + return; +} + +static void autofs4_del_active(struct dentry *dentry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + if (ino) { + spin_lock(&sbi->lookup_lock); + ino->active_count--; + if (!ino->active_count) { + if (!list_empty(&ino->active)) + list_del_init(&ino->active); + } + spin_unlock(&sbi->lookup_lock); + } + return; +} + static int autofs4_dir_open(struct inode *inode, struct file *file) { struct dentry *dentry = file->f_path.dentry; @@ -513,9 +545,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s dentry->d_fsdata = ino; ino->dentry = dentry; - spin_lock(&sbi->lookup_lock); - list_add(&ino->active, &sbi->active_list); - spin_unlock(&sbi->lookup_lock); + autofs4_add_active(dentry); d_instantiate(dentry, NULL); } @@ -624,10 +654,7 @@ static int autofs4_dir_symlink(struct inode *dir, if (!ino) return -ENOMEM; - spin_lock(&sbi->lookup_lock); - if (!list_empty(&ino->active)) - list_del_init(&ino->active); - spin_unlock(&sbi->lookup_lock); + autofs4_del_active(dentry); ino->size = strlen(symname); cp = kmalloc(ino->size + 1, GFP_KERNEL); @@ -775,10 +802,7 @@ static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) if (!ino) return -ENOMEM; - spin_lock(&sbi->lookup_lock); - if (!list_empty(&ino->active)) - list_del_init(&ino->active); - spin_unlock(&sbi->lookup_lock); + autofs4_del_active(dentry); inode = autofs4_get_inode(dir->i_sb, ino); if (!inode) { -- cgit v0.10.2 From c4cd70b3e3e95cc2201a00edf6deb52327d73c6b Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:43 -0800 Subject: autofs4: use helper functions for expiring list Define some simple helper functions for adding and deleting entries on the expiring dentry list. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h index f3cf151..fe9fc23 100644 --- a/fs/autofs4/autofs_i.h +++ b/fs/autofs4/autofs_i.h @@ -266,5 +266,31 @@ out: return ret; } +static inline void autofs4_add_expiring(struct dentry *dentry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + if (ino) { + spin_lock(&sbi->lookup_lock); + if (list_empty(&ino->expiring)) + list_add(&ino->expiring, &sbi->expiring_list); + spin_unlock(&sbi->lookup_lock); + } + return; +} + +static inline void autofs4_del_expiring(struct dentry *dentry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + if (ino) { + spin_lock(&sbi->lookup_lock); + if (!list_empty(&ino->expiring)) + list_del_init(&ino->expiring); + spin_unlock(&sbi->lookup_lock); + } + return; +} + void autofs4_dentry_release(struct dentry *); extern void autofs4_kill_sb(struct super_block *); diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index 67d8d96..2954ac5 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -563,10 +563,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s */ ino = autofs4_dentry_ino(expiring); autofs4_expire_wait(expiring); - spin_lock(&sbi->lookup_lock); - if (!list_empty(&ino->expiring)) - list_del_init(&ino->expiring); - spin_unlock(&sbi->lookup_lock); + autofs4_del_expiring(expiring); dput(expiring); } @@ -732,10 +729,7 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) dir->i_mtime = CURRENT_TIME; spin_lock(&dcache_lock); - spin_lock(&sbi->lookup_lock); - if (list_empty(&ino->expiring)) - list_add(&ino->expiring, &sbi->expiring_list); - spin_unlock(&sbi->lookup_lock); + autofs4_add_expiring(dentry); spin_lock(&dentry->d_lock); __d_drop(dentry); spin_unlock(&dentry->d_lock); @@ -761,10 +755,7 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) spin_unlock(&dcache_lock); return -ENOTEMPTY; } - spin_lock(&sbi->lookup_lock); - if (list_empty(&ino->expiring)) - list_add(&ino->expiring, &sbi->expiring_list); - spin_unlock(&sbi->lookup_lock); + autofs4_add_expiring(dentry); spin_lock(&dentry->d_lock); __d_drop(dentry); spin_unlock(&dentry->d_lock); -- cgit v0.10.2 From 36b6413ef301d30f60037e497ecb902897895473 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:44 -0800 Subject: autofs4: use helper function for need mount check Define simple helper function for checking if we need to trigger a mount. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index 2954ac5..f6e8ca9 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -104,6 +104,14 @@ static void autofs4_del_active(struct dentry *dentry) return; } +static unsigned int autofs4_need_mount(unsigned int flags) +{ + unsigned int res = 0; + if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS)) + res = 1; + return res; +} + static int autofs4_dir_open(struct inode *inode, struct file *file) { struct dentry *dentry = file->f_path.dentry; @@ -168,7 +176,7 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) } /* Trigger mount for path component or follow link */ } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING || - flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) || + autofs4_need_mount(flags) || current->link_count) { DPRINTK("waiting for mount name=%.*s", dentry->d_name.len, dentry->d_name.name); @@ -234,7 +242,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) autofs4_expire_wait(dentry); /* We trigger a mount for almost all flags */ - lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS); + lookup_type = autofs4_need_mount(nd->flags); if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING)) goto follow; -- cgit v0.10.2 From aa952eb26d4344fdad44c104f3c298d3130c53da Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:45 -0800 Subject: autofs4: use autofs_info for pending flag Eliminate the use of the d_lock spin lock by using the autofs super block info spin lock. This reduces the number of spin locks we use by one and makes the code for the following patch (to redirect ->d_revalidate() to ->lookup()) a little simpler. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h index fe9fc23..3d283ab 100644 --- a/fs/autofs4/autofs_i.h +++ b/fs/autofs4/autofs_i.h @@ -97,6 +97,7 @@ struct autofs_info { #define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */ #define AUTOFS_INF_MOUNTPOINT (1<<1) /* mountpoint status for direct expire */ +#define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */ struct autofs_wait_queue { wait_queue_head_t queue; @@ -163,7 +164,7 @@ static inline int autofs4_ispending(struct dentry *dentry) { struct autofs_info *inf = autofs4_dentry_ino(dentry); - if (dentry->d_flags & DCACHE_AUTOFS_PENDING) + if (inf->flags & AUTOFS_INF_PENDING) return 1; if (inf->flags & AUTOFS_INF_EXPIRING) diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c index 3da18d4..a796c94 100644 --- a/fs/autofs4/expire.c +++ b/fs/autofs4/expire.c @@ -27,7 +27,7 @@ static inline int autofs4_can_expire(struct dentry *dentry, return 0; /* No point expiring a pending mount */ - if (dentry->d_flags & DCACHE_AUTOFS_PENDING) + if (ino->flags & AUTOFS_INF_PENDING) return 0; if (!do_now) { diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index f6e8ca9..305136b 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -166,32 +166,32 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) /* Turn this into a real negative dentry? */ if (status == -ENOENT) { - spin_lock(&dentry->d_lock); - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); return status; } else if (status) { /* Return a negative dentry, but leave it "pending" */ return status; } /* Trigger mount for path component or follow link */ - } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING || + } else if (ino->flags & AUTOFS_INF_PENDING || autofs4_need_mount(flags) || current->link_count) { DPRINTK("waiting for mount name=%.*s", dentry->d_name.len, dentry->d_name.name); - spin_lock(&dentry->d_lock); - dentry->d_flags |= DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags |= AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); status = autofs4_wait(sbi, dentry, NFY_MOUNT); DPRINTK("mount done status=%d", status); if (status) { - spin_lock(&dentry->d_lock); - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); return status; } } @@ -200,9 +200,9 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) if (ino) ino->last_used = jiffies; - spin_lock(&dentry->d_lock); - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); return 0; } @@ -243,18 +243,23 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) /* We trigger a mount for almost all flags */ lookup_type = autofs4_need_mount(nd->flags); - if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING)) + spin_lock(&sbi->fs_lock); + spin_lock(&dcache_lock); + if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) { + spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); goto follow; + } /* * If the dentry contains directories then it is an autofs * multi-mount with no root mount offset. So don't try to * mount it again. */ - spin_lock(&dcache_lock); - if (dentry->d_flags & DCACHE_AUTOFS_PENDING || + if (ino->flags & AUTOFS_INF_PENDING || (!d_mountpoint(dentry) && __simple_empty(dentry))) { spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); status = try_to_fill_dentry(dentry, 0); if (status) @@ -263,6 +268,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) goto follow; } spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); follow: /* * If there is no root mount it must be an autofs @@ -525,9 +531,10 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name); - if (unhashed) + if (unhashed) { dentry = unhashed; - else { + ino = autofs4_dentry_ino(dentry); + } else { /* * Mark the dentry incomplete but don't hash it. We do this * to serialize our inode creation operations (symlink and @@ -569,15 +576,14 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s * be quite complete but the directory has been removed * so it must have been successful, so just wait for it. */ - ino = autofs4_dentry_ino(expiring); autofs4_expire_wait(expiring); autofs4_del_expiring(expiring); dput(expiring); } - spin_lock(&dentry->d_lock); - dentry->d_flags |= DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags |= AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); if (dentry->d_op && dentry->d_op->d_revalidate) (dentry->d_op->d_revalidate)(dentry, nd); mutex_lock(&dir->i_mutex); @@ -587,7 +593,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s * If we are still pending, check if we had to handle * a signal. If so we can force a restart.. */ - if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { + if (ino->flags & AUTOFS_INF_PENDING) { /* See if we were interrupted */ if (signal_pending(current)) { sigset_t *sigset = ¤t->pending.signal; @@ -600,9 +606,9 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s } } if (!oz_mode) { - spin_lock(&dentry->d_lock); - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; - spin_unlock(&dentry->d_lock); + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); } } -- cgit v0.10.2 From 90387c9c1d5787aeb7dfdfc90c8f8aeaeed7ad0e Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:46 -0800 Subject: autofs4: renamer unhashed to active in autofs4_lookup() Rename the variable unhashed to active in autofs4_lookup() to better reflect its usage. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index 305136b..961ff37 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -514,7 +514,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s { struct autofs_sb_info *sbi; struct autofs_info *ino; - struct dentry *expiring, *unhashed; + struct dentry *expiring, *active; int oz_mode; DPRINTK("name = %.*s", @@ -530,9 +530,9 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); - unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name); - if (unhashed) { - dentry = unhashed; + active = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name); + if (active) { + dentry = active; ino = autofs4_dentry_ino(dentry); } else { /* @@ -600,8 +600,8 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s if (sigismember (sigset, SIGKILL) || sigismember (sigset, SIGQUIT) || sigismember (sigset, SIGINT)) { - if (unhashed) - dput(unhashed); + if (active) + dput(active); return ERR_PTR(-ERESTARTNOINTR); } } @@ -633,14 +633,14 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s else dentry = ERR_PTR(-ENOENT); - if (unhashed) - dput(unhashed); + if (active) + dput(active); return dentry; } - if (unhashed) - return unhashed; + if (active) + return active; return NULL; } -- cgit v0.10.2 From 6510c9d8595adcee2b0dc86408bc432a8dd7d652 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:47 -0800 Subject: autofs4: cleanup active and expire lookup The lookup functions for active and expiring dentrys use parameters that can be easily obtained on entry so we change the call to to take just the dentry. This makes the subsequent change, to send all lookups to ->lookup(), a bit cleaner. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index 961ff37..81700f4 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -405,8 +405,11 @@ static const struct dentry_operations autofs4_dentry_operations = { .d_release = autofs4_dentry_release, }; -static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name) +static struct dentry *autofs4_lookup_active(struct dentry *dentry) { + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct dentry *parent = dentry->d_parent; + struct qstr *name = &dentry->d_name; unsigned int len = name->len; unsigned int hash = name->hash; const unsigned char *str = name->name; @@ -457,8 +460,11 @@ next: return NULL; } -static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name) +static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) { + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct dentry *parent = dentry->d_parent; + struct qstr *name = &dentry->d_name; unsigned int len = name->len; unsigned int hash = name->hash; const unsigned char *str = name->name; @@ -530,7 +536,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); - active = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name); + active = autofs4_lookup_active(dentry); if (active) { dentry = active; ino = autofs4_dentry_ino(dentry); @@ -567,9 +573,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s if (!oz_mode) { mutex_unlock(&dir->i_mutex); - expiring = autofs4_lookup_expiring(sbi, - dentry->d_parent, - &dentry->d_name); + expiring = autofs4_lookup_expiring(dentry); if (expiring) { /* * If we are racing with expire the request might not -- cgit v0.10.2 From c42c7f7e698fa888abbd50eb9c8e328fff68914f Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:48 -0800 Subject: autofs4: eliminate d_unhashed in path walk checks We unhash the dentry (in a subsequent patch) in ->d_revalidate() in order to send mount requests to ->lookup(). But then we can not rely on d_unhased() to give reliable results because it may be called at any time by any code path. The d_unhashed() function is used by __simple_empty() in the path walking callbacks but autofs mount point dentrys should have no directories at all so a list_empty() on d_subdirs should be (and is) sufficient. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index 81700f4..b6530f3 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -133,7 +133,7 @@ static int autofs4_dir_open(struct inode *inode, struct file *file) * it. */ spin_lock(&dcache_lock); - if (!d_mountpoint(dentry) && __simple_empty(dentry)) { + if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { spin_unlock(&dcache_lock); return -ENOENT; } @@ -257,7 +257,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) * mount it again. */ if (ino->flags & AUTOFS_INF_PENDING || - (!d_mountpoint(dentry) && __simple_empty(dentry))) { + (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) { spin_unlock(&dcache_lock); spin_unlock(&sbi->fs_lock); @@ -340,8 +340,7 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) /* Check for a non-mountpoint directory with no contents */ spin_lock(&dcache_lock); if (S_ISDIR(dentry->d_inode->i_mode) && - !d_mountpoint(dentry) && - __simple_empty(dentry)) { + !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { DPRINTK("dentry=%p %.*s, emptydir", dentry, dentry->d_name.len, dentry->d_name.name); spin_unlock(&dcache_lock); -- cgit v0.10.2 From e4d5ade7b54cf74efcf53ff3dcb09454c29d70cf Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:49 -0800 Subject: autofs4: rename dentry to active in autofs4_lookup_active() In autofs4_lookup_active() a declaration within the list traversal loop uses a declaration that has the same name as the function parameter. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index b6530f3..e8a8881 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -419,23 +419,23 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry) head = &sbi->active_list; list_for_each(p, head) { struct autofs_info *ino; - struct dentry *dentry; + struct dentry *active; struct qstr *qstr; ino = list_entry(p, struct autofs_info, active); - dentry = ino->dentry; + active = ino->dentry; - spin_lock(&dentry->d_lock); + spin_lock(&active->d_lock); /* Already gone? */ - if (atomic_read(&dentry->d_count) == 0) + if (atomic_read(&active->d_count) == 0) goto next; - qstr = &dentry->d_name; + qstr = &active->d_name; - if (dentry->d_name.hash != hash) + if (active->d_name.hash != hash) goto next; - if (dentry->d_parent != parent) + if (active->d_parent != parent) goto next; if (qstr->len != len) @@ -443,15 +443,15 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry) if (memcmp(qstr->name, str, len)) goto next; - if (d_unhashed(dentry)) { - dget(dentry); - spin_unlock(&dentry->d_lock); + if (d_unhashed(active)) { + dget(active); + spin_unlock(&active->d_lock); spin_unlock(&sbi->lookup_lock); spin_unlock(&dcache_lock); - return dentry; + return active; } next: - spin_unlock(&dentry->d_lock); + spin_unlock(&active->d_lock); } spin_unlock(&sbi->lookup_lock); spin_unlock(&dcache_lock); -- cgit v0.10.2 From cb4b492ac7595aad10756fe0b04691f0965e0cfc Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:50 -0800 Subject: autofs4: rename dentry to expiring in autofs4_lookup_expiring() In autofs4_lookup_expiring() a declaration within the list traversal loop uses a declaration that has the same name as the function parameter. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index e8a8881..a015b49 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -474,23 +474,23 @@ static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) head = &sbi->expiring_list; list_for_each(p, head) { struct autofs_info *ino; - struct dentry *dentry; + struct dentry *expiring; struct qstr *qstr; ino = list_entry(p, struct autofs_info, expiring); - dentry = ino->dentry; + expiring = ino->dentry; - spin_lock(&dentry->d_lock); + spin_lock(&expiring->d_lock); /* Bad luck, we've already been dentry_iput */ - if (!dentry->d_inode) + if (!expiring->d_inode) goto next; - qstr = &dentry->d_name; + qstr = &expiring->d_name; - if (dentry->d_name.hash != hash) + if (expiring->d_name.hash != hash) goto next; - if (dentry->d_parent != parent) + if (expiring->d_parent != parent) goto next; if (qstr->len != len) @@ -498,15 +498,15 @@ static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) if (memcmp(qstr->name, str, len)) goto next; - if (d_unhashed(dentry)) { - dget(dentry); - spin_unlock(&dentry->d_lock); + if (d_unhashed(expiring)) { + dget(expiring); + spin_unlock(&expiring->d_lock); spin_unlock(&sbi->lookup_lock); spin_unlock(&dcache_lock); - return dentry; + return expiring; } next: - spin_unlock(&dentry->d_lock); + spin_unlock(&expiring->d_lock); } spin_unlock(&sbi->lookup_lock); spin_unlock(&dcache_lock); -- cgit v0.10.2 From 213614d583748d00967a91cacd656f417efb36ce Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 15 Dec 2009 16:45:51 -0800 Subject: autofs4: always use lookup for lookup We need to be able to cope with the directory mutex being held during ->d_revalidate() in some cases, but not all cases, and not necessarily by us. Because we need to release the mutex when we call back to the daemon to do perform a mount we must be sure that it is us who holds the mutex so we must redirect mount requests to ->lookup() if the mutex is held. Signed-off-by: Ian Kent Cc: Sage Weil Cc: Al Viro Cc: Andreas Dilger Cc: Christoph Hellwig Cc: Yehuda Saheh Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h index 3d283ab..0118d67 100644 --- a/fs/autofs4/autofs_i.h +++ b/fs/autofs4/autofs_i.h @@ -60,6 +60,11 @@ do { \ current->pid, __func__, ##args); \ } while (0) +struct rehash_entry { + struct task_struct *task; + struct list_head list; +}; + /* Unified info structure. This is pointed to by both the dentry and inode structures. Each file in the filesystem has an instance of this structure. It holds a reference to the dentry, so dentries are never @@ -76,6 +81,7 @@ struct autofs_info { struct list_head active; int active_count; + struct list_head rehash_list; struct list_head expiring; @@ -98,6 +104,7 @@ struct autofs_info { #define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */ #define AUTOFS_INF_MOUNTPOINT (1<<1) /* mountpoint status for direct expire */ #define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */ +#define AUTOFS_INF_REHASH (1<<3) /* dentry in transit to ->lookup() */ struct autofs_wait_queue { wait_queue_head_t queue; diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c index a796c94..74bc9aa 100644 --- a/fs/autofs4/expire.c +++ b/fs/autofs4/expire.c @@ -279,6 +279,7 @@ struct dentry *autofs4_expire_direct(struct super_block *sb, root->d_mounted--; } ino->flags |= AUTOFS_INF_EXPIRING; + autofs4_add_expiring(root); init_completion(&ino->expire_complete); spin_unlock(&sbi->fs_lock); return root; @@ -406,6 +407,7 @@ found: expired, (int)expired->d_name.len, expired->d_name.name); ino = autofs4_dentry_ino(expired); ino->flags |= AUTOFS_INF_EXPIRING; + autofs4_add_expiring(expired); init_completion(&ino->expire_complete); spin_unlock(&sbi->fs_lock); spin_lock(&dcache_lock); @@ -433,7 +435,7 @@ int autofs4_expire_wait(struct dentry *dentry) DPRINTK("expire done status=%d", status); - if (d_unhashed(dentry)) + if (d_unhashed(dentry) && IS_DEADDIR(dentry->d_inode)) return -EAGAIN; return status; @@ -473,6 +475,7 @@ int autofs4_expire_run(struct super_block *sb, spin_lock(&sbi->fs_lock); ino = autofs4_dentry_ino(dentry); ino->flags &= ~AUTOFS_INF_EXPIRING; + autofs4_del_expiring(dentry); complete_all(&ino->expire_complete); spin_unlock(&sbi->fs_lock); @@ -503,6 +506,7 @@ int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt, ino->flags &= ~AUTOFS_INF_MOUNTPOINT; } ino->flags &= ~AUTOFS_INF_EXPIRING; + autofs4_del_expiring(dentry); complete_all(&ino->expire_complete); spin_unlock(&sbi->fs_lock); dput(dentry); diff --git a/fs/autofs4/inode.c b/fs/autofs4/inode.c index 4670a78..d0a3de2 100644 --- a/fs/autofs4/inode.c +++ b/fs/autofs4/inode.c @@ -49,6 +49,7 @@ struct autofs_info *autofs4_init_ino(struct autofs_info *ino, ino->dentry = NULL; ino->size = 0; INIT_LIST_HEAD(&ino->active); + INIT_LIST_HEAD(&ino->rehash_list); ino->active_count = 0; INIT_LIST_HEAD(&ino->expiring); atomic_set(&ino->count, 0); diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c index a015b49..30cc9dd 100644 --- a/fs/autofs4/root.c +++ b/fs/autofs4/root.c @@ -104,6 +104,99 @@ static void autofs4_del_active(struct dentry *dentry) return; } +static void autofs4_add_rehash_entry(struct autofs_info *ino, + struct rehash_entry *entry) +{ + entry->task = current; + INIT_LIST_HEAD(&entry->list); + list_add(&entry->list, &ino->rehash_list); + return; +} + +static void autofs4_remove_rehash_entry(struct autofs_info *ino) +{ + struct list_head *head = &ino->rehash_list; + struct rehash_entry *entry; + list_for_each_entry(entry, head, list) { + if (entry->task == current) { + list_del(&entry->list); + kfree(entry); + break; + } + } + return; +} + +static void autofs4_remove_rehash_entrys(struct autofs_info *ino) +{ + struct autofs_sb_info *sbi = ino->sbi; + struct rehash_entry *entry, *next; + struct list_head *head; + + spin_lock(&sbi->fs_lock); + spin_lock(&sbi->lookup_lock); + if (!(ino->flags & AUTOFS_INF_REHASH)) { + spin_unlock(&sbi->lookup_lock); + spin_unlock(&sbi->fs_lock); + return; + } + ino->flags &= ~AUTOFS_INF_REHASH; + head = &ino->rehash_list; + list_for_each_entry_safe(entry, next, head, list) { + list_del(&entry->list); + kfree(entry); + } + spin_unlock(&sbi->lookup_lock); + spin_unlock(&sbi->fs_lock); + dput(ino->dentry); + + return; +} + +static void autofs4_revalidate_drop(struct dentry *dentry, + struct rehash_entry *entry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + /* + * Add to the active list so we can pick this up in + * ->lookup(). Also add an entry to a rehash list so + * we know when there are no dentrys in flight so we + * know when we can rehash the dentry. + */ + spin_lock(&sbi->lookup_lock); + if (list_empty(&ino->active)) + list_add(&ino->active, &sbi->active_list); + autofs4_add_rehash_entry(ino, entry); + spin_unlock(&sbi->lookup_lock); + if (!(ino->flags & AUTOFS_INF_REHASH)) { + ino->flags |= AUTOFS_INF_REHASH; + dget(dentry); + spin_lock(&dentry->d_lock); + __d_drop(dentry); + spin_unlock(&dentry->d_lock); + } + return; +} + +static void autofs4_revalidate_rehash(struct dentry *dentry) +{ + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + if (ino->flags & AUTOFS_INF_REHASH) { + spin_lock(&sbi->lookup_lock); + autofs4_remove_rehash_entry(ino); + if (list_empty(&ino->rehash_list)) { + spin_unlock(&sbi->lookup_lock); + ino->flags &= ~AUTOFS_INF_REHASH; + d_rehash(dentry); + dput(ino->dentry); + } else + spin_unlock(&sbi->lookup_lock); + } + return; +} + static unsigned int autofs4_need_mount(unsigned int flags) { unsigned int res = 0; @@ -143,7 +236,7 @@ out: return dcache_dir_open(inode, file); } -static int try_to_fill_dentry(struct dentry *dentry, int flags) +static int try_to_fill_dentry(struct dentry *dentry) { struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); struct autofs_info *ino = autofs4_dentry_ino(dentry); @@ -156,55 +249,17 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) * Wait for a pending mount, triggering one if there * isn't one already */ - if (dentry->d_inode == NULL) { - DPRINTK("waiting for mount name=%.*s", - dentry->d_name.len, dentry->d_name.name); + DPRINTK("waiting for mount name=%.*s", + dentry->d_name.len, dentry->d_name.name); - status = autofs4_wait(sbi, dentry, NFY_MOUNT); + status = autofs4_wait(sbi, dentry, NFY_MOUNT); - DPRINTK("mount done status=%d", status); - - /* Turn this into a real negative dentry? */ - if (status == -ENOENT) { - spin_lock(&sbi->fs_lock); - ino->flags &= ~AUTOFS_INF_PENDING; - spin_unlock(&sbi->fs_lock); - return status; - } else if (status) { - /* Return a negative dentry, but leave it "pending" */ - return status; - } - /* Trigger mount for path component or follow link */ - } else if (ino->flags & AUTOFS_INF_PENDING || - autofs4_need_mount(flags) || - current->link_count) { - DPRINTK("waiting for mount name=%.*s", - dentry->d_name.len, dentry->d_name.name); + DPRINTK("mount done status=%d", status); - spin_lock(&sbi->fs_lock); - ino->flags |= AUTOFS_INF_PENDING; - spin_unlock(&sbi->fs_lock); - status = autofs4_wait(sbi, dentry, NFY_MOUNT); + /* Update expiry counter */ + ino->last_used = jiffies; - DPRINTK("mount done status=%d", status); - - if (status) { - spin_lock(&sbi->fs_lock); - ino->flags &= ~AUTOFS_INF_PENDING; - spin_unlock(&sbi->fs_lock); - return status; - } - } - - /* Initialize expiry counter after successful mount */ - if (ino) - ino->last_used = jiffies; - - spin_lock(&sbi->fs_lock); - ino->flags &= ~AUTOFS_INF_PENDING; - spin_unlock(&sbi->fs_lock); - - return 0; + return status; } /* For autofs direct mounts the follow link triggers the mount */ @@ -258,10 +313,16 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) */ if (ino->flags & AUTOFS_INF_PENDING || (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) { + ino->flags |= AUTOFS_INF_PENDING; spin_unlock(&dcache_lock); spin_unlock(&sbi->fs_lock); - status = try_to_fill_dentry(dentry, 0); + status = try_to_fill_dentry(dentry); + + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); + if (status) goto out_error; @@ -300,18 +361,47 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) { struct inode *dir = dentry->d_parent->d_inode; struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); - int oz_mode = autofs4_oz_mode(sbi); + struct autofs_info *ino = autofs4_dentry_ino(dentry); + struct rehash_entry *entry; int flags = nd ? nd->flags : 0; - int status = 1; + unsigned int mutex_aquired; + + DPRINTK("name = %.*s oz_mode = %d", + dentry->d_name.len, dentry->d_name.name, oz_mode); + + /* Daemon never causes a mount to trigger */ + if (autofs4_oz_mode(sbi)) + return 1; + + entry = kmalloc(sizeof(struct rehash_entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + mutex_aquired = mutex_trylock(&dir->i_mutex); - /* Pending dentry */ spin_lock(&sbi->fs_lock); + spin_lock(&dcache_lock); + /* Pending dentry */ if (autofs4_ispending(dentry)) { - /* The daemon never causes a mount to trigger */ - spin_unlock(&sbi->fs_lock); + int status; - if (oz_mode) - return 1; + /* + * We can only unhash and send this to ->lookup() if + * the directory mutex is held over d_revalidate() and + * ->lookup(). This prevents the VFS from incorrectly + * seeing the dentry as non-existent. + */ + ino->flags |= AUTOFS_INF_PENDING; + if (!mutex_aquired) { + autofs4_revalidate_drop(dentry, entry); + spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); + return 0; + } + spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); + mutex_unlock(&dir->i_mutex); + kfree(entry); /* * If the directory has gone away due to an expire @@ -325,45 +415,82 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) * A zero status is success otherwise we have a * negative error code. */ - status = try_to_fill_dentry(dentry, flags); + status = try_to_fill_dentry(dentry); + + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); + if (status == 0) return 1; return status; } - spin_unlock(&sbi->fs_lock); - - /* Negative dentry.. invalidate if "old" */ - if (dentry->d_inode == NULL) - return 0; /* Check for a non-mountpoint directory with no contents */ - spin_lock(&dcache_lock); if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { DPRINTK("dentry=%p %.*s, emptydir", dentry, dentry->d_name.len, dentry->d_name.name); - spin_unlock(&dcache_lock); - /* The daemon never causes a mount to trigger */ - if (oz_mode) - return 1; + if (autofs4_need_mount(flags) || current->link_count) { + int status; - /* - * A zero status is success otherwise we have a - * negative error code. - */ - status = try_to_fill_dentry(dentry, flags); - if (status == 0) - return 1; + /* + * We can only unhash and send this to ->lookup() if + * the directory mutex is held over d_revalidate() and + * ->lookup(). This prevents the VFS from incorrectly + * seeing the dentry as non-existent. + */ + ino->flags |= AUTOFS_INF_PENDING; + if (!mutex_aquired) { + autofs4_revalidate_drop(dentry, entry); + spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); + return 0; + } + spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); + mutex_unlock(&dir->i_mutex); + kfree(entry); - return status; + /* + * A zero status is success otherwise we have a + * negative error code. + */ + status = try_to_fill_dentry(dentry); + + spin_lock(&sbi->fs_lock); + ino->flags &= ~AUTOFS_INF_PENDING; + spin_unlock(&sbi->fs_lock); + + if (status == 0) + return 1; + + return status; + } } spin_unlock(&dcache_lock); + spin_unlock(&sbi->fs_lock); + + if (mutex_aquired) + mutex_unlock(&dir->i_mutex); + + kfree(entry); return 1; } +static void autofs4_free_rehash_entrys(struct autofs_info *inf) +{ + struct list_head *head = &inf->rehash_list; + struct rehash_entry *entry, *next; + list_for_each_entry_safe(entry, next, head, list) { + list_del(&entry->list); + kfree(entry); + } +} + void autofs4_dentry_release(struct dentry *de) { struct autofs_info *inf; @@ -382,6 +509,8 @@ void autofs4_dentry_release(struct dentry *de) list_del(&inf->active); if (!list_empty(&inf->expiring)) list_del(&inf->expiring); + if (!list_empty(&inf->rehash_list)) + autofs4_free_rehash_entrys(inf); spin_unlock(&sbi->lookup_lock); } @@ -414,6 +543,7 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry) const unsigned char *str = name->name; struct list_head *p, *head; +restart: spin_lock(&dcache_lock); spin_lock(&sbi->lookup_lock); head = &sbi->active_list; @@ -431,6 +561,19 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry) if (atomic_read(&active->d_count) == 0) goto next; + if (active->d_inode && IS_DEADDIR(active->d_inode)) { + if (!list_empty(&ino->rehash_list)) { + dget(active); + spin_unlock(&active->d_lock); + spin_unlock(&sbi->lookup_lock); + spin_unlock(&dcache_lock); + autofs4_remove_rehash_entrys(ino); + dput(active); + goto restart; + } + goto next; + } + qstr = &active->d_name; if (active->d_name.hash != hash) @@ -443,13 +586,11 @@ static struct dentry *autofs4_lookup_active(struct dentry *dentry) if (memcmp(qstr->name, str, len)) goto next; - if (d_unhashed(active)) { - dget(active); - spin_unlock(&active->d_lock); - spin_unlock(&sbi->lookup_lock); - spin_unlock(&dcache_lock); - return active; - } + dget(active); + spin_unlock(&active->d_lock); + spin_unlock(&sbi->lookup_lock); + spin_unlock(&dcache_lock); + return active; next: spin_unlock(&active->d_lock); } @@ -498,13 +639,11 @@ static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) if (memcmp(qstr->name, str, len)) goto next; - if (d_unhashed(expiring)) { - dget(expiring); - spin_unlock(&expiring->d_lock); - spin_unlock(&sbi->lookup_lock); - spin_unlock(&dcache_lock); - return expiring; - } + dget(expiring); + spin_unlock(&expiring->d_lock); + spin_unlock(&sbi->lookup_lock); + spin_unlock(&dcache_lock); + return expiring; next: spin_unlock(&expiring->d_lock); } @@ -514,6 +653,48 @@ next: return NULL; } +static struct autofs_info *init_new_dentry(struct autofs_sb_info *sbi, + struct dentry *dentry, int oz_mode) +{ + struct autofs_info *ino; + + /* + * Mark the dentry incomplete but don't hash it. We do this + * to serialize our inode creation operations (symlink and + * mkdir) which prevents deadlock during the callback to + * the daemon. Subsequent user space lookups for the same + * dentry are placed on the wait queue while the daemon + * itself is allowed passage unresticted so the create + * operation itself can then hash the dentry. Finally, + * we check for the hashed dentry and return the newly + * hashed dentry. + */ + dentry->d_op = &autofs4_root_dentry_operations; + + /* + * And we need to ensure that the same dentry is used for + * all following lookup calls until it is hashed so that + * the dentry flags are persistent throughout the request. + */ + ino = autofs4_init_ino(NULL, sbi, 0555); + if (!ino) + return ERR_PTR(-ENOMEM); + + dentry->d_fsdata = ino; + ino->dentry = dentry; + + /* + * Only set the mount pending flag for new dentrys not created + * by the daemon. + */ + if (!oz_mode) + ino->flags |= AUTOFS_INF_PENDING; + + d_instantiate(dentry, NULL); + + return ino; +} + /* Lookups in the root directory */ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) { @@ -521,6 +702,7 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s struct autofs_info *ino; struct dentry *expiring, *active; int oz_mode; + int status = 0; DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name); @@ -535,44 +717,26 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); + spin_lock(&sbi->fs_lock); active = autofs4_lookup_active(dentry); if (active) { dentry = active; ino = autofs4_dentry_ino(dentry); + /* If this came from revalidate, rehash it */ + autofs4_revalidate_rehash(dentry); + spin_unlock(&sbi->fs_lock); } else { - /* - * Mark the dentry incomplete but don't hash it. We do this - * to serialize our inode creation operations (symlink and - * mkdir) which prevents deadlock during the callback to - * the daemon. Subsequent user space lookups for the same - * dentry are placed on the wait queue while the daemon - * itself is allowed passage unresticted so the create - * operation itself can then hash the dentry. Finally, - * we check for the hashed dentry and return the newly - * hashed dentry. - */ - dentry->d_op = &autofs4_root_dentry_operations; - - /* - * And we need to ensure that the same dentry is used for - * all following lookup calls until it is hashed so that - * the dentry flags are persistent throughout the request. - */ - ino = autofs4_init_ino(NULL, sbi, 0555); - if (!ino) - return ERR_PTR(-ENOMEM); - - dentry->d_fsdata = ino; - ino->dentry = dentry; - - autofs4_add_active(dentry); - - d_instantiate(dentry, NULL); + spin_unlock(&sbi->fs_lock); + ino = init_new_dentry(sbi, dentry, oz_mode); + if (IS_ERR(ino)) + return (struct dentry *) ino; } + autofs4_add_active(dentry); + if (!oz_mode) { - mutex_unlock(&dir->i_mutex); expiring = autofs4_lookup_expiring(dentry); + mutex_unlock(&dir->i_mutex); if (expiring) { /* * If we are racing with expire the request might not @@ -580,23 +744,22 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s * so it must have been successful, so just wait for it. */ autofs4_expire_wait(expiring); - autofs4_del_expiring(expiring); dput(expiring); } - + status = try_to_fill_dentry(dentry); + mutex_lock(&dir->i_mutex); spin_lock(&sbi->fs_lock); - ino->flags |= AUTOFS_INF_PENDING; + ino->flags &= ~AUTOFS_INF_PENDING; spin_unlock(&sbi->fs_lock); - if (dentry->d_op && dentry->d_op->d_revalidate) - (dentry->d_op->d_revalidate)(dentry, nd); - mutex_lock(&dir->i_mutex); } + autofs4_del_active(dentry); + /* - * If we are still pending, check if we had to handle + * If we had a mount fail, check if we had to handle * a signal. If so we can force a restart.. */ - if (ino->flags & AUTOFS_INF_PENDING) { + if (status) { /* See if we were interrupted */ if (signal_pending(current)) { sigset_t *sigset = ¤t->pending.signal; @@ -608,43 +771,46 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s return ERR_PTR(-ERESTARTNOINTR); } } - if (!oz_mode) { - spin_lock(&sbi->fs_lock); - ino->flags &= ~AUTOFS_INF_PENDING; - spin_unlock(&sbi->fs_lock); - } } /* - * If this dentry is unhashed, then we shouldn't honour this - * lookup. Returning ENOENT here doesn't do the right thing - * for all system calls, but it should be OK for the operations - * we permit from an autofs. + * User space can (and has done in the past) remove and re-create + * this directory during the callback. This can leave us with an + * unhashed dentry, but a successful mount! So we need to + * perform another cached lookup in case the dentry now exists. */ - if (!oz_mode && d_unhashed(dentry)) { - /* - * A user space application can (and has done in the past) - * remove and re-create this directory during the callback. - * This can leave us with an unhashed dentry, but a - * successful mount! So we need to perform another - * cached lookup in case the dentry now exists. - */ - struct dentry *parent = dentry->d_parent; - struct dentry *new = d_lookup(parent, &dentry->d_name); - if (new != NULL) - dentry = new; - else - dentry = ERR_PTR(-ENOENT); + if (!oz_mode && !have_submounts(dentry)) { + struct dentry *new; + new = d_lookup(dentry->d_parent, &dentry->d_name); + if (new) { + if (active) + dput(active); + return new; + } else { + if (!status) + status = -ENOENT; + } + } + /* + * If we had a mount failure, return status to user space. + * If the mount succeeded and we used a dentry from the active queue + * return it. + */ + if (status) { + dentry = ERR_PTR(status); if (active) dput(active); - return dentry; + } else { + /* + * Valid successful mount, return active dentry or NULL + * for a new dentry. + */ + if (active) + return active; } - if (active) - return active; - return NULL; } @@ -668,8 +834,6 @@ static int autofs4_dir_symlink(struct inode *dir, if (!ino) return -ENOMEM; - autofs4_del_active(dentry); - ino->size = strlen(symname); cp = kmalloc(ino->size + 1, GFP_KERNEL); if (!cp) { @@ -746,7 +910,6 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) dir->i_mtime = CURRENT_TIME; spin_lock(&dcache_lock); - autofs4_add_expiring(dentry); spin_lock(&dentry->d_lock); __d_drop(dentry); spin_unlock(&dentry->d_lock); @@ -772,7 +935,6 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) spin_unlock(&dcache_lock); return -ENOTEMPTY; } - autofs4_add_expiring(dentry); spin_lock(&dentry->d_lock); __d_drop(dentry); spin_unlock(&dentry->d_lock); @@ -810,8 +972,6 @@ static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) if (!ino) return -ENOMEM; - autofs4_del_active(dentry); - inode = autofs4_get_inode(dir->i_sb, ino); if (!inode) { if (!dentry->d_fsdata) -- cgit v0.10.2 From d1b2efa83fbf7b33919238fa29ef6ab935820103 Mon Sep 17 00:00:00 2001 From: Maxim Levitsky Date: Tue, 15 Dec 2009 16:45:52 -0800 Subject: rtc: disable hpet emulation on suspend I noticed that rtc wont generate interrupts after a resume from disk. Here hpet rtc emulation is used. Problem is that rtc hpet comparator, isn't reinitialized after resume. Easiest way to solve this, is always mask all hpet interrupts on suspend This is triggered, when suspending with alarm set. Otherwise, hpet driver will think it doesn't need to reinitialize the rtc comparator, thus rtc interrupts won't work. This emulation isn't need for wakealarm. Signed-off-by: Maxim Levitsky Cc: David Brownell Cc: "H. Peter Anvin" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "Rafael J. Wysocki" Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c index f7a4701..876e3fc 100644 --- a/drivers/rtc/rtc-cmos.c +++ b/drivers/rtc/rtc-cmos.c @@ -871,8 +871,9 @@ static int cmos_suspend(struct device *dev, pm_message_t mesg) mask = RTC_IRQMASK; tmp &= ~mask; CMOS_WRITE(tmp, RTC_CONTROL); - hpet_mask_rtc_irq_bit(mask); + /* shut down hpet emulation - we don't need it for alarm */ + hpet_mask_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE); cmos_checkintr(cmos, tmp); } spin_unlock_irq(&rtc_lock); -- cgit v0.10.2 From b74d2caa64f8e542e9c6716ae6ed4a60d681ea9f Mon Sep 17 00:00:00 2001 From: Alessandro Zummo Date: Tue, 15 Dec 2009 16:45:53 -0800 Subject: rtc: fix driver data issues in several rtc drivers Herton Ronaldo Krzesinski recently raised up, and fixed, an issue with the rtc_cmos driver, which was referring to an inconsistent driver data. This patch ensures that driver data registration happens before rtc_device_register(). Signed-off-by: Alessandro Zummo Acked-by: Thomas Hommel Acked-by: Hans-Christian Egtvedt Acked-by: Paul Mundt Cc: David S. Miller Cc: Thomas Bogendoerfer Cc: Andrew Sharp Cc: Atsushi Nemoto Cc: Alexander Bigga Cc: Thomas Bogendoerfer Cc: Mark Zhan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-at32ap700x.c b/drivers/rtc/rtc-at32ap700x.c index e1ec33e..8825695 100644 --- a/drivers/rtc/rtc-at32ap700x.c +++ b/drivers/rtc/rtc-at32ap700x.c @@ -256,6 +256,8 @@ static int __init at32_rtc_probe(struct platform_device *pdev) goto out_iounmap; } + platform_set_drvdata(pdev, rtc); + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &at32_rtc_ops, THIS_MODULE); if (IS_ERR(rtc->rtc)) { @@ -264,7 +266,6 @@ static int __init at32_rtc_probe(struct platform_device *pdev) goto out_free_irq; } - platform_set_drvdata(pdev, rtc); device_init_wakeup(&pdev->dev, 1); dev_info(&pdev->dev, "Atmel RTC for AT32AP700x at %08lx irq %ld\n", @@ -273,6 +274,7 @@ static int __init at32_rtc_probe(struct platform_device *pdev) return 0; out_free_irq: + platform_set_drvdata(pdev, NULL); free_irq(irq, rtc); out_iounmap: iounmap(rtc->regs); diff --git a/drivers/rtc/rtc-bq4802.c b/drivers/rtc/rtc-bq4802.c index d00a274..280fe48 100644 --- a/drivers/rtc/rtc-bq4802.c +++ b/drivers/rtc/rtc-bq4802.c @@ -169,6 +169,8 @@ static int __devinit bq4802_probe(struct platform_device *pdev) goto out_free; } + platform_set_drvdata(pdev, p); + p->rtc = rtc_device_register("bq4802", &pdev->dev, &bq4802_ops, THIS_MODULE); if (IS_ERR(p->rtc)) { @@ -176,7 +178,6 @@ static int __devinit bq4802_probe(struct platform_device *pdev) goto out_iounmap; } - platform_set_drvdata(pdev, p); err = 0; out: return err; diff --git a/drivers/rtc/rtc-ds1302.c b/drivers/rtc/rtc-ds1302.c index 1e73c8f..532acf9 100644 --- a/drivers/rtc/rtc-ds1302.c +++ b/drivers/rtc/rtc-ds1302.c @@ -143,7 +143,6 @@ static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd, #ifdef RTC_SET_CHARGE case RTC_SET_CHARGE: { - struct ds1302_rtc *rtc = dev_get_drvdata(dev); int tcs_val; if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int))) diff --git a/drivers/rtc/rtc-ds1305.c b/drivers/rtc/rtc-ds1305.c index 2736b11..259db7f 100644 --- a/drivers/rtc/rtc-ds1305.c +++ b/drivers/rtc/rtc-ds1305.c @@ -617,7 +617,6 @@ static struct bin_attribute nvram = { static int __devinit ds1305_probe(struct spi_device *spi) { struct ds1305 *ds1305; - struct rtc_device *rtc; int status; u8 addr, value; struct ds1305_platform_data *pdata = spi->dev.platform_data; @@ -756,14 +755,13 @@ static int __devinit ds1305_probe(struct spi_device *spi) dev_dbg(&spi->dev, "AM/PM\n"); /* register RTC ... from here on, ds1305->ctrl needs locking */ - rtc = rtc_device_register("ds1305", &spi->dev, + ds1305->rtc = rtc_device_register("ds1305", &spi->dev, &ds1305_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - status = PTR_ERR(rtc); + if (IS_ERR(ds1305->rtc)) { + status = PTR_ERR(ds1305->rtc); dev_dbg(&spi->dev, "register rtc --> %d\n", status); goto fail0; } - ds1305->rtc = rtc; /* Maybe set up alarm IRQ; be ready to handle it triggering right * away. NOTE that we don't share this. The signal is active low, @@ -774,7 +772,7 @@ static int __devinit ds1305_probe(struct spi_device *spi) if (spi->irq) { INIT_WORK(&ds1305->work, ds1305_work); status = request_irq(spi->irq, ds1305_irq, - 0, dev_name(&rtc->dev), ds1305); + 0, dev_name(&ds1305->rtc->dev), ds1305); if (status < 0) { dev_dbg(&spi->dev, "request_irq %d --> %d\n", spi->irq, status); @@ -794,7 +792,7 @@ static int __devinit ds1305_probe(struct spi_device *spi) fail2: free_irq(spi->irq, ds1305); fail1: - rtc_device_unregister(rtc); + rtc_device_unregister(ds1305->rtc); fail0: kfree(ds1305); return status; @@ -802,7 +800,7 @@ fail0: static int __devexit ds1305_remove(struct spi_device *spi) { - struct ds1305 *ds1305 = spi_get_drvdata(spi); + struct ds1305 *ds1305 = spi_get_drvdata(spi); sysfs_remove_bin_file(&spi->dev.kobj, &nvram); diff --git a/drivers/rtc/rtc-m48t35.c b/drivers/rtc/rtc-m48t35.c index 0b21975..8cb5b89 100644 --- a/drivers/rtc/rtc-m48t35.c +++ b/drivers/rtc/rtc-m48t35.c @@ -142,7 +142,6 @@ static const struct rtc_class_ops m48t35_ops = { static int __devinit m48t35_probe(struct platform_device *pdev) { - struct rtc_device *rtc; struct resource *res; struct m48t35_priv *priv; int ret = 0; @@ -171,20 +170,21 @@ static int __devinit m48t35_probe(struct platform_device *pdev) ret = -ENOMEM; goto out; } + spin_lock_init(&priv->lock); - rtc = rtc_device_register("m48t35", &pdev->dev, + + platform_set_drvdata(pdev, priv); + + priv->rtc = rtc_device_register("m48t35", &pdev->dev, &m48t35_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); + if (IS_ERR(priv->rtc)) { + ret = PTR_ERR(priv->rtc); goto out; } - priv->rtc = rtc; - platform_set_drvdata(pdev, priv); + return 0; out: - if (priv->rtc) - rtc_device_unregister(priv->rtc); if (priv->reg) iounmap(priv->reg); if (priv->baseaddr) diff --git a/drivers/rtc/rtc-m48t59.c b/drivers/rtc/rtc-m48t59.c index 33921a6..ede43b8 100644 --- a/drivers/rtc/rtc-m48t59.c +++ b/drivers/rtc/rtc-m48t59.c @@ -481,6 +481,9 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev) goto out; } + spin_lock_init(&m48t59->lock); + platform_set_drvdata(pdev, m48t59); + m48t59->rtc = rtc_device_register(name, &pdev->dev, ops, THIS_MODULE); if (IS_ERR(m48t59->rtc)) { ret = PTR_ERR(m48t59->rtc); @@ -490,16 +493,14 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev) m48t59_nvram_attr.size = pdata->offset; ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr); - if (ret) + if (ret) { + rtc_device_unregister(m48t59->rtc); goto out; + } - spin_lock_init(&m48t59->lock); - platform_set_drvdata(pdev, m48t59); return 0; out: - if (!IS_ERR(m48t59->rtc)) - rtc_device_unregister(m48t59->rtc); if (m48t59->irq != NO_IRQ) free_irq(m48t59->irq, &pdev->dev); if (m48t59->ioaddr) diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c index b725913..65f346b 100644 --- a/drivers/rtc/rtc-pcf8563.c +++ b/drivers/rtc/rtc-pcf8563.c @@ -212,6 +212,8 @@ static int pcf8563_probe(struct i2c_client *client, dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); + i2c_set_clientdata(client, pcf8563); + pcf8563->rtc = rtc_device_register(pcf8563_driver.driver.name, &client->dev, &pcf8563_rtc_ops, THIS_MODULE); @@ -220,8 +222,6 @@ static int pcf8563_probe(struct i2c_client *client, goto exit_kfree; } - i2c_set_clientdata(client, pcf8563); - return 0; exit_kfree: diff --git a/drivers/rtc/rtc-pcf8583.c b/drivers/rtc/rtc-pcf8583.c index 7d33cda..2d201af 100644 --- a/drivers/rtc/rtc-pcf8583.c +++ b/drivers/rtc/rtc-pcf8583.c @@ -277,6 +277,8 @@ static int pcf8583_probe(struct i2c_client *client, if (!pcf8583) return -ENOMEM; + i2c_set_clientdata(client, pcf8583); + pcf8583->rtc = rtc_device_register(pcf8583_driver.driver.name, &client->dev, &pcf8583_rtc_ops, THIS_MODULE); @@ -285,7 +287,6 @@ static int pcf8583_probe(struct i2c_client *client, goto exit_kfree; } - i2c_set_clientdata(client, pcf8583); return 0; exit_kfree: diff --git a/drivers/rtc/rtc-stk17ta8.c b/drivers/rtc/rtc-stk17ta8.c index d491eb2..62c2969 100644 --- a/drivers/rtc/rtc-stk17ta8.c +++ b/drivers/rtc/rtc-stk17ta8.c @@ -288,7 +288,6 @@ static struct bin_attribute stk17ta8_nvram_attr = { static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) { - struct rtc_device *rtc; struct resource *res; unsigned int cal; unsigned int flags; @@ -338,22 +337,23 @@ static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) } } - rtc = rtc_device_register(pdev->name, &pdev->dev, + pdata->last_jiffies = jiffies; + platform_set_drvdata(pdev, pdata); + + pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, &stk17ta8_rtc_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); + if (IS_ERR(pdata->rtc)) { + ret = PTR_ERR(pdata->rtc); goto out; } - pdata->rtc = rtc; - pdata->last_jiffies = jiffies; - platform_set_drvdata(pdev, pdata); + ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr); - if (ret) + if (ret) { + rtc_device_unregister(pdata->rtc); goto out; + } return 0; out: - if (pdata->rtc) - rtc_device_unregister(pdata->rtc); if (pdata->irq > 0) free_irq(pdata->irq, pdev); if (ioaddr) diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c index ad741af..bed4cab 100644 --- a/drivers/rtc/rtc-v3020.c +++ b/drivers/rtc/rtc-v3020.c @@ -304,7 +304,6 @@ static int rtc_probe(struct platform_device *pdev) { struct v3020_platform_data *pdata = pdev->dev.platform_data; struct v3020 *chip; - struct rtc_device *rtc; int retval = -EBUSY; int i; int temp; @@ -353,13 +352,12 @@ static int rtc_probe(struct platform_device *pdev) platform_set_drvdata(pdev, chip); - rtc = rtc_device_register("v3020", + chip->rtc = rtc_device_register("v3020", &pdev->dev, &v3020_rtc_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - retval = PTR_ERR(rtc); + if (IS_ERR(chip->rtc)) { + retval = PTR_ERR(chip->rtc); goto err_io; } - chip->rtc = rtc; return 0; -- cgit v0.10.2 From a8462ef63c961639a743f9fcddf408da46641281 Mon Sep 17 00:00:00 2001 From: Herton Ronaldo Krzesinski Date: Tue, 15 Dec 2009 16:45:56 -0800 Subject: rtc-cmos: convert RTC_AIE/RTC_UIE to rtc irq API Drop ioctl function that handles RTC_AIE/RTC_UIE, and use instead the rtc subsystem API (alarm_irq_enable/update_irq_enable callbacks). Signed-off-by: Herton Ronaldo Krzesinski Cc: Alessandro Zummo Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c index 876e3fc..eb154dc 100644 --- a/drivers/rtc/rtc-cmos.c +++ b/drivers/rtc/rtc-cmos.c @@ -420,49 +420,43 @@ static int cmos_irq_set_state(struct device *dev, int enabled) return 0; } -#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE) - -static int -cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) +static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct cmos_rtc *cmos = dev_get_drvdata(dev); unsigned long flags; - switch (cmd) { - case RTC_AIE_OFF: - case RTC_AIE_ON: - case RTC_UIE_OFF: - case RTC_UIE_ON: - if (!is_valid_irq(cmos->irq)) - return -EINVAL; - break; - /* PIE ON/OFF is handled by cmos_irq_set_state() */ - default: - return -ENOIOCTLCMD; - } + if (!is_valid_irq(cmos->irq)) + return -EINVAL; spin_lock_irqsave(&rtc_lock, flags); - switch (cmd) { - case RTC_AIE_OFF: /* alarm off */ - cmos_irq_disable(cmos, RTC_AIE); - break; - case RTC_AIE_ON: /* alarm on */ + + if (enabled) cmos_irq_enable(cmos, RTC_AIE); - break; - case RTC_UIE_OFF: /* update off */ - cmos_irq_disable(cmos, RTC_UIE); - break; - case RTC_UIE_ON: /* update on */ - cmos_irq_enable(cmos, RTC_UIE); - break; - } + else + cmos_irq_disable(cmos, RTC_AIE); + spin_unlock_irqrestore(&rtc_lock, flags); return 0; } -#else -#define cmos_rtc_ioctl NULL -#endif +static int cmos_update_irq_enable(struct device *dev, unsigned int enabled) +{ + struct cmos_rtc *cmos = dev_get_drvdata(dev); + unsigned long flags; + + if (!is_valid_irq(cmos->irq)) + return -EINVAL; + + spin_lock_irqsave(&rtc_lock, flags); + + if (enabled) + cmos_irq_enable(cmos, RTC_UIE); + else + cmos_irq_disable(cmos, RTC_UIE); + + spin_unlock_irqrestore(&rtc_lock, flags); + return 0; +} #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE) @@ -503,14 +497,15 @@ static int cmos_procfs(struct device *dev, struct seq_file *seq) #endif static const struct rtc_class_ops cmos_rtc_ops = { - .ioctl = cmos_rtc_ioctl, - .read_time = cmos_read_time, - .set_time = cmos_set_time, - .read_alarm = cmos_read_alarm, - .set_alarm = cmos_set_alarm, - .proc = cmos_procfs, - .irq_set_freq = cmos_irq_set_freq, - .irq_set_state = cmos_irq_set_state, + .read_time = cmos_read_time, + .set_time = cmos_set_time, + .read_alarm = cmos_read_alarm, + .set_alarm = cmos_set_alarm, + .proc = cmos_procfs, + .irq_set_freq = cmos_irq_set_freq, + .irq_set_state = cmos_irq_set_state, + .alarm_irq_enable = cmos_alarm_irq_enable, + .update_irq_enable = cmos_update_irq_enable, }; /*----------------------------------------------------------------*/ -- cgit v0.10.2 From ba4f3e47cb9ef72f864f1b5548688e0a195011e9 Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:45:58 -0800 Subject: rtc-ds1511: fix races around device registration - Call dev_set_drvdata before rtc device creation. - Use its own spinlock instead of rtc->irq_lock. Because pdata->rtc must be initialized to use the irq_lock (pdata->rtc->irq_lock). There is a small window which rtc methods can be called before pdata->rtc is initialized. And there is no need use the irq_lock to protect hardware registers. The driver's own spinlock shoule be enough. - Check pdata->rtc before calling rtc_update_irq. - Use {alarm,update}_irq_enable and remove ioctl routine. - Use devres APIs and simplify error/remove path. These fixes are ported from ds1553 driver and just compile-tested only. Signed-off-by: Atsushi Nemoto Cc: Andrew Sharp Cc: Thomas Hommel Cc: David Brownell Cc: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-ds1511.c b/drivers/rtc/rtc-ds1511.c index 539676e..4166b84 100644 --- a/drivers/rtc/rtc-ds1511.c +++ b/drivers/rtc/rtc-ds1511.c @@ -87,7 +87,6 @@ enum ds1511reg { struct rtc_plat_data { struct rtc_device *rtc; void __iomem *ioaddr; /* virtual base address */ - unsigned long baseaddr; /* physical base address */ int size; /* amount of memory mapped */ int irq; unsigned int irqen; @@ -95,6 +94,7 @@ struct rtc_plat_data { int alrm_min; int alrm_hour; int alrm_mday; + spinlock_t lock; }; static DEFINE_SPINLOCK(ds1511_lock); @@ -302,7 +302,7 @@ ds1511_rtc_update_alarm(struct rtc_plat_data *pdata) { unsigned long flags; - spin_lock_irqsave(&pdata->rtc->irq_lock, flags); + spin_lock_irqsave(&pdata->lock, flags); rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f, RTC_ALARM_DATE); @@ -317,7 +317,7 @@ ds1511_rtc_update_alarm(struct rtc_plat_data *pdata) RTC_ALARM_SEC); rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD); rtc_read(RTC_CMD1); /* clear interrupts */ - spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags); + spin_unlock_irqrestore(&pdata->lock, flags); } static int @@ -362,61 +362,63 @@ ds1511_interrupt(int irq, void *dev_id) { struct platform_device *pdev = dev_id; struct rtc_plat_data *pdata = platform_get_drvdata(pdev); - unsigned long events = RTC_IRQF; + unsigned long events = 0; + spin_lock(&pdata->lock); /* * read and clear interrupt */ - if (!(rtc_read(RTC_CMD1) & DS1511_IRQF)) { - return IRQ_NONE; - } - if (rtc_read(RTC_ALARM_SEC) & 0x80) { - events |= RTC_UF; - } else { - events |= RTC_AF; - } - rtc_update_irq(pdata->rtc, 1, events); - return IRQ_HANDLED; + if (rtc_read(RTC_CMD1) & DS1511_IRQF) { + events = RTC_IRQF; + if (rtc_read(RTC_ALARM_SEC) & 0x80) + events |= RTC_UF; + else + events |= RTC_AF; + if (likely(pdata->rtc)) + rtc_update_irq(pdata->rtc, 1, events); + } + spin_unlock(&pdata->lock); + return events ? IRQ_HANDLED : IRQ_NONE; } - static int -ds1511_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) +static int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); - if (pdata->irq <= 0) { - return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */ - } - switch (cmd) { - case RTC_AIE_OFF: - pdata->irqen &= ~RTC_AF; - ds1511_rtc_update_alarm(pdata); - break; - case RTC_AIE_ON: + if (pdata->irq <= 0) + return -EINVAL; + if (enabled) pdata->irqen |= RTC_AF; - ds1511_rtc_update_alarm(pdata); - break; - case RTC_UIE_OFF: - pdata->irqen &= ~RTC_UF; - ds1511_rtc_update_alarm(pdata); - break; - case RTC_UIE_ON: + else + pdata->irqen &= ~RTC_AF; + ds1511_rtc_update_alarm(pdata); + return 0; +} + +static int ds1511_rtc_update_irq_enable(struct device *dev, + unsigned int enabled) +{ + struct platform_device *pdev = to_platform_device(dev); + struct rtc_plat_data *pdata = platform_get_drvdata(pdev); + + if (pdata->irq <= 0) + return -EINVAL; + if (enabled) pdata->irqen |= RTC_UF; - ds1511_rtc_update_alarm(pdata); - break; - default: - return -ENOIOCTLCMD; - } + else + pdata->irqen &= ~RTC_UF; + ds1511_rtc_update_alarm(pdata); return 0; } static const struct rtc_class_ops ds1511_rtc_ops = { - .read_time = ds1511_rtc_read_time, - .set_time = ds1511_rtc_set_time, - .read_alarm = ds1511_rtc_read_alarm, - .set_alarm = ds1511_rtc_set_alarm, - .ioctl = ds1511_rtc_ioctl, + .read_time = ds1511_rtc_read_time, + .set_time = ds1511_rtc_set_time, + .read_alarm = ds1511_rtc_read_alarm, + .set_alarm = ds1511_rtc_set_alarm, + .alarm_irq_enable = ds1511_rtc_alarm_irq_enable, + .update_irq_enable = ds1511_rtc_update_irq_enable, }; static ssize_t @@ -492,29 +494,23 @@ ds1511_rtc_probe(struct platform_device *pdev) { struct rtc_device *rtc; struct resource *res; - struct rtc_plat_data *pdata = NULL; + struct rtc_plat_data *pdata; int ret = 0; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) { return -ENODEV; } - pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); - if (!pdata) { + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) return -ENOMEM; - } pdata->size = res->end - res->start + 1; - if (!request_mem_region(res->start, pdata->size, pdev->name)) { - ret = -EBUSY; - goto out; - } - pdata->baseaddr = res->start; - pdata->size = pdata->size; - ds1511_base = ioremap(pdata->baseaddr, pdata->size); - if (!ds1511_base) { - ret = -ENOMEM; - goto out; - } + if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size, + pdev->name)) + return -EBUSY; + ds1511_base = devm_ioremap(&pdev->dev, res->start, pdata->size); + if (!ds1511_base) + return -ENOMEM; pdata->ioaddr = ds1511_base; pdata->irq = platform_get_irq(pdev, 0); @@ -540,13 +536,15 @@ ds1511_rtc_probe(struct platform_device *pdev) dev_warn(&pdev->dev, "voltage-low detected.\n"); } + spin_lock_init(&pdata->lock); + platform_set_drvdata(pdev, pdata); /* * if the platform has an interrupt in mind for this device, * then by all means, set it */ if (pdata->irq > 0) { rtc_read(RTC_CMD1); - if (request_irq(pdata->irq, ds1511_interrupt, + if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt, IRQF_DISABLED | IRQF_SHARED, pdev->name, pdev) < 0) { dev_warn(&pdev->dev, "interrupt not available.\n"); @@ -556,33 +554,13 @@ ds1511_rtc_probe(struct platform_device *pdev) rtc = rtc_device_register(pdev->name, &pdev->dev, &ds1511_rtc_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); - goto out; - } + if (IS_ERR(rtc)) + return PTR_ERR(rtc); pdata->rtc = rtc; - platform_set_drvdata(pdev, pdata); + ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr); - if (ret) { - goto out; - } - return 0; - out: - if (pdata->rtc) { + if (ret) rtc_device_unregister(pdata->rtc); - } - if (pdata->irq > 0) { - free_irq(pdata->irq, pdev); - } - if (ds1511_base) { - iounmap(ds1511_base); - ds1511_base = NULL; - } - if (pdata->baseaddr) { - release_mem_region(pdata->baseaddr, pdata->size); - } - - kfree(pdata); return ret; } @@ -593,19 +571,13 @@ ds1511_rtc_remove(struct platform_device *pdev) sysfs_remove_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr); rtc_device_unregister(pdata->rtc); - pdata->rtc = NULL; if (pdata->irq > 0) { /* * disable the alarm interrupt */ rtc_write(rtc_read(RTC_CMD) & ~RTC_TIE, RTC_CMD); rtc_read(RTC_CMD1); - free_irq(pdata->irq, pdev); } - iounmap(pdata->ioaddr); - ds1511_base = NULL; - release_mem_region(pdata->baseaddr, pdata->size); - kfree(pdata); return 0; } -- cgit v0.10.2 From af69a180e0675ce95842adb519079204ee5647ea Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:46:00 -0800 Subject: rtc-tx4939: fix races around device registration * Use its own spinlock instead of rtc->irq_lock * Check pdata->rtc before calling rtc_update_irq * Disable interrupt after rtc_device_unregister Signed-off-by: Atsushi Nemoto Acked-by: Alessandro Zummo Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c index 4a6ed11..9ee81d8 100644 --- a/drivers/rtc/rtc-tx4939.c +++ b/drivers/rtc/rtc-tx4939.c @@ -17,6 +17,7 @@ struct tx4939rtc_plat_data { struct rtc_device *rtc; struct tx4939_rtc_reg __iomem *rtcreg; + spinlock_t lock; }; static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev) @@ -52,14 +53,14 @@ static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs) buf[3] = secs >> 8; buf[4] = secs >> 16; buf[5] = secs >> 24; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); __raw_writel(0, &rtcreg->adr); for (i = 0; i < 6; i++) __raw_writel(buf[i], &rtcreg->dat); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETTIME | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } @@ -71,18 +72,18 @@ static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm) unsigned long sec; unsigned char buf[6]; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_GETTIME | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); if (ret) { - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } __raw_writel(2, &rtcreg->adr); for (i = 2; i < 6; i++) buf[i] = __raw_readl(&rtcreg->dat); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; rtc_time_to_tm(sec, tm); return rtc_valid_tm(tm); @@ -110,13 +111,13 @@ static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) buf[3] = sec >> 8; buf[4] = sec >> 16; buf[5] = sec >> 24; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); __raw_writel(0, &rtcreg->adr); for (i = 0; i < 6; i++) __raw_writel(buf[i], &rtcreg->dat); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM | (alrm->enabled ? TX4939_RTCCTL_ALME : 0)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } @@ -129,12 +130,12 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) unsigned char buf[6]; u32 ctl; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_GETALARM | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); if (ret) { - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return ret; } __raw_writel(2, &rtcreg->adr); @@ -143,7 +144,7 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) ctl = __raw_readl(&rtcreg->ctl); alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0; alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0; - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; rtc_time_to_tm(sec, &alrm->time); return rtc_valid_tm(&alrm->time); @@ -153,11 +154,11 @@ static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP | (enabled ? TX4939_RTCCTL_ALME : 0)); - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return 0; } @@ -167,13 +168,14 @@ static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id) struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; unsigned long events = RTC_IRQF; - spin_lock(&pdata->rtc->irq_lock); + spin_lock(&pdata->lock); if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) { events |= RTC_AF; tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP); } - spin_unlock(&pdata->rtc->irq_lock); - rtc_update_irq(pdata->rtc, 1, events); + spin_unlock(&pdata->lock); + if (likely(pdata->rtc)) + rtc_update_irq(pdata->rtc, 1, events); return IRQ_HANDLED; } @@ -194,13 +196,13 @@ static ssize_t tx4939_rtc_nvram_read(struct kobject *kobj, struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; ssize_t count; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; count++, size--) { __raw_writel(pos++, &rtcreg->adr); *buf++ = __raw_readl(&rtcreg->dat); } - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return count; } @@ -213,13 +215,13 @@ static ssize_t tx4939_rtc_nvram_write(struct kobject *kobj, struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; ssize_t count; - spin_lock_irq(&pdata->rtc->irq_lock); + spin_lock_irq(&pdata->lock); for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; count++, size--) { __raw_writel(pos++, &rtcreg->adr); __raw_writel(*buf++, &rtcreg->dat); } - spin_unlock_irq(&pdata->rtc->irq_lock); + spin_unlock_irq(&pdata->lock); return count; } @@ -259,6 +261,7 @@ static int __init tx4939_rtc_probe(struct platform_device *pdev) if (!pdata->rtcreg) return -EBUSY; + spin_lock_init(&pdata->lock); tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt, IRQF_DISABLED, pdev->name, &pdev->dev) < 0) @@ -277,14 +280,12 @@ static int __init tx4939_rtc_probe(struct platform_device *pdev) static int __exit tx4939_rtc_remove(struct platform_device *pdev) { struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev); - struct rtc_device *rtc = pdata->rtc; - spin_lock_irq(&rtc->irq_lock); - tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); - spin_unlock_irq(&rtc->irq_lock); sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); - rtc_device_unregister(rtc); - platform_set_drvdata(pdev, NULL); + rtc_device_unregister(pdata->rtc); + spin_lock_irq(&pdata->lock); + tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); + spin_unlock_irq(&pdata->lock); return 0; } -- cgit v0.10.2 From 618161f71cb10ff9bfe74c9bd766faf339f98274 Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:46:01 -0800 Subject: rtc-ds1553: fix races around device registration * Call dev_set_drvdata before rtc device creation * Use its own spinlock instead of rtc->irq_lock * Check pdata->rtc before calling rtc_update_irq * Use {alarm,update}_irq_enable and remove ioctl routine * Use devres APIs and simplify error/remove path Signed-off-by: Atsushi Nemoto Acked-by: Alessandro Zummo Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-ds1553.c b/drivers/rtc/rtc-ds1553.c index 7172885..dadd0f5 100644 --- a/drivers/rtc/rtc-ds1553.c +++ b/drivers/rtc/rtc-ds1553.c @@ -18,7 +18,7 @@ #include #include -#define DRV_VERSION "0.2" +#define DRV_VERSION "0.3" #define RTC_REG_SIZE 0x2000 #define RTC_OFFSET 0x1ff0 @@ -61,7 +61,6 @@ struct rtc_plat_data { struct rtc_device *rtc; void __iomem *ioaddr; - resource_size_t baseaddr; unsigned long last_jiffies; int irq; unsigned int irqen; @@ -69,6 +68,7 @@ struct rtc_plat_data { int alrm_min; int alrm_hour; int alrm_mday; + spinlock_t lock; }; static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm) @@ -139,7 +139,7 @@ static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata) void __iomem *ioaddr = pdata->ioaddr; unsigned long flags; - spin_lock_irqsave(&pdata->rtc->irq_lock, flags); + spin_lock_irqsave(&pdata->lock, flags); writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 0x80 : bin2bcd(pdata->alrm_mday), ioaddr + RTC_DATE_ALARM); @@ -154,7 +154,7 @@ static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata) ioaddr + RTC_SECONDS_ALARM); writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS); readb(ioaddr + RTC_FLAGS); /* clear interrupts */ - spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags); + spin_unlock_irqrestore(&pdata->lock, flags); } static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) @@ -194,56 +194,61 @@ static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id) struct platform_device *pdev = dev_id; struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; - unsigned long events = RTC_IRQF; + unsigned long events = 0; + spin_lock(&pdata->lock); /* read and clear interrupt */ - if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF)) - return IRQ_NONE; - if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) - events |= RTC_UF; - else - events |= RTC_AF; - rtc_update_irq(pdata->rtc, 1, events); - return IRQ_HANDLED; + if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { + events = RTC_IRQF; + if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) + events |= RTC_UF; + else + events |= RTC_AF; + if (likely(pdata->rtc)) + rtc_update_irq(pdata->rtc, 1, events); + } + spin_unlock(&pdata->lock); + return events ? IRQ_HANDLED : IRQ_NONE; } -static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd, - unsigned long arg) +static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) { struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); if (pdata->irq <= 0) - return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */ - switch (cmd) { - case RTC_AIE_OFF: - pdata->irqen &= ~RTC_AF; - ds1553_rtc_update_alarm(pdata); - break; - case RTC_AIE_ON: + return -EINVAL; + if (enabled) pdata->irqen |= RTC_AF; - ds1553_rtc_update_alarm(pdata); - break; - case RTC_UIE_OFF: - pdata->irqen &= ~RTC_UF; - ds1553_rtc_update_alarm(pdata); - break; - case RTC_UIE_ON: + else + pdata->irqen &= ~RTC_AF; + ds1553_rtc_update_alarm(pdata); + return 0; +} + +static int ds1553_rtc_update_irq_enable(struct device *dev, + unsigned int enabled) +{ + struct platform_device *pdev = to_platform_device(dev); + struct rtc_plat_data *pdata = platform_get_drvdata(pdev); + + if (pdata->irq <= 0) + return -EINVAL; + if (enabled) pdata->irqen |= RTC_UF; - ds1553_rtc_update_alarm(pdata); - break; - default: - return -ENOIOCTLCMD; - } + else + pdata->irqen &= ~RTC_UF; + ds1553_rtc_update_alarm(pdata); return 0; } static const struct rtc_class_ops ds1553_rtc_ops = { - .read_time = ds1553_rtc_read_time, - .set_time = ds1553_rtc_set_time, - .read_alarm = ds1553_rtc_read_alarm, - .set_alarm = ds1553_rtc_set_alarm, - .ioctl = ds1553_rtc_ioctl, + .read_time = ds1553_rtc_read_time, + .set_time = ds1553_rtc_set_time, + .read_alarm = ds1553_rtc_read_alarm, + .set_alarm = ds1553_rtc_set_alarm, + .alarm_irq_enable = ds1553_rtc_alarm_irq_enable, + .update_irq_enable = ds1553_rtc_update_irq_enable, }; static ssize_t ds1553_nvram_read(struct kobject *kobj, @@ -291,26 +296,23 @@ static int __devinit ds1553_rtc_probe(struct platform_device *pdev) struct rtc_device *rtc; struct resource *res; unsigned int cen, sec; - struct rtc_plat_data *pdata = NULL; - void __iomem *ioaddr = NULL; + struct rtc_plat_data *pdata; + void __iomem *ioaddr; int ret = 0; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) return -ENODEV; - pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) return -ENOMEM; - if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) { - ret = -EBUSY; - goto out; - } - pdata->baseaddr = res->start; - ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE); - if (!ioaddr) { - ret = -ENOMEM; - goto out; - } + if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE, + pdev->name)) + return -EBUSY; + + ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE); + if (!ioaddr) + return -ENOMEM; pdata->ioaddr = ioaddr; pdata->irq = platform_get_irq(pdev, 0); @@ -326,9 +328,13 @@ static int __devinit ds1553_rtc_probe(struct platform_device *pdev) if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF) dev_warn(&pdev->dev, "voltage-low detected.\n"); + spin_lock_init(&pdata->lock); + pdata->last_jiffies = jiffies; + platform_set_drvdata(pdev, pdata); if (pdata->irq > 0) { writeb(0, ioaddr + RTC_INTERRUPTS); - if (request_irq(pdata->irq, ds1553_rtc_interrupt, + if (devm_request_irq(&pdev->dev, pdata->irq, + ds1553_rtc_interrupt, IRQF_DISABLED, pdev->name, pdev) < 0) { dev_warn(&pdev->dev, "interrupt not available.\n"); pdata->irq = 0; @@ -337,27 +343,13 @@ static int __devinit ds1553_rtc_probe(struct platform_device *pdev) rtc = rtc_device_register(pdev->name, &pdev->dev, &ds1553_rtc_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); - goto out; - } + if (IS_ERR(rtc)) + return PTR_ERR(rtc); pdata->rtc = rtc; - pdata->last_jiffies = jiffies; - platform_set_drvdata(pdev, pdata); + ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); if (ret) - goto out; - return 0; - out: - if (pdata->rtc) - rtc_device_unregister(pdata->rtc); - if (pdata->irq > 0) - free_irq(pdata->irq, pdev); - if (ioaddr) - iounmap(ioaddr); - if (pdata->baseaddr) - release_mem_region(pdata->baseaddr, RTC_REG_SIZE); - kfree(pdata); + rtc_device_unregister(rtc); return ret; } @@ -367,13 +359,8 @@ static int __devexit ds1553_rtc_remove(struct platform_device *pdev) sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr); rtc_device_unregister(pdata->rtc); - if (pdata->irq > 0) { + if (pdata->irq > 0) writeb(0, pdata->ioaddr + RTC_INTERRUPTS); - free_irq(pdata->irq, pdev); - } - iounmap(pdata->ioaddr); - release_mem_region(pdata->baseaddr, RTC_REG_SIZE); - kfree(pdata); return 0; } -- cgit v0.10.2 From ac18eb622f4d8cdd544222e56230db850259cd37 Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:46:02 -0800 Subject: rtc-ds1742: fix races around device registration * Call dev_set_drvdata before rtc device creation * Use devres APIs and simplify error/remove path Signed-off-by: Atsushi Nemoto Acked-by: Alessandro Zummo Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-ds1742.c b/drivers/rtc/rtc-ds1742.c index 0924945..19d2f33 100644 --- a/drivers/rtc/rtc-ds1742.c +++ b/drivers/rtc/rtc-ds1742.c @@ -21,7 +21,7 @@ #include #include -#define DRV_VERSION "0.3" +#define DRV_VERSION "0.4" #define RTC_SIZE 8 @@ -55,7 +55,6 @@ struct rtc_plat_data { void __iomem *ioaddr_rtc; size_t size_nvram; size_t size; - resource_size_t baseaddr; unsigned long last_jiffies; struct bin_attribute nvram_attr; }; @@ -163,27 +162,24 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev) struct rtc_device *rtc; struct resource *res; unsigned int cen, sec; - struct rtc_plat_data *pdata = NULL; - void __iomem *ioaddr = NULL; + struct rtc_plat_data *pdata; + void __iomem *ioaddr; int ret = 0; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) return -ENODEV; - pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) return -ENOMEM; pdata->size = res->end - res->start + 1; - if (!request_mem_region(res->start, pdata->size, pdev->name)) { - ret = -EBUSY; - goto out; - } - pdata->baseaddr = res->start; - ioaddr = ioremap(pdata->baseaddr, pdata->size); - if (!ioaddr) { - ret = -ENOMEM; - goto out; - } + if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size, + pdev->name)) + return -EBUSY; + ioaddr = devm_ioremap(&pdev->dev, res->start, pdata->size); + if (!ioaddr) + return -ENOMEM; + pdata->ioaddr_nvram = ioaddr; pdata->size_nvram = pdata->size - RTC_SIZE; pdata->ioaddr_rtc = ioaddr + pdata->size_nvram; @@ -207,31 +203,19 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev) if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG)) dev_warn(&pdev->dev, "voltage-low detected.\n"); + pdata->last_jiffies = jiffies; + platform_set_drvdata(pdev, pdata); rtc = rtc_device_register(pdev->name, &pdev->dev, &ds1742_rtc_ops, THIS_MODULE); - if (IS_ERR(rtc)) { - ret = PTR_ERR(rtc); - goto out; - } + if (IS_ERR(rtc)) + return PTR_ERR(rtc); pdata->rtc = rtc; - pdata->last_jiffies = jiffies; - platform_set_drvdata(pdev, pdata); ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr); if (ret) { dev_err(&pdev->dev, "creating nvram file in sysfs failed\n"); - goto out; + rtc_device_unregister(rtc); } - - return 0; - out: - if (pdata->rtc) - rtc_device_unregister(pdata->rtc); - if (pdata->ioaddr_nvram) - iounmap(pdata->ioaddr_nvram); - if (pdata->baseaddr) - release_mem_region(pdata->baseaddr, pdata->size); - kfree(pdata); return ret; } @@ -241,9 +225,6 @@ static int __devexit ds1742_rtc_remove(struct platform_device *pdev) sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr); rtc_device_unregister(pdata->rtc); - iounmap(pdata->ioaddr_nvram); - release_mem_region(pdata->baseaddr, pdata->size); - kfree(pdata); return 0; } -- cgit v0.10.2 From 3151520d88b27b9dd4fb1c1f89a94807f0ad7ef1 Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:46:04 -0800 Subject: rtc-stk17ta8: fix races around device registration - Call dev_set_drvdata before rtc device creation. - Use its own spinlock instead of rtc->irq_lock. Because pdata->rtc must be initialized to use the irq_lock (pdata->rtc->irq_lock). There is a small window which rtc methods can be called before pdata->rtc is initialized. And there is no need use the irq_lock to protect hardware registers. The driver's own spinlock shoule be enough. - Check pdata->rtc before calling rtc_update_irq. - Use alarm_irq_enable and remove ioctl routine. - Use devres APIs and simplify error/remove path. These fixes are ported from ds1553 driver and just compile-tested only. Signed-off-by: Atsushi Nemoto Cc: Alessandro Zummo Cc: Thomas Hommel Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-stk17ta8.c b/drivers/rtc/rtc-stk17ta8.c index 62c2969..31ea220 100644 --- a/drivers/rtc/rtc-stk17ta8.c +++ b/drivers/rtc/rtc-stk17ta8.c @@ -62,7 +62,6 @@ struct rtc_plat_data { struct rtc_device *rtc; void __iomem *ioaddr; - unsigned long baseaddr; unsigned long last_jiffies; int irq; unsigned int irqen; @@ -70,6 +69,7 @@ struct rtc_plat_data { int alrm_min; int alrm_hour; int alrm_mday; + spinlock_t lock; }; static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm) @@ -142,7 +142,7 @@ static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata) unsigned long irqflags; u8 flags; - spin_lock_irqsave(&pdata->rtc->irq_lock, irqflags); + spin_lock_irqsave(&pdata->lock, irqflags); flags = readb(ioaddr + RTC_FLAGS); writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); @@ -162,7 +162,7 @@ static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata) writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS); readb(ioaddr + RTC_FLAGS); /* clear interrupts */ writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS); - spin_unlock_irqrestore(&pdata->rtc->irq_lock, irqflags); + spin_unlock_irqrestore(&pdata->lock, irqflags); } static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) @@ -202,48 +202,45 @@ static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id) struct platform_device *pdev = dev_id; struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; - unsigned long events = RTC_IRQF; + unsigned long events = 0; + spin_lock(&pdata->lock); /* read and clear interrupt */ - if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF)) - return IRQ_NONE; - if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) - events |= RTC_UF; - else - events |= RTC_AF; - rtc_update_irq(pdata->rtc, 1, events); - return IRQ_HANDLED; + if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { + events = RTC_IRQF; + if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) + events |= RTC_UF; + else + events |= RTC_AF; + if (likely(pdata->rtc)) + rtc_update_irq(pdata->rtc, 1, events); + } + spin_unlock(&pdata->lock); + return events ? IRQ_HANDLED : IRQ_NONE; } -static int stk17ta8_rtc_ioctl(struct device *dev, unsigned int cmd, - unsigned long arg) +static int stk17ta8_rtc_alarm_irq_enable(struct device *dev, + unsigned int enabled) { struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); if (pdata->irq <= 0) - return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */ - switch (cmd) { - case RTC_AIE_OFF: - pdata->irqen &= ~RTC_AF; - stk17ta8_rtc_update_alarm(pdata); - break; - case RTC_AIE_ON: + return -EINVAL; + if (enabled) pdata->irqen |= RTC_AF; - stk17ta8_rtc_update_alarm(pdata); - break; - default: - return -ENOIOCTLCMD; - } + else + pdata->irqen &= ~RTC_AF; + stk17ta8_rtc_update_alarm(pdata); return 0; } static const struct rtc_class_ops stk17ta8_rtc_ops = { - .read_time = stk17ta8_rtc_read_time, - .set_time = stk17ta8_rtc_set_time, - .read_alarm = stk17ta8_rtc_read_alarm, - .set_alarm = stk17ta8_rtc_set_alarm, - .ioctl = stk17ta8_rtc_ioctl, + .read_time = stk17ta8_rtc_read_time, + .set_time = stk17ta8_rtc_set_time, + .read_alarm = stk17ta8_rtc_read_alarm, + .set_alarm = stk17ta8_rtc_set_alarm, + .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable, }; static ssize_t stk17ta8_nvram_read(struct kobject *kobj, @@ -292,26 +289,22 @@ static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) unsigned int cal; unsigned int flags; struct rtc_plat_data *pdata; - void __iomem *ioaddr = NULL; + void __iomem *ioaddr; int ret = 0; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) return -ENODEV; - pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); + pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) return -ENOMEM; - if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) { - ret = -EBUSY; - goto out; - } - pdata->baseaddr = res->start; - ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE); - if (!ioaddr) { - ret = -ENOMEM; - goto out; - } + if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE, + pdev->name)) + return -EBUSY; + ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE); + if (!ioaddr) + return -ENOMEM; pdata->ioaddr = ioaddr; pdata->irq = platform_get_irq(pdev, 0); @@ -327,9 +320,13 @@ static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF) dev_warn(&pdev->dev, "voltage-low detected.\n"); + spin_lock_init(&pdata->lock); + pdata->last_jiffies = jiffies; + platform_set_drvdata(pdev, pdata); if (pdata->irq > 0) { writeb(0, ioaddr + RTC_INTERRUPTS); - if (request_irq(pdata->irq, stk17ta8_rtc_interrupt, + if (devm_request_irq(&pdev->dev, pdata->irq, + stk17ta8_rtc_interrupt, IRQF_DISABLED | IRQF_SHARED, pdev->name, pdev) < 0) { dev_warn(&pdev->dev, "interrupt not available.\n"); @@ -337,30 +334,14 @@ static int __devinit stk17ta8_rtc_probe(struct platform_device *pdev) } } - pdata->last_jiffies = jiffies; - platform_set_drvdata(pdev, pdata); - pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, &stk17ta8_rtc_ops, THIS_MODULE); - if (IS_ERR(pdata->rtc)) { - ret = PTR_ERR(pdata->rtc); - goto out; - } + if (IS_ERR(pdata->rtc)) + return PTR_ERR(pdata->rtc); ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr); - if (ret) { + if (ret) rtc_device_unregister(pdata->rtc); - goto out; - } - return 0; - out: - if (pdata->irq > 0) - free_irq(pdata->irq, pdev); - if (ioaddr) - iounmap(ioaddr); - if (pdata->baseaddr) - release_mem_region(pdata->baseaddr, RTC_REG_SIZE); - kfree(pdata); return ret; } @@ -370,13 +351,8 @@ static int __devexit stk17ta8_rtc_remove(struct platform_device *pdev) sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr); rtc_device_unregister(pdata->rtc); - if (pdata->irq > 0) { + if (pdata->irq > 0) writeb(0, pdata->ioaddr + RTC_INTERRUPTS); - free_irq(pdata->irq, pdev); - } - iounmap(pdata->ioaddr); - release_mem_region(pdata->baseaddr, RTC_REG_SIZE); - kfree(pdata); return 0; } -- cgit v0.10.2 From 50e49bee3fc5c0cd0aff80ebd8061f356b7b9bd8 Mon Sep 17 00:00:00 2001 From: Atsushi Nemoto Date: Tue, 15 Dec 2009 16:46:04 -0800 Subject: rtc: do not use container_of macro as an argument for to_platform_device The to_platform_device macro itself uses container_of macro. Nested use of container_of macro causes following sparse warnings: rtc-ds1553.c:259:3: warning: symbol '__mptr' shadows an earlier one rtc-ds1553.c:259:3: originally declared here Signed-off-by: Atsushi Nemoto Cc: Alessandro Zummo Cc: David Brownell Cc: Andrew Sharp Cc: Thomas Hommel Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-ds1553.c b/drivers/rtc/rtc-ds1553.c index dadd0f5..ed1ef7c 100644 --- a/drivers/rtc/rtc-ds1553.c +++ b/drivers/rtc/rtc-ds1553.c @@ -255,8 +255,8 @@ static ssize_t ds1553_nvram_read(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; ssize_t count; @@ -270,8 +270,8 @@ static ssize_t ds1553_nvram_write(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; ssize_t count; diff --git a/drivers/rtc/rtc-ds1742.c b/drivers/rtc/rtc-ds1742.c index 19d2f33..a127336 100644 --- a/drivers/rtc/rtc-ds1742.c +++ b/drivers/rtc/rtc-ds1742.c @@ -131,8 +131,8 @@ static ssize_t ds1742_nvram_read(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr_nvram; ssize_t count; @@ -146,8 +146,8 @@ static ssize_t ds1742_nvram_write(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr_nvram; ssize_t count; diff --git a/drivers/rtc/rtc-stk17ta8.c b/drivers/rtc/rtc-stk17ta8.c index 31ea220..6770083 100644 --- a/drivers/rtc/rtc-stk17ta8.c +++ b/drivers/rtc/rtc-stk17ta8.c @@ -247,8 +247,8 @@ static ssize_t stk17ta8_nvram_read(struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; ssize_t count; @@ -262,8 +262,8 @@ static ssize_t stk17ta8_nvram_write(struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t pos, size_t size) { - struct platform_device *pdev = - to_platform_device(container_of(kobj, struct device, kobj)); + struct device *dev = container_of(kobj, struct device, kobj); + struct platform_device *pdev = to_platform_device(dev); struct rtc_plat_data *pdata = platform_get_drvdata(pdev); void __iomem *ioaddr = pdata->ioaddr; ssize_t count; -- cgit v0.10.2 From f29627c2a3df5732af84bc217df26d1c311f5112 Mon Sep 17 00:00:00 2001 From: John Kacur Date: Tue, 15 Dec 2009 16:46:06 -0800 Subject: efirtc: explicitly set llseek to no_llseek Now that we've removed the BKL here, let's explicitly set llseek to no_llseek since the default llseek is not used here. The default_llseek function still contains the BKL. When we are auditing code to see if we can remove the BKL, this is one of the hidden considerations we need to take into account. i.e., is there syncronization between code that has the BKL and llseek. At the same time we remove the BKL it would be a good idea to do indicate when no llseek function is required, so we don't have to revisit this code again, when we are trying to determine if we can remove the BKL from the default_llseek. Signed-off-by: John Kacur Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/char/efirtc.c b/drivers/char/efirtc.c index 26a47dc..53c524e 100644 --- a/drivers/char/efirtc.c +++ b/drivers/char/efirtc.c @@ -285,6 +285,7 @@ static const struct file_operations efi_rtc_fops = { .unlocked_ioctl = efi_rtc_ioctl, .open = efi_rtc_open, .release = efi_rtc_close, + .llseek = no_llseek, }; static struct miscdevice efi_rtc_dev= { -- cgit v0.10.2 From a766ae3ebd3575cf8c6b9a39fd5aa66856a578b4 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 15 Dec 2009 16:46:07 -0800 Subject: rtc: pcf50633: manage RTC alarm "pending" flag Add setting and clearing of the "pending" flag of the RTC alarm. The semantics follow the UEFI specification 2.2 available at http://www.uefi.org/specs/, i.e., the "pending" flag is cleared by disabling the alarm, but not by any other condition (such as the passing of time, a successful wakeup, or setting of a new alarm.) Signed-off-by: Werner Almesberger Signed-off-by: Paul Fertser Cc: Alessandro Zummo Cc: Paul Gortmaker Cc: Balaji Rao Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-pcf50633.c b/drivers/rtc/rtc-pcf50633.c index 9b74e9c..854c3cb 100644 --- a/drivers/rtc/rtc-pcf50633.c +++ b/drivers/rtc/rtc-pcf50633.c @@ -58,6 +58,7 @@ struct pcf50633_time { struct pcf50633_rtc { int alarm_enabled; int second_enabled; + int alarm_pending; struct pcf50633 *pcf; struct rtc_device *rtc_dev; @@ -209,6 +210,7 @@ static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) rtc = dev_get_drvdata(dev); alrm->enabled = rtc->alarm_enabled; + alrm->pending = rtc->alarm_pending; ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA, PCF50633_TI_EXTENT, &pcf_tm.time[0]); @@ -244,6 +246,8 @@ static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) /* Returns 0 on success */ ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA, PCF50633_TI_EXTENT, &pcf_tm.time[0]); + if (!alrm->enabled) + rtc->alarm_pending = 0; if (!alarm_masked || alrm->enabled) pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM); @@ -268,6 +272,7 @@ static void pcf50633_rtc_irq(int irq, void *data) switch (irq) { case PCF50633_IRQ_ALARM: rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); + rtc->alarm_pending = 1; break; case PCF50633_IRQ_SECOND: rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF); -- cgit v0.10.2 From aeedacaeaf9c02dddfeb6af87bef80c96f9050cb Mon Sep 17 00:00:00 2001 From: Saeed Bishara Date: Tue, 15 Dec 2009 16:46:08 -0800 Subject: rtc-mv: add support for Alarm This patch adds the Alarm support, this mode enabled when adding IORESOURCE_IRQ to the platform device resources. The patch also enables the wakeup mode, so the wakealarm sysfs file (under /sys/class/rtc/rtcX/) can be used to configure the alarm clock. Signed-off-by: Saeed Bishara Signed-off-by: Nicolas Pitre Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-mv.c b/drivers/rtc/rtc-mv.c index e0263d2..dc052ce 100644 --- a/drivers/rtc/rtc-mv.c +++ b/drivers/rtc/rtc-mv.c @@ -27,10 +27,17 @@ #define RTC_MONTH_OFFS 8 #define RTC_YEAR_OFFS 16 +#define RTC_ALARM_TIME_REG_OFFS 8 +#define RTC_ALARM_DATE_REG_OFFS 0xc +#define RTC_ALARM_VALID (1 << 7) + +#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10 +#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14 struct rtc_plat_data { struct rtc_device *rtc; void __iomem *ioaddr; + int irq; }; static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm) @@ -84,12 +91,134 @@ static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm) return rtc_valid_tm(tm); } +static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) +{ + struct rtc_plat_data *pdata = dev_get_drvdata(dev); + void __iomem *ioaddr = pdata->ioaddr; + u32 rtc_time, rtc_date; + unsigned int year, month, day, hour, minute, second, wday; + + rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS); + rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS); + + second = rtc_time & 0x7f; + minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f; + hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */ + wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7; + + day = rtc_date & 0x3f; + month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f; + year = (rtc_date >> RTC_YEAR_OFFS) & 0xff; + + alm->time.tm_sec = bcd2bin(second); + alm->time.tm_min = bcd2bin(minute); + alm->time.tm_hour = bcd2bin(hour); + alm->time.tm_mday = bcd2bin(day); + alm->time.tm_wday = bcd2bin(wday); + alm->time.tm_mon = bcd2bin(month) - 1; + /* hw counts from year 2000, but tm_year is relative to 1900 */ + alm->time.tm_year = bcd2bin(year) + 100; + + if (rtc_valid_tm(&alm->time) < 0) { + dev_err(dev, "retrieved alarm date/time is not valid.\n"); + rtc_time_to_tm(0, &alm->time); + } + + alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); + return 0; +} + +static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) +{ + struct rtc_plat_data *pdata = dev_get_drvdata(dev); + void __iomem *ioaddr = pdata->ioaddr; + u32 rtc_reg = 0; + + if (alm->time.tm_sec >= 0) + rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec)) + << RTC_SECONDS_OFFS; + if (alm->time.tm_min >= 0) + rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min)) + << RTC_MINUTES_OFFS; + if (alm->time.tm_hour >= 0) + rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour)) + << RTC_HOURS_OFFS; + + writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS); + + if (alm->time.tm_mday >= 0) + rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday)) + << RTC_MDAY_OFFS; + else + rtc_reg = 0; + + if (alm->time.tm_mon >= 0) + rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1)) + << RTC_MONTH_OFFS; + + if (alm->time.tm_year >= 0) + rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100)) + << RTC_YEAR_OFFS; + + writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS); + writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS); + writel(alm->enabled ? 1 : 0, + ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); + + return 0; +} + +static int mv_rtc_ioctl(struct device *dev, unsigned int cmd, + unsigned long arg) +{ + struct platform_device *pdev = to_platform_device(dev); + struct rtc_plat_data *pdata = platform_get_drvdata(pdev); + void __iomem *ioaddr = pdata->ioaddr; + + if (pdata->irq < 0) + return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */ + switch (cmd) { + case RTC_AIE_OFF: + writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); + break; + case RTC_AIE_ON: + writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); + break; + default: + return -ENOIOCTLCMD; + } + return 0; +} + +static irqreturn_t mv_rtc_interrupt(int irq, void *data) +{ + struct rtc_plat_data *pdata = data; + void __iomem *ioaddr = pdata->ioaddr; + + /* alarm irq? */ + if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS)) + return IRQ_NONE; + + /* clear interrupt */ + writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS); + rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF); + return IRQ_HANDLED; +} + static const struct rtc_class_ops mv_rtc_ops = { .read_time = mv_rtc_read_time, .set_time = mv_rtc_set_time, }; -static int __init mv_rtc_probe(struct platform_device *pdev) +static const struct rtc_class_ops mv_rtc_alarm_ops = { + .read_time = mv_rtc_read_time, + .set_time = mv_rtc_set_time, + .read_alarm = mv_rtc_read_alarm, + .set_alarm = mv_rtc_set_alarm, + .ioctl = mv_rtc_ioctl, +}; + +static int __devinit mv_rtc_probe(struct platform_device *pdev) { struct resource *res; struct rtc_plat_data *pdata; @@ -130,12 +259,31 @@ static int __init mv_rtc_probe(struct platform_device *pdev) } } + pdata->irq = platform_get_irq(pdev, 0); + platform_set_drvdata(pdev, pdata); - pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, - &mv_rtc_ops, THIS_MODULE); + + if (pdata->irq >= 0) { + device_init_wakeup(&pdev->dev, 1); + pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, + &mv_rtc_alarm_ops, + THIS_MODULE); + } else + pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, + &mv_rtc_ops, THIS_MODULE); if (IS_ERR(pdata->rtc)) return PTR_ERR(pdata->rtc); + if (pdata->irq >= 0) { + writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); + if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt, + IRQF_DISABLED | IRQF_SHARED, + pdev->name, pdata) < 0) { + dev_warn(&pdev->dev, "interrupt not available.\n"); + pdata->irq = -1; + } + } + return 0; } @@ -143,6 +291,9 @@ static int __exit mv_rtc_remove(struct platform_device *pdev) { struct rtc_plat_data *pdata = platform_get_drvdata(pdev); + if (pdata->irq >= 0) + device_init_wakeup(&pdev->dev, 0); + rtc_device_unregister(pdata->rtc); return 0; } -- cgit v0.10.2 From 6f38b0436f7f0f0626d1f078edf4c38b0802b8f8 Mon Sep 17 00:00:00 2001 From: Mark Brown Date: Tue, 15 Dec 2009 16:46:09 -0800 Subject: rtc: convert WM8350 RTC driver to dev_pm_ops Convert WM8350 RTC driver to dev_pm_ops Signed-off-by: Mark Brown Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-wm8350.c b/drivers/rtc/rtc-wm8350.c index f164866..f1e44052 100644 --- a/drivers/rtc/rtc-wm8350.c +++ b/drivers/rtc/rtc-wm8350.c @@ -354,8 +354,9 @@ static const struct rtc_class_ops wm8350_rtc_ops = { }; #ifdef CONFIG_PM -static int wm8350_rtc_suspend(struct platform_device *pdev, pm_message_t state) +static int wm8350_rtc_suspend(struct device *dev) { + struct platform_device *pdev = to_platform_device(dev); struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); int ret = 0; u16 reg; @@ -373,8 +374,9 @@ static int wm8350_rtc_suspend(struct platform_device *pdev, pm_message_t state) return ret; } -static int wm8350_rtc_resume(struct platform_device *pdev) +static int wm8350_rtc_resume(struct device *dev) { + struct platform_device *pdev = to_platform_device(dev); struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); int ret; @@ -484,13 +486,17 @@ static int __devexit wm8350_rtc_remove(struct platform_device *pdev) return 0; } +static struct dev_pm_ops wm8350_rtc_pm_ops = { + .suspend = wm8350_rtc_suspend, + .resume = wm8350_rtc_resume, +}; + static struct platform_driver wm8350_rtc_driver = { .probe = wm8350_rtc_probe, .remove = __devexit_p(wm8350_rtc_remove), - .suspend = wm8350_rtc_suspend, - .resume = wm8350_rtc_resume, .driver = { .name = "wm8350-rtc", + .pm = &wm8350_rtc_pm_ops, }, }; -- cgit v0.10.2 From 43299f285937c907abcdd987c670c755194943cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= Date: Tue, 15 Dec 2009 16:46:09 -0800 Subject: rtc: add Freescale MC13783 RTC driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This driver provides support for the RTC part integrated into the Freescale MC13783 PMIC and bases on patch created earlier by Sascha Hauer. Signed-off-by: Sascha Hauer Signed-off-by: Uwe Kleine-König Acked-by: Valentin Longchamp Cc: Paul Gortmaker Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 71fbd6e..564f3d1 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -846,4 +846,10 @@ config RTC_DRV_PCAP If you say Y here you will get support for the RTC found on the PCAP2 ASIC used on some Motorola phones. +config RTC_DRV_MC13783 + depends on MFD_MC13783 + tristate "Freescale MC13783 RTC" + help + This enables support for the Freescale MC13783 PMIC RTC + endif # RTC_CLASS diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 7da6efb..6fa20f4 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -52,6 +52,7 @@ obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o obj-$(CONFIG_RTC_MXC) += rtc-mxc.o obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o +obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o diff --git a/drivers/rtc/rtc-mc13783.c b/drivers/rtc/rtc-mc13783.c new file mode 100644 index 0000000..850f983 --- /dev/null +++ b/drivers/rtc/rtc-mc13783.c @@ -0,0 +1,262 @@ +/* + * Real Time Clock driver for Freescale MC13783 PMIC + * + * (C) 2009 Sascha Hauer, Pengutronix + * (C) 2009 Uwe Kleine-Koenig, Pengutronix + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include + +#define DRIVER_NAME "mc13783-rtc" + +#define MC13783_RTCTOD 20 +#define MC13783_RTCTODA 21 +#define MC13783_RTCDAY 22 +#define MC13783_RTCDAYA 23 + +struct mc13783_rtc { + struct rtc_device *rtc; + struct mc13783 *mc13783; + int valid; +}; + +static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct mc13783_rtc *priv = dev_get_drvdata(dev); + unsigned int seconds, days1, days2; + unsigned long s1970; + int ret; + + mc13783_lock(priv->mc13783); + + if (!priv->valid) { + ret = -ENODATA; + goto out; + } + + ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1); + if (unlikely(ret)) + goto out; + + ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds); + if (unlikely(ret)) + goto out; + + ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2); +out: + mc13783_unlock(priv->mc13783); + + if (ret) + return ret; + + if (days2 == days1 + 1) { + if (seconds >= 86400 / 2) + days2 = days1; + else + days1 = days2; + } + + if (days1 != days2) + return -EIO; + + s1970 = days1 * 86400 + seconds; + + rtc_time_to_tm(s1970, tm); + + return rtc_valid_tm(tm); +} + +static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs) +{ + struct mc13783_rtc *priv = dev_get_drvdata(dev); + unsigned int seconds, days; + int ret; + + seconds = secs % 86400; + days = secs / 86400; + + mc13783_lock(priv->mc13783); + + /* + * first write seconds=0 to prevent a day switch between writing days + * and seconds below + */ + ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0); + if (unlikely(ret)) + goto out; + + ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days); + if (unlikely(ret)) + goto out; + + ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds); + if (unlikely(ret)) + goto out; + + ret = mc13783_ackirq(priv->mc13783, MC13783_IRQ_RTCRST); + if (unlikely(ret)) + goto out; + + ret = mc13783_unmask(priv->mc13783, MC13783_IRQ_RTCRST); +out: + priv->valid = !ret; + + mc13783_unlock(priv->mc13783); + + return ret; +} + +static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev) +{ + struct mc13783_rtc *priv = dev; + struct mc13783 *mc13783 = priv->mc13783; + + dev_dbg(&priv->rtc->dev, "1HZ\n"); + + rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF); + + mc13783_ackirq(mc13783, irq); + + return IRQ_HANDLED; +} + +static int mc13783_rtc_update_irq_enable(struct device *dev, + unsigned int enabled) +{ + struct mc13783_rtc *priv = dev_get_drvdata(dev); + int ret = -ENODATA; + + mc13783_lock(priv->mc13783); + if (!priv->valid) + goto out; + + ret = (enabled ? mc13783_unmask : mc13783_mask)(priv->mc13783, + MC13783_IRQ_1HZ); +out: + mc13783_unlock(priv->mc13783); + + return ret; +} + +static const struct rtc_class_ops mc13783_rtc_ops = { + .read_time = mc13783_rtc_read_time, + .set_mmss = mc13783_rtc_set_mmss, + .update_irq_enable = mc13783_rtc_update_irq_enable, +}; + +static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev) +{ + struct mc13783_rtc *priv = dev; + struct mc13783 *mc13783 = priv->mc13783; + + dev_dbg(&priv->rtc->dev, "RTCRST\n"); + priv->valid = 0; + + mc13783_mask(mc13783, irq); + + return IRQ_HANDLED; +} + +static int __devinit mc13783_rtc_probe(struct platform_device *pdev) +{ + int ret; + struct mc13783_rtc *priv; + + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->mc13783 = dev_get_drvdata(pdev->dev.parent); + platform_set_drvdata(pdev, priv); + + priv->valid = 1; + + mc13783_lock(priv->mc13783); + + ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_RTCRST, + mc13783_rtc_reset_handler, DRIVER_NAME, priv); + if (ret) + goto err_reset_irq_request; + + ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_1HZ, + mc13783_rtc_update_handler, DRIVER_NAME, priv); + if (ret) + goto err_update_irq_request; + + mc13783_unlock(priv->mc13783); + + priv->rtc = rtc_device_register(pdev->name, + &pdev->dev, &mc13783_rtc_ops, THIS_MODULE); + + if (IS_ERR(priv->rtc)) { + ret = PTR_ERR(priv->rtc); + + mc13783_lock(priv->mc13783); + + mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); +err_update_irq_request: + + mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); +err_reset_irq_request: + + mc13783_unlock(priv->mc13783); + + platform_set_drvdata(pdev, NULL); + kfree(priv); + } + + return ret; +} + +static int __devexit mc13783_rtc_remove(struct platform_device *pdev) +{ + struct mc13783_rtc *priv = platform_get_drvdata(pdev); + + rtc_device_unregister(priv->rtc); + + mc13783_lock(priv->mc13783); + + mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv); + mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv); + + mc13783_unlock(priv->mc13783); + + platform_set_drvdata(pdev, NULL); + + kfree(priv); + + return 0; +} + +static struct platform_driver mc13783_rtc_driver = { + .remove = __devexit_p(mc13783_rtc_remove), + .driver = { + .name = DRIVER_NAME, + .owner = THIS_MODULE, + }, +}; + +static int __init mc13783_rtc_init(void) +{ + return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe); +} +module_init(mc13783_rtc_init); + +static void __exit mc13783_rtc_exit(void) +{ + platform_driver_unregister(&mc13783_rtc_driver); +} +module_exit(mc13783_rtc_exit); + +MODULE_AUTHOR("Sascha Hauer "); +MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC"); +MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:" DRIVER_NAME); -- cgit v0.10.2 From 8cfde8c1df31724f881de1a37f878ccbba4f178f Mon Sep 17 00:00:00 2001 From: "Mark A. Greer" Date: Tue, 15 Dec 2009 16:46:11 -0800 Subject: rtc: make rtc-omap driver ioremap its register space The rtc-omap driver currently assumes that the rtc's registers are at a fixed address and already mapped into virtual memory space. Remove those assumptions so the same driver can be used for similar devices that reside at different physical addresses (e.g., TI's DA8xx/OMAP-L13x SoC's). Also allow the possibility for the timer and alarm interrupts to use the same IRQ. Signed-off-by: Mark A. Greer Acked-by: David Brownell Acked-by: Kevin Hilman Acked-by: Tony Lindgren Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 564f3d1..9930d79 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -597,10 +597,11 @@ comment "on-CPU RTC drivers" config RTC_DRV_OMAP tristate "TI OMAP1" - depends on ARCH_OMAP15XX || ARCH_OMAP16XX || ARCH_OMAP730 + depends on ARCH_OMAP15XX || ARCH_OMAP16XX || ARCH_OMAP730 || ARCH_DAVINCI_DA8XX help - Say "yes" here to support the real time clock on TI OMAP1 chips. - This driver can also be built as a module called rtc-omap. + Say "yes" here to support the real time clock on TI OMAP1 and + DA8xx/OMAP-L13x chips. This driver can also be built as a + module called rtc-omap. config RTC_DRV_S3C tristate "Samsung S3C series SoC RTC" diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c index 0587d53..64d9727 100644 --- a/drivers/rtc/rtc-omap.c +++ b/drivers/rtc/rtc-omap.c @@ -87,9 +87,10 @@ #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3) #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2) +static void __iomem *rtc_base; -#define rtc_read(addr) omap_readb(OMAP_RTC_BASE + (addr)) -#define rtc_write(val, addr) omap_writeb(val, OMAP_RTC_BASE + (addr)) +#define rtc_read(addr) __raw_readb(rtc_base + (addr)) +#define rtc_write(val, addr) __raw_writeb(val, rtc_base + (addr)) /* we rely on the rtc framework to handle locking (rtc->ops_lock), @@ -330,32 +331,31 @@ static int __init omap_rtc_probe(struct platform_device *pdev) return -ENOENT; } - /* NOTE: using static mapping for RTC registers */ res = platform_get_resource(pdev, IORESOURCE_MEM, 0); - if (res && res->start != OMAP_RTC_BASE) { - pr_debug("%s: RTC registers at %08x, expected %08x\n", - pdev->name, (unsigned) res->start, OMAP_RTC_BASE); + if (!res) { + pr_debug("%s: RTC resource data missing\n", pdev->name); return -ENOENT; } - if (res) - mem = request_mem_region(res->start, - res->end - res->start + 1, - pdev->name); - else - mem = NULL; + mem = request_mem_region(res->start, resource_size(res), pdev->name); if (!mem) { pr_debug("%s: RTC registers at %08x are not free\n", - pdev->name, OMAP_RTC_BASE); + pdev->name, res->start); return -EBUSY; } + rtc_base = ioremap(res->start, resource_size(res)); + if (!rtc_base) { + pr_debug("%s: RTC registers can't be mapped\n", pdev->name); + goto fail; + } + rtc = rtc_device_register(pdev->name, &pdev->dev, &omap_rtc_ops, THIS_MODULE); if (IS_ERR(rtc)) { pr_debug("%s: can't register RTC device, err %ld\n", pdev->name, PTR_ERR(rtc)); - goto fail; + goto fail0; } platform_set_drvdata(pdev, rtc); dev_set_drvdata(&rtc->dev, mem); @@ -380,13 +380,14 @@ static int __init omap_rtc_probe(struct platform_device *pdev) dev_name(&rtc->dev), rtc)) { pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", pdev->name, omap_rtc_timer); - goto fail0; + goto fail1; } - if (request_irq(omap_rtc_alarm, rtc_irq, IRQF_DISABLED, - dev_name(&rtc->dev), rtc)) { + if ((omap_rtc_timer != omap_rtc_alarm) && + (request_irq(omap_rtc_alarm, rtc_irq, IRQF_DISABLED, + dev_name(&rtc->dev), rtc))) { pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n", pdev->name, omap_rtc_alarm); - goto fail1; + goto fail2; } /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ @@ -419,10 +420,12 @@ static int __init omap_rtc_probe(struct platform_device *pdev) return 0; -fail1: +fail2: free_irq(omap_rtc_timer, NULL); -fail0: +fail1: rtc_device_unregister(rtc); +fail0: + iounmap(rtc_base); fail: release_resource(mem); return -EIO; @@ -438,7 +441,9 @@ static int __exit omap_rtc_remove(struct platform_device *pdev) rtc_write(0, OMAP_RTC_INTERRUPTS_REG); free_irq(omap_rtc_timer, rtc); - free_irq(omap_rtc_alarm, rtc); + + if (omap_rtc_timer != omap_rtc_alarm) + free_irq(omap_rtc_alarm, rtc); release_resource(dev_get_drvdata(&rtc->dev)); rtc_device_unregister(rtc); -- cgit v0.10.2 From 1ce7c83fa91d27bd0e195e8b2ff10d3a1caeb0d6 Mon Sep 17 00:00:00 2001 From: Piotr Ziecik Date: Tue, 15 Dec 2009 16:46:12 -0800 Subject: rtc: add driver for BQ32000 I2C RTC This patch adds basic support for Texas Instruments BQ32000 I2C RTC. Only time reading/writing is implemented. Advanced features, such as trickle charger and crystal calibration are not supported. Signed-off-by: Piotr Ziecik Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 9930d79..40c168b 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -242,6 +242,15 @@ config RTC_DRV_M41T80_WDT If you say Y here you will get support for the watchdog timer in the ST M41T60 and M41T80 RTC chips series. +config RTC_DRV_BQ32K + tristate "TI BQ32000" + help + If you say Y here you will get support for the TI + BQ32000 I2C RTC chip. + + This driver can also be built as a module. If so, the module + will be called rtc-bq32k. + config RTC_DRV_DM355EVM tristate "TI DaVinci DM355 EVM RTC" depends on MFD_DM355EVM_MSP diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 6fa20f4..52f5f3c 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o obj-$(CONFIG_RTC_DRV_AT91SAM9) += rtc-at91sam9.o obj-$(CONFIG_RTC_DRV_AU1XXX) += rtc-au1xxx.o obj-$(CONFIG_RTC_DRV_BFIN) += rtc-bfin.o +obj-$(CONFIG_RTC_DRV_BQ32K) += rtc-bq32k.o obj-$(CONFIG_RTC_DRV_BQ4802) += rtc-bq4802.o obj-$(CONFIG_RTC_DRV_CMOS) += rtc-cmos.o obj-$(CONFIG_RTC_DRV_COH901331) += rtc-coh901331.o diff --git a/drivers/rtc/rtc-bq32k.c b/drivers/rtc/rtc-bq32k.c new file mode 100644 index 0000000..408cc8f --- /dev/null +++ b/drivers/rtc/rtc-bq32k.c @@ -0,0 +1,204 @@ +/* + * Driver for TI BQ32000 RTC. + * + * Copyright (C) 2009 Semihalf. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include + +#define BQ32K_SECONDS 0x00 /* Seconds register address */ +#define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */ +#define BQ32K_STOP 0x80 /* Oscillator Stop flat */ + +#define BQ32K_MINUTES 0x01 /* Minutes register address */ +#define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */ +#define BQ32K_OF 0x80 /* Oscillator Failure flag */ + +#define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */ +#define BQ32K_CENT 0x40 /* Century flag */ +#define BQ32K_CENT_EN 0x80 /* Century flag enable bit */ + +struct bq32k_regs { + uint8_t seconds; + uint8_t minutes; + uint8_t cent_hours; + uint8_t day; + uint8_t date; + uint8_t month; + uint8_t years; +}; + +static struct i2c_driver bq32k_driver; + +static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len) +{ + struct i2c_client *client = to_i2c_client(dev); + struct i2c_msg msgs[] = { + { + .addr = client->addr, + .flags = 0, + .len = 1, + .buf = &off, + }, { + .addr = client->addr, + .flags = I2C_M_RD, + .len = len, + .buf = data, + } + }; + + if (i2c_transfer(client->adapter, msgs, 2) == 2) + return 0; + + return -EIO; +} + +static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len) +{ + struct i2c_client *client = to_i2c_client(dev); + uint8_t buffer[len + 1]; + + buffer[0] = off; + memcpy(&buffer[1], data, len); + + if (i2c_master_send(client, buffer, len + 1) == len + 1) + return 0; + + return -EIO; +} + +static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct bq32k_regs regs; + int error; + + error = bq32k_read(dev, ®s, 0, sizeof(regs)); + if (error) + return error; + + tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK); + tm->tm_min = bcd2bin(regs.minutes & BQ32K_SECONDS_MASK); + tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK); + tm->tm_mday = bcd2bin(regs.date); + tm->tm_wday = bcd2bin(regs.day) - 1; + tm->tm_mon = bcd2bin(regs.month) - 1; + tm->tm_year = bcd2bin(regs.years) + + ((regs.cent_hours & BQ32K_CENT) ? 100 : 0); + + return rtc_valid_tm(tm); +} + +static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct bq32k_regs regs; + + regs.seconds = bin2bcd(tm->tm_sec); + regs.minutes = bin2bcd(tm->tm_min); + regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN; + regs.day = bin2bcd(tm->tm_wday + 1); + regs.date = bin2bcd(tm->tm_mday); + regs.month = bin2bcd(tm->tm_mon + 1); + + if (tm->tm_year >= 100) { + regs.cent_hours |= BQ32K_CENT; + regs.years = bin2bcd(tm->tm_year - 100); + } else + regs.years = bin2bcd(tm->tm_year); + + return bq32k_write(dev, ®s, 0, sizeof(regs)); +} + +static const struct rtc_class_ops bq32k_rtc_ops = { + .read_time = bq32k_rtc_read_time, + .set_time = bq32k_rtc_set_time, +}; + +static int bq32k_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct device *dev = &client->dev; + struct rtc_device *rtc; + uint8_t reg; + int error; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return -ENODEV; + + /* Check Oscillator Stop flag */ + error = bq32k_read(dev, ®, BQ32K_SECONDS, 1); + if (!error && (reg & BQ32K_STOP)) { + dev_warn(dev, "Oscillator was halted. Restarting...\n"); + reg &= ~BQ32K_STOP; + error = bq32k_write(dev, ®, BQ32K_SECONDS, 1); + } + if (error) + return error; + + /* Check Oscillator Failure flag */ + error = bq32k_read(dev, ®, BQ32K_MINUTES, 1); + if (!error && (reg & BQ32K_OF)) { + dev_warn(dev, "Oscillator Failure. Check RTC battery.\n"); + reg &= ~BQ32K_OF; + error = bq32k_write(dev, ®, BQ32K_MINUTES, 1); + } + if (error) + return error; + + rtc = rtc_device_register(bq32k_driver.driver.name, &client->dev, + &bq32k_rtc_ops, THIS_MODULE); + if (IS_ERR(rtc)) + return PTR_ERR(rtc); + + i2c_set_clientdata(client, rtc); + + return 0; +} + +static int __devexit bq32k_remove(struct i2c_client *client) +{ + struct rtc_device *rtc = i2c_get_clientdata(client); + + rtc_device_unregister(rtc); + return 0; +} + +static const struct i2c_device_id bq32k_id[] = { + { "bq32000", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, bq32k_id); + +static struct i2c_driver bq32k_driver = { + .driver = { + .name = "bq32k", + .owner = THIS_MODULE, + }, + .probe = bq32k_probe, + .remove = __devexit_p(bq32k_remove), + .id_table = bq32k_id, +}; + +static __init int bq32k_init(void) +{ + return i2c_add_driver(&bq32k_driver); +} +module_init(bq32k_init); + +static __exit void bq32k_exit(void) +{ + i2c_del_driver(&bq32k_driver); +} +module_exit(bq32k_exit); + +MODULE_AUTHOR("Semihalf, Piotr Ziecik "); +MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver"); +MODULE_LICENSE("GPL"); -- cgit v0.10.2 From 2934d6a859b70e57c729644e169a4fdf8c1c290c Mon Sep 17 00:00:00 2001 From: Linus Walleij Date: Tue, 15 Dec 2009 16:46:13 -0800 Subject: rtc: remove __raw_* accessors from PL031 RTC This switches __raw_[read|write]l() for plain [read|write]l in the PL031 RTC driver. The sister driver for PL030 use the simple accessors as most PrimeCell drivers. Signed-off-by: Linus Walleij Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-pl031.c b/drivers/rtc/rtc-pl031.c index f41873f..0264b11 100644 --- a/drivers/rtc/rtc-pl031.c +++ b/drivers/rtc/rtc-pl031.c @@ -51,10 +51,10 @@ static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) switch (cmd) { case RTC_AIE_OFF: - __raw_writel(1, ldata->base + RTC_MIS); + writel(1, ldata->base + RTC_MIS); return 0; case RTC_AIE_ON: - __raw_writel(0, ldata->base + RTC_MIS); + writel(0, ldata->base + RTC_MIS); return 0; } @@ -65,7 +65,7 @@ static int pl031_read_time(struct device *dev, struct rtc_time *tm) { struct pl031_local *ldata = dev_get_drvdata(dev); - rtc_time_to_tm(__raw_readl(ldata->base + RTC_DR), tm); + rtc_time_to_tm(readl(ldata->base + RTC_DR), tm); return 0; } @@ -76,7 +76,7 @@ static int pl031_set_time(struct device *dev, struct rtc_time *tm) struct pl031_local *ldata = dev_get_drvdata(dev); rtc_tm_to_time(tm, &time); - __raw_writel(time, ldata->base + RTC_LR); + writel(time, ldata->base + RTC_LR); return 0; } @@ -85,9 +85,9 @@ static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) { struct pl031_local *ldata = dev_get_drvdata(dev); - rtc_time_to_tm(__raw_readl(ldata->base + RTC_MR), &alarm->time); - alarm->pending = __raw_readl(ldata->base + RTC_RIS); - alarm->enabled = __raw_readl(ldata->base + RTC_IMSC); + rtc_time_to_tm(readl(ldata->base + RTC_MR), &alarm->time); + alarm->pending = readl(ldata->base + RTC_RIS); + alarm->enabled = readl(ldata->base + RTC_IMSC); return 0; } @@ -99,8 +99,8 @@ static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) rtc_tm_to_time(&alarm->time, &time); - __raw_writel(time, ldata->base + RTC_MR); - __raw_writel(!alarm->enabled, ldata->base + RTC_MIS); + writel(time, ldata->base + RTC_MR); + writel(!alarm->enabled, ldata->base + RTC_MIS); return 0; } @@ -180,8 +180,9 @@ err_req: static struct amba_id pl031_ids[] __initdata = { { - .id = 0x00041031, - .mask = 0x000fffff, }, + .id = 0x00041031, + .mask = 0x000fffff, + }, {0, 0}, }; -- cgit v0.10.2 From 43d15bcd4b6ba45fc45c99f5ef46e2528fb0dadf Mon Sep 17 00:00:00 2001 From: Dmitry Eremin-Solenikov Date: Tue, 15 Dec 2009 16:46:14 -0800 Subject: rtc: ds1307 make it possible to share an irq It's possible to have RTC irq shared with other device (e.g. mpc8349e-mitx board shares ds1339 irq with phy one). Handle this in driver. Signed-off-by: Dmitry Eremin-Solenikov Signed-off-by: Alessandro Zummo Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c index eb99ee4..8a99da6 100644 --- a/drivers/rtc/rtc-ds1307.c +++ b/drivers/rtc/rtc-ds1307.c @@ -874,7 +874,7 @@ read_rtc: } if (want_irq) { - err = request_irq(client->irq, ds1307_irq, 0, + err = request_irq(client->irq, ds1307_irq, IRQF_SHARED, ds1307->rtc->name, client); if (err) { dev_err(&client->dev, -- cgit v0.10.2 From a91912f8e4e92807d2cbf02d9687d421c1188f99 Mon Sep 17 00:00:00 2001 From: Yoichi Yuasa Date: Tue, 15 Dec 2009 16:46:15 -0800 Subject: rtc-vr41xx: use resource_size() Signed-off-by: Yoichi Yuasa Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-vr41xx.c b/drivers/rtc/rtc-vr41xx.c index fadddac..c324424 100644 --- a/drivers/rtc/rtc-vr41xx.c +++ b/drivers/rtc/rtc-vr41xx.c @@ -327,7 +327,7 @@ static int __devinit rtc_probe(struct platform_device *pdev) if (!res) return -EBUSY; - rtc1_base = ioremap(res->start, res->end - res->start + 1); + rtc1_base = ioremap(res->start, resource_size(res)); if (!rtc1_base) return -EBUSY; @@ -337,7 +337,7 @@ static int __devinit rtc_probe(struct platform_device *pdev) goto err_rtc1_iounmap; } - rtc2_base = ioremap(res->start, res->end - res->start + 1); + rtc2_base = ioremap(res->start, resource_size(res)); if (!rtc2_base) { retval = -EBUSY; goto err_rtc1_iounmap; -- cgit v0.10.2 From d973b632a362cb4075fbe5363eba238b6f245b55 Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Tue, 15 Dec 2009 16:46:16 -0800 Subject: rtc-x1205: unconditionally set date when setting clock All callsites of x1205_set_datetime() want the date to be set as well, so remove the flag parameter and set it unconditionally. Signed-off-by: Johannes Weiner Cc: Alessandro Zummo Cc: Paul Gortmaker Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/rtc-x1205.c b/drivers/rtc/rtc-x1205.c index 6583c1a..9aae491 100644 --- a/drivers/rtc/rtc-x1205.c +++ b/drivers/rtc/rtc-x1205.c @@ -155,11 +155,11 @@ static int x1205_get_status(struct i2c_client *client, unsigned char *sr) } static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, - int datetoo, u8 reg_base, unsigned char alm_enable) + u8 reg_base, unsigned char alm_enable) { - int i, xfer, nbytes; - unsigned char buf[8]; + int i, xfer; unsigned char rdata[10] = { 0, reg_base }; + unsigned char *buf = rdata + 2; static const unsigned char wel[3] = { 0, X1205_REG_SR, X1205_SR_WEL }; @@ -170,9 +170,9 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, static const unsigned char diswe[3] = { 0, X1205_REG_SR, 0 }; dev_dbg(&client->dev, - "%s: secs=%d, mins=%d, hours=%d\n", - __func__, - tm->tm_sec, tm->tm_min, tm->tm_hour); + "%s: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d\n", + __func__, tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, + tm->tm_mon, tm->tm_year, tm->tm_wday); buf[CCR_SEC] = bin2bcd(tm->tm_sec); buf[CCR_MIN] = bin2bcd(tm->tm_min); @@ -180,23 +180,15 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, /* set hour and 24hr bit */ buf[CCR_HOUR] = bin2bcd(tm->tm_hour) | X1205_HR_MIL; - /* should we also set the date? */ - if (datetoo) { - dev_dbg(&client->dev, - "%s: mday=%d, mon=%d, year=%d, wday=%d\n", - __func__, - tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); + buf[CCR_MDAY] = bin2bcd(tm->tm_mday); - buf[CCR_MDAY] = bin2bcd(tm->tm_mday); + /* month, 1 - 12 */ + buf[CCR_MONTH] = bin2bcd(tm->tm_mon + 1); - /* month, 1 - 12 */ - buf[CCR_MONTH] = bin2bcd(tm->tm_mon + 1); - - /* year, since the rtc epoch*/ - buf[CCR_YEAR] = bin2bcd(tm->tm_year % 100); - buf[CCR_WDAY] = tm->tm_wday & 0x07; - buf[CCR_Y2K] = bin2bcd((tm->tm_year + 1900) / 100); - } + /* year, since the rtc epoch*/ + buf[CCR_YEAR] = bin2bcd(tm->tm_year % 100); + buf[CCR_WDAY] = tm->tm_wday & 0x07; + buf[CCR_Y2K] = bin2bcd((tm->tm_year + 1900) / 100); /* If writing alarm registers, set compare bits on registers 0-4 */ if (reg_base < X1205_CCR_BASE) @@ -214,17 +206,8 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, return -EIO; } - - /* write register's data */ - if (datetoo) - nbytes = 8; - else - nbytes = 3; - for (i = 0; i < nbytes; i++) - rdata[2+i] = buf[i]; - - xfer = i2c_master_send(client, rdata, nbytes+2); - if (xfer != nbytes+2) { + xfer = i2c_master_send(client, rdata, sizeof(rdata)); + if (xfer != sizeof(rdata)) { dev_err(&client->dev, "%s: result=%d addr=%02x, data=%02x\n", __func__, @@ -282,7 +265,7 @@ static int x1205_fix_osc(struct i2c_client *client) memset(&tm, 0, sizeof(tm)); - err = x1205_set_datetime(client, &tm, 1, X1205_CCR_BASE, 0); + err = x1205_set_datetime(client, &tm, X1205_CCR_BASE, 0); if (err < 0) dev_err(&client->dev, "unable to restart the oscillator\n"); @@ -481,7 +464,7 @@ static int x1205_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) static int x1205_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) { return x1205_set_datetime(to_i2c_client(dev), - &alrm->time, 1, X1205_ALM0_BASE, alrm->enabled); + &alrm->time, X1205_ALM0_BASE, alrm->enabled); } static int x1205_rtc_read_time(struct device *dev, struct rtc_time *tm) @@ -493,7 +476,7 @@ static int x1205_rtc_read_time(struct device *dev, struct rtc_time *tm) static int x1205_rtc_set_time(struct device *dev, struct rtc_time *tm) { return x1205_set_datetime(to_i2c_client(dev), - tm, 1, X1205_CCR_BASE, 0); + tm, X1205_CCR_BASE, 0); } static int x1205_rtc_proc(struct device *dev, struct seq_file *seq) -- cgit v0.10.2 From afd49a7e92851239c3edffae730cc27080bfab78 Mon Sep 17 00:00:00 2001 From: Wan ZongShun Date: Tue, 15 Dec 2009 16:46:17 -0800 Subject: ARM: NUC900: add RTC driver support for nuc910 and nuc920 Signed-off-by: Wan ZongShun Cc: Alessandro Zummo Cc: Hu Ruihuan Cc: Russell King Cc: "jack wang" Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 40c168b..8167e9e 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -601,6 +601,12 @@ config RTC_DRV_AB3100 Select this to enable the ST-Ericsson AB3100 Mixed Signal IC RTC support. This chip contains a battery- and capacitor-backed RTC. +config RTC_DRV_NUC900 + tristate "NUC910/NUC920 RTC driver" + depends on RTC_CLASS && ARCH_W90X900 + help + If you say yes here you get support for the RTC subsystem of the + NUC910/NUC920 used in embedded systems. comment "on-CPU RTC drivers" diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 52f5f3c..e5160fd 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -56,6 +56,7 @@ obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o +obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o obj-$(CONFIG_RTC_DRV_PCAP) += rtc-pcap.o obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o diff --git a/drivers/rtc/rtc-nuc900.c b/drivers/rtc/rtc-nuc900.c new file mode 100644 index 0000000..bf59c9c --- /dev/null +++ b/drivers/rtc/rtc-nuc900.c @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2008-2009 Nuvoton technology corporation. + * + * Wan ZongShun + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation;version 2 of the License. + * + */ + +#include +#include +#include +#include +#include +#include +#include + +/* RTC Control Registers */ +#define REG_RTC_INIR 0x00 +#define REG_RTC_AER 0x04 +#define REG_RTC_FCR 0x08 +#define REG_RTC_TLR 0x0C +#define REG_RTC_CLR 0x10 +#define REG_RTC_TSSR 0x14 +#define REG_RTC_DWR 0x18 +#define REG_RTC_TAR 0x1C +#define REG_RTC_CAR 0x20 +#define REG_RTC_LIR 0x24 +#define REG_RTC_RIER 0x28 +#define REG_RTC_RIIR 0x2C +#define REG_RTC_TTR 0x30 + +#define RTCSET 0x01 +#define AERRWENB 0x10000 +#define INIRRESET 0xa5eb1357 +#define AERPOWERON 0xA965 +#define AERPOWEROFF 0x0000 +#define LEAPYEAR 0x0001 +#define TICKENB 0x80 +#define TICKINTENB 0x0002 +#define ALARMINTENB 0x0001 +#define MODE24 0x0001 + +struct nuc900_rtc { + int irq_num; + void __iomem *rtc_reg; + struct rtc_device *rtcdev; +}; + +struct nuc900_bcd_time { + int bcd_sec; + int bcd_min; + int bcd_hour; + int bcd_mday; + int bcd_mon; + int bcd_year; +}; + +static irqreturn_t nuc900_rtc_interrupt(int irq, void *_rtc) +{ + struct nuc900_rtc *rtc = _rtc; + unsigned long events = 0, rtc_irq; + + rtc_irq = __raw_readl(rtc->rtc_reg + REG_RTC_RIIR); + + if (rtc_irq & ALARMINTENB) { + rtc_irq &= ~ALARMINTENB; + __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR); + events |= RTC_AF | RTC_IRQF; + } + + if (rtc_irq & TICKINTENB) { + rtc_irq &= ~TICKINTENB; + __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR); + events |= RTC_UF | RTC_IRQF; + } + + rtc_update_irq(rtc->rtcdev, 1, events); + + return IRQ_HANDLED; +} + +static int *check_rtc_access_enable(struct nuc900_rtc *nuc900_rtc) +{ + unsigned int i; + __raw_writel(INIRRESET, nuc900_rtc->rtc_reg + REG_RTC_INIR); + + mdelay(10); + + __raw_writel(AERPOWERON, nuc900_rtc->rtc_reg + REG_RTC_AER); + + for (i = 0; i < 1000; i++) { + if (__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB) + return 0; + } + + if ((__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB) == 0x0) + return ERR_PTR(-ENODEV); + + return ERR_PTR(-EPERM); +} + +static void nuc900_rtc_bcd2bin(unsigned int timereg, + unsigned int calreg, struct rtc_time *tm) +{ + tm->tm_mday = bcd2bin(calreg >> 0); + tm->tm_mon = bcd2bin(calreg >> 8); + tm->tm_year = bcd2bin(calreg >> 16) + 100; + + tm->tm_sec = bcd2bin(timereg >> 0); + tm->tm_min = bcd2bin(timereg >> 8); + tm->tm_hour = bcd2bin(timereg >> 16); + + rtc_valid_tm(tm); +} + +static void nuc900_rtc_bin2bcd(struct rtc_time *settm, + struct nuc900_bcd_time *gettm) +{ + gettm->bcd_mday = bin2bcd(settm->tm_mday) << 0; + gettm->bcd_mon = bin2bcd(settm->tm_mon) << 8; + gettm->bcd_year = bin2bcd(settm->tm_year - 100) << 16; + + gettm->bcd_sec = bin2bcd(settm->tm_sec) << 0; + gettm->bcd_min = bin2bcd(settm->tm_min) << 8; + gettm->bcd_hour = bin2bcd(settm->tm_hour) << 16; +} + +static int nuc900_update_irq_enable(struct device *dev, unsigned int enabled) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + + if (enabled) + __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)| + (TICKINTENB), rtc->rtc_reg + REG_RTC_RIER); + else + __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)& + (~TICKINTENB), rtc->rtc_reg + REG_RTC_RIER); + + return 0; +} + +static int nuc900_alarm_irq_enable(struct device *dev, unsigned int enabled) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + + if (enabled) + __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)| + (ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER); + else + __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)& + (~ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER); + + return 0; +} + +static int nuc900_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + unsigned int timeval, clrval; + + timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TLR); + clrval = __raw_readl(rtc->rtc_reg + REG_RTC_CLR); + + nuc900_rtc_bcd2bin(timeval, clrval, tm); + + return 0; +} + +static int nuc900_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + struct nuc900_bcd_time gettm; + unsigned long val; + int *err; + + nuc900_rtc_bin2bcd(tm, &gettm); + + err = check_rtc_access_enable(rtc); + if (IS_ERR(err)) + return PTR_ERR(err); + + val = gettm.bcd_mday | gettm.bcd_mon | gettm.bcd_year; + __raw_writel(val, rtc->rtc_reg + REG_RTC_CLR); + + val = gettm.bcd_sec | gettm.bcd_min | gettm.bcd_hour; + __raw_writel(val, rtc->rtc_reg + REG_RTC_TLR); + + return 0; +} + +static int nuc900_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + unsigned int timeval, carval; + + timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TAR); + carval = __raw_readl(rtc->rtc_reg + REG_RTC_CAR); + + nuc900_rtc_bcd2bin(timeval, carval, &alrm->time); + + return 0; +} + +static int nuc900_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct nuc900_rtc *rtc = dev_get_drvdata(dev); + struct nuc900_bcd_time tm; + unsigned long val; + int *err; + + nuc900_rtc_bin2bcd(&alrm->time, &tm); + + err = check_rtc_access_enable(rtc); + if (IS_ERR(err)) + return PTR_ERR(err); + + val = tm.bcd_mday | tm.bcd_mon | tm.bcd_year; + __raw_writel(val, rtc->rtc_reg + REG_RTC_CAR); + + val = tm.bcd_sec | tm.bcd_min | tm.bcd_hour; + __raw_writel(val, rtc->rtc_reg + REG_RTC_TAR); + + return 0; +} + +static struct rtc_class_ops nuc900_rtc_ops = { + .read_time = nuc900_rtc_read_time, + .set_time = nuc900_rtc_set_time, + .read_alarm = nuc900_rtc_read_alarm, + .set_alarm = nuc900_rtc_set_alarm, + .alarm_irq_enable = nuc900_alarm_irq_enable, + .update_irq_enable = nuc900_update_irq_enable, +}; + +static int __devinit nuc900_rtc_probe(struct platform_device *pdev) +{ + struct resource *res; + struct nuc900_rtc *nuc900_rtc; + int err = 0; + + nuc900_rtc = kzalloc(sizeof(struct nuc900_rtc), GFP_KERNEL); + if (!nuc900_rtc) { + dev_err(&pdev->dev, "kzalloc nuc900_rtc failed\n"); + return -ENOMEM; + } + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!res) { + dev_err(&pdev->dev, "platform_get_resource failed\n"); + err = -ENXIO; + goto fail1; + } + + if (!request_mem_region(res->start, resource_size(res), + pdev->name)) { + dev_err(&pdev->dev, "request_mem_region failed\n"); + err = -EBUSY; + goto fail1; + } + + nuc900_rtc->rtc_reg = ioremap(res->start, resource_size(res)); + if (!nuc900_rtc->rtc_reg) { + dev_err(&pdev->dev, "ioremap rtc_reg failed\n"); + err = -ENOMEM; + goto fail2; + } + + nuc900_rtc->irq_num = platform_get_irq(pdev, 0); + if (request_irq(nuc900_rtc->irq_num, nuc900_rtc_interrupt, + IRQF_DISABLED, "nuc900rtc", nuc900_rtc)) { + dev_err(&pdev->dev, "NUC900 RTC request irq failed\n"); + err = -EBUSY; + goto fail3; + } + + nuc900_rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev, + &nuc900_rtc_ops, THIS_MODULE); + if (IS_ERR(nuc900_rtc->rtcdev)) { + dev_err(&pdev->dev, "rtc device register faild\n"); + err = PTR_ERR(nuc900_rtc->rtcdev); + goto fail4; + } + + platform_set_drvdata(pdev, nuc900_rtc); + __raw_writel(__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_TSSR) | MODE24, + nuc900_rtc->rtc_reg + REG_RTC_TSSR); + + return 0; + +fail4: free_irq(nuc900_rtc->irq_num, nuc900_rtc); +fail3: iounmap(nuc900_rtc->rtc_reg); +fail2: release_mem_region(res->start, resource_size(res)); +fail1: kfree(nuc900_rtc); + return err; +} + +static int __devexit nuc900_rtc_remove(struct platform_device *pdev) +{ + struct nuc900_rtc *nuc900_rtc = platform_get_drvdata(pdev); + struct resource *res; + + rtc_device_unregister(nuc900_rtc->rtcdev); + free_irq(nuc900_rtc->irq_num, nuc900_rtc); + iounmap(nuc900_rtc->rtc_reg); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + release_mem_region(res->start, resource_size(res)); + + kfree(nuc900_rtc); + + platform_set_drvdata(pdev, NULL); + + return 0; +} + +static struct platform_driver nuc900_rtc_driver = { + .remove = __devexit_p(nuc900_rtc_remove), + .driver = { + .name = "nuc900-rtc", + .owner = THIS_MODULE, + }, +}; + +static int __init nuc900_rtc_init(void) +{ + return platform_driver_probe(&nuc900_rtc_driver, nuc900_rtc_probe); +} + +static void __exit nuc900_rtc_exit(void) +{ + platform_driver_unregister(&nuc900_rtc_driver); +} + +module_init(nuc900_rtc_init); +module_exit(nuc900_rtc_exit); + +MODULE_AUTHOR("Wan ZongShun "); +MODULE_DESCRIPTION("nuc910/nuc920 RTC driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nuc900-rtc"); -- cgit v0.10.2 From 4efec6272e8e61fc77132b4d2bae56d61b289956 Mon Sep 17 00:00:00 2001 From: Roel Kluin Date: Tue, 15 Dec 2009 16:46:18 -0800 Subject: gpio: fix test on unsigned in lnw_irq_type() The wrong test was used, gpio is unsigned and it had an off-by-one. Signed-off-by: Roel Kluin Cc: Alek Du Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/gpio/langwell_gpio.c b/drivers/gpio/langwell_gpio.c index 4baf3d7..6c0ebbd 100644 --- a/drivers/gpio/langwell_gpio.c +++ b/drivers/gpio/langwell_gpio.c @@ -123,7 +123,7 @@ static int lnw_irq_type(unsigned irq, unsigned type) void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]); void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]); - if (gpio < 0 || gpio > lnw->chip.ngpio) + if (gpio >= lnw->chip.ngpio) return -EINVAL; spin_lock_irqsave(&lnw->lock, flags); if (type & IRQ_TYPE_EDGE_RISING) -- cgit v0.10.2 From 35570ac6039ef490b9c5abde1fee4803a39bf4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20R=C3=B6jfors?= Date: Tue, 15 Dec 2009 16:46:18 -0800 Subject: gpio: add GPIO driver for the Timberdale FPGA MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A GPIO driver for the Timberdale FPGA found on the Intel Atom board Russellville. The GPIO driver also has an IRQ-chip to support interrupts on the pins. Signed-off-by: Richard Röjfors Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 57ca339..a019b49 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -206,6 +206,12 @@ config GPIO_LANGWELL help Say Y here to support Intel Moorestown platform GPIO. +config GPIO_TIMBERDALE + bool "Support for timberdale GPIO IP" + depends on MFD_TIMBERDALE && GPIOLIB && HAS_IOMEM + ---help--- + Add support for the GPIO IP in the timberdale FPGA. + comment "SPI GPIO expanders:" config GPIO_MAX7301 diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 270b6d7..52fe4cf 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -13,6 +13,7 @@ obj-$(CONFIG_GPIO_MCP23S08) += mcp23s08.o obj-$(CONFIG_GPIO_PCA953X) += pca953x.o obj-$(CONFIG_GPIO_PCF857X) += pcf857x.o obj-$(CONFIG_GPIO_PL061) += pl061.o +obj-$(CONFIG_GPIO_TIMBERDALE) += timbgpio.o obj-$(CONFIG_GPIO_TWL4030) += twl4030-gpio.o obj-$(CONFIG_GPIO_UCB1400) += ucb1400_gpio.o obj-$(CONFIG_GPIO_XILINX) += xilinx_gpio.o diff --git a/drivers/gpio/timbgpio.c b/drivers/gpio/timbgpio.c new file mode 100644 index 0000000..a4d344b --- /dev/null +++ b/drivers/gpio/timbgpio.c @@ -0,0 +1,342 @@ +/* + * timbgpio.c timberdale FPGA GPIO driver + * Copyright (c) 2009 Intel Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* Supports: + * Timberdale FPGA GPIO + */ + +#include +#include +#include +#include +#include +#include + +#define DRIVER_NAME "timb-gpio" + +#define TGPIOVAL 0x00 +#define TGPIODIR 0x04 +#define TGPIO_IER 0x08 +#define TGPIO_ISR 0x0c +#define TGPIO_IPR 0x10 +#define TGPIO_ICR 0x14 +#define TGPIO_FLR 0x18 +#define TGPIO_LVR 0x1c + +struct timbgpio { + void __iomem *membase; + spinlock_t lock; /* mutual exclusion */ + struct gpio_chip gpio; + int irq_base; +}; + +static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index, + unsigned offset, bool enabled) +{ + struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio); + u32 reg; + + spin_lock(&tgpio->lock); + reg = ioread32(tgpio->membase + offset); + + if (enabled) + reg |= (1 << index); + else + reg &= ~(1 << index); + + iowrite32(reg, tgpio->membase + offset); + spin_unlock(&tgpio->lock); + + return 0; +} + +static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr) +{ + return timbgpio_update_bit(gpio, nr, TGPIODIR, true); +} + +static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr) +{ + struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio); + u32 value; + + value = ioread32(tgpio->membase + TGPIOVAL); + return (value & (1 << nr)) ? 1 : 0; +} + +static int timbgpio_gpio_direction_output(struct gpio_chip *gpio, + unsigned nr, int val) +{ + return timbgpio_update_bit(gpio, nr, TGPIODIR, false); +} + +static void timbgpio_gpio_set(struct gpio_chip *gpio, + unsigned nr, int val) +{ + timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0); +} + +static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset) +{ + struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio); + + if (tgpio->irq_base <= 0) + return -EINVAL; + + return tgpio->irq_base + offset; +} + +/* + * GPIO IRQ + */ +static void timbgpio_irq_disable(unsigned irq) +{ + struct timbgpio *tgpio = get_irq_chip_data(irq); + int offset = irq - tgpio->irq_base; + + timbgpio_update_bit(&tgpio->gpio, offset, TGPIO_IER, 0); +} + +static void timbgpio_irq_enable(unsigned irq) +{ + struct timbgpio *tgpio = get_irq_chip_data(irq); + int offset = irq - tgpio->irq_base; + + timbgpio_update_bit(&tgpio->gpio, offset, TGPIO_IER, 1); +} + +static int timbgpio_irq_type(unsigned irq, unsigned trigger) +{ + struct timbgpio *tgpio = get_irq_chip_data(irq); + int offset = irq - tgpio->irq_base; + unsigned long flags; + u32 lvr, flr; + + if (offset < 0 || offset > tgpio->gpio.ngpio) + return -EINVAL; + + spin_lock_irqsave(&tgpio->lock, flags); + + lvr = ioread32(tgpio->membase + TGPIO_LVR); + flr = ioread32(tgpio->membase + TGPIO_FLR); + + if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) { + flr &= ~(1 << offset); + if (trigger & IRQ_TYPE_LEVEL_HIGH) + lvr |= 1 << offset; + else + lvr &= ~(1 << offset); + } + + if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) + return -EINVAL; + else { + flr |= 1 << offset; + /* opposite compared to the datasheet, but it mirrors the + * reality + */ + if (trigger & IRQ_TYPE_EDGE_FALLING) + lvr |= 1 << offset; + else + lvr &= ~(1 << offset); + } + + iowrite32(lvr, tgpio->membase + TGPIO_LVR); + iowrite32(flr, tgpio->membase + TGPIO_FLR); + iowrite32(1 << offset, tgpio->membase + TGPIO_ICR); + spin_unlock_irqrestore(&tgpio->lock, flags); + + return 0; +} + +static void timbgpio_irq(unsigned int irq, struct irq_desc *desc) +{ + struct timbgpio *tgpio = get_irq_data(irq); + unsigned long ipr; + int offset; + + desc->chip->ack(irq); + ipr = ioread32(tgpio->membase + TGPIO_IPR); + iowrite32(ipr, tgpio->membase + TGPIO_ICR); + + for_each_bit(offset, &ipr, tgpio->gpio.ngpio) + generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset)); +} + +static struct irq_chip timbgpio_irqchip = { + .name = "GPIO", + .enable = timbgpio_irq_enable, + .disable = timbgpio_irq_disable, + .set_type = timbgpio_irq_type, +}; + +static int __devinit timbgpio_probe(struct platform_device *pdev) +{ + int err, i; + struct gpio_chip *gc; + struct timbgpio *tgpio; + struct resource *iomem; + struct timbgpio_platform_data *pdata = pdev->dev.platform_data; + int irq = platform_get_irq(pdev, 0); + + if (!pdata || pdata->nr_pins > 32) { + err = -EINVAL; + goto err_mem; + } + + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!iomem) { + err = -EINVAL; + goto err_mem; + } + + tgpio = kzalloc(sizeof(*tgpio), GFP_KERNEL); + if (!tgpio) { + err = -EINVAL; + goto err_mem; + } + tgpio->irq_base = pdata->irq_base; + + spin_lock_init(&tgpio->lock); + + if (!request_mem_region(iomem->start, resource_size(iomem), + DRIVER_NAME)) { + err = -EBUSY; + goto err_request; + } + + tgpio->membase = ioremap(iomem->start, resource_size(iomem)); + if (!tgpio->membase) { + err = -ENOMEM; + goto err_ioremap; + } + + gc = &tgpio->gpio; + + gc->label = dev_name(&pdev->dev); + gc->owner = THIS_MODULE; + gc->dev = &pdev->dev; + gc->direction_input = timbgpio_gpio_direction_input; + gc->get = timbgpio_gpio_get; + gc->direction_output = timbgpio_gpio_direction_output; + gc->set = timbgpio_gpio_set; + gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL; + gc->dbg_show = NULL; + gc->base = pdata->gpio_base; + gc->ngpio = pdata->nr_pins; + gc->can_sleep = 0; + + err = gpiochip_add(gc); + if (err) + goto err_chipadd; + + platform_set_drvdata(pdev, tgpio); + + /* make sure to disable interrupts */ + iowrite32(0x0, tgpio->membase + TGPIO_IER); + + if (irq < 0 || tgpio->irq_base <= 0) + return 0; + + for (i = 0; i < pdata->nr_pins; i++) { + set_irq_chip_and_handler_name(tgpio->irq_base + i, + &timbgpio_irqchip, handle_simple_irq, "mux"); + set_irq_chip_data(tgpio->irq_base + i, tgpio); +#ifdef CONFIG_ARM + set_irq_flags(tgpio->irq_base + i, IRQF_VALID | IRQF_PROBE); +#endif + } + + set_irq_data(irq, tgpio); + set_irq_chained_handler(irq, timbgpio_irq); + + return 0; + +err_chipadd: + iounmap(tgpio->membase); +err_ioremap: + release_mem_region(iomem->start, resource_size(iomem)); +err_request: + kfree(tgpio); +err_mem: + printk(KERN_ERR DRIVER_NAME": Failed to register GPIOs: %d\n", err); + + return err; +} + +static int __devexit timbgpio_remove(struct platform_device *pdev) +{ + int err; + struct timbgpio_platform_data *pdata = pdev->dev.platform_data; + struct timbgpio *tgpio = platform_get_drvdata(pdev); + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); + int irq = platform_get_irq(pdev, 0); + + if (irq >= 0 && tgpio->irq_base > 0) { + int i; + for (i = 0; i < pdata->nr_pins; i++) { + set_irq_chip(tgpio->irq_base + i, NULL); + set_irq_chip_data(tgpio->irq_base + i, NULL); + } + + set_irq_handler(irq, NULL); + set_irq_data(irq, NULL); + } + + err = gpiochip_remove(&tgpio->gpio); + if (err) + printk(KERN_ERR DRIVER_NAME": failed to remove gpio_chip\n"); + + iounmap(tgpio->membase); + release_mem_region(iomem->start, resource_size(iomem)); + kfree(tgpio); + + platform_set_drvdata(pdev, NULL); + + return 0; +} + +static struct platform_driver timbgpio_platform_driver = { + .driver = { + .name = DRIVER_NAME, + .owner = THIS_MODULE, + }, + .probe = timbgpio_probe, + .remove = timbgpio_remove, +}; + +/*--------------------------------------------------------------------------*/ + +static int __init timbgpio_init(void) +{ + return platform_driver_register(&timbgpio_platform_driver); +} + +static void __exit timbgpio_exit(void) +{ + platform_driver_unregister(&timbgpio_platform_driver); +} + +module_init(timbgpio_init); +module_exit(timbgpio_exit); + +MODULE_DESCRIPTION("Timberdale GPIO driver"); +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Mocean Laboratories"); +MODULE_ALIAS("platform:"DRIVER_NAME); + diff --git a/include/linux/timb_gpio.h b/include/linux/timb_gpio.h new file mode 100644 index 0000000..ce456ea --- /dev/null +++ b/include/linux/timb_gpio.h @@ -0,0 +1,37 @@ +/* + * timb_gpio.h timberdale FPGA GPIO driver, platform data definition + * Copyright (c) 2009 Intel Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef _LINUX_TIMB_GPIO_H +#define _LINUX_TIMB_GPIO_H + +/** + * struct timbgpio_platform_data - Platform data of the Timberdale GPIO driver + * @gpio_base The number of the first GPIO pin, set to -1 for + * dynamic number allocation. + * @nr_pins Number of pins that is supported by the hardware (1-32) + * @irq_base If IRQ is supported by the hardware, this is the base + * number of IRQ:s. One IRQ per pin will be used. Set to + * -1 if IRQ:s is not supported. + */ +struct timbgpio_platform_data { + int gpio_base; + int nr_pins; + int irq_base; +}; + +#endif -- cgit v0.10.2 From 0769746183caff9d4334be48c7b0e7d2ec8716c4 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Tue, 15 Dec 2009 16:46:20 -0800 Subject: gpiolib: add support for changing value polarity in sysfs Drivers may use gpiolib sysfs as part of their public user space interface. The GPIO number and polarity might change from board to board. The gpio_export_link() call can be used to hide the GPIO number from user space. Add support for also hiding the GPIO line polarity changes from user space. Signed-off-by: Jani Nikula Cc: David Brownell Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/Documentation/gpio.txt b/Documentation/gpio.txt index e4e7daed2..1866c27 100644 --- a/Documentation/gpio.txt +++ b/Documentation/gpio.txt @@ -531,6 +531,13 @@ and have the following read/write attributes: This file exists only if the pin can be configured as an interrupt generating input pin. + "active_low" ... reads as either 0 (false) or 1 (true). Write + any nonzero value to invert the value attribute both + for reading and writing. Existing and subsequent + poll(2) support configuration via the edge attribute + for "rising" and "falling" edges will follow this + setting. + GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the controller implementing GPIOs starting at #42) and have the following read-only attributes: @@ -566,6 +573,8 @@ requested using gpio_request(): int gpio_export_link(struct device *dev, const char *name, unsigned gpio) + /* change the polarity of a GPIO node in sysfs */ + int gpio_sysfs_set_active_low(unsigned gpio, int value); After a kernel driver requests a GPIO, it may only be made available in the sysfs interface by gpio_export(). The driver can control whether the @@ -580,3 +589,9 @@ After the GPIO has been exported, gpio_export_link() allows creating symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can use this to provide the interface under their own device in sysfs with a descriptive name. + +Drivers can use gpio_sysfs_set_active_low() to hide GPIO line polarity +differences between boards from user space. This only affects the +sysfs interface. Polarity change can be done both before and after +gpio_export(), and previously enabled poll(2) support for either +rising or falling edge will be reconfigured to follow this setting. diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 50de0f5..a25ad28 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -53,6 +53,7 @@ struct gpio_desc { #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ #define FLAG_TRIG_FALL 5 /* trigger on falling edge */ #define FLAG_TRIG_RISE 6 /* trigger on rising edge */ +#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */ #define PDESC_ID_SHIFT 16 /* add new flags before this one */ @@ -210,6 +211,11 @@ static DEFINE_MUTEX(sysfs_lock); * * configures behavior of poll(2) on /value * * available only if pin can generate IRQs on input * * is read/write as "none", "falling", "rising", or "both" + * /active_low + * * configures polarity of /value + * * is read/write as zero/nonzero + * * also affects existing and subsequent "falling" and "rising" + * /edge configuration */ static ssize_t gpio_direction_show(struct device *dev, @@ -255,7 +261,7 @@ static ssize_t gpio_direction_store(struct device *dev, return status ? : size; } -static const DEVICE_ATTR(direction, 0644, +static /* const */ DEVICE_ATTR(direction, 0644, gpio_direction_show, gpio_direction_store); static ssize_t gpio_value_show(struct device *dev, @@ -267,10 +273,17 @@ static ssize_t gpio_value_show(struct device *dev, mutex_lock(&sysfs_lock); - if (!test_bit(FLAG_EXPORT, &desc->flags)) + if (!test_bit(FLAG_EXPORT, &desc->flags)) { status = -EIO; - else - status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio)); + } else { + int value; + + value = !!gpio_get_value_cansleep(gpio); + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + value = !value; + + status = sprintf(buf, "%d\n", value); + } mutex_unlock(&sysfs_lock); return status; @@ -294,6 +307,8 @@ static ssize_t gpio_value_store(struct device *dev, status = strict_strtol(buf, 0, &value); if (status == 0) { + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + value = !value; gpio_set_value_cansleep(gpio, value != 0); status = size; } @@ -303,7 +318,7 @@ static ssize_t gpio_value_store(struct device *dev, return status; } -static /*const*/ DEVICE_ATTR(value, 0644, +static const DEVICE_ATTR(value, 0644, gpio_value_show, gpio_value_store); static irqreturn_t gpio_sysfs_irq(int irq, void *priv) @@ -352,9 +367,11 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, irq_flags = IRQF_SHARED; if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) - irq_flags |= IRQF_TRIGGER_FALLING; + irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? + IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) - irq_flags |= IRQF_TRIGGER_RISING; + irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? + IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; if (!pdesc) { pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL); @@ -475,9 +492,79 @@ found: static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); +static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, + int value) +{ + int status = 0; + + if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) + return 0; + + if (value) + set_bit(FLAG_ACTIVE_LOW, &desc->flags); + else + clear_bit(FLAG_ACTIVE_LOW, &desc->flags); + + /* reconfigure poll(2) support if enabled on one edge only */ + if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ + !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { + unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; + + gpio_setup_irq(desc, dev, 0); + status = gpio_setup_irq(desc, dev, trigger_flags); + } + + return status; +} + +static ssize_t gpio_active_low_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + const struct gpio_desc *desc = dev_get_drvdata(dev); + ssize_t status; + + mutex_lock(&sysfs_lock); + + if (!test_bit(FLAG_EXPORT, &desc->flags)) + status = -EIO; + else + status = sprintf(buf, "%d\n", + !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); + + mutex_unlock(&sysfs_lock); + + return status; +} + +static ssize_t gpio_active_low_store(struct device *dev, + struct device_attribute *attr, const char *buf, size_t size) +{ + struct gpio_desc *desc = dev_get_drvdata(dev); + ssize_t status; + + mutex_lock(&sysfs_lock); + + if (!test_bit(FLAG_EXPORT, &desc->flags)) { + status = -EIO; + } else { + long value; + + status = strict_strtol(buf, 0, &value); + if (status == 0) + status = sysfs_set_active_low(desc, dev, value != 0); + } + + mutex_unlock(&sysfs_lock); + + return status ? : size; +} + +static const DEVICE_ATTR(active_low, 0644, + gpio_active_low_show, gpio_active_low_store); + static const struct attribute *gpio_attrs[] = { - &dev_attr_direction.attr, &dev_attr_value.attr, + &dev_attr_active_low.attr, NULL, }; @@ -662,12 +749,12 @@ int gpio_export(unsigned gpio, bool direction_may_change) dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), desc, ioname ? ioname : "gpio%d", gpio); if (!IS_ERR(dev)) { - if (direction_may_change) - status = sysfs_create_group(&dev->kobj, + status = sysfs_create_group(&dev->kobj, &gpio_attr_group); - else + + if (!status && direction_may_change) status = device_create_file(dev, - &dev_attr_value); + &dev_attr_direction); if (!status && gpio_to_irq(gpio) >= 0 && (direction_may_change @@ -744,6 +831,55 @@ done: } EXPORT_SYMBOL_GPL(gpio_export_link); + +/** + * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value + * @gpio: gpio to change + * @value: non-zero to use active low, i.e. inverted values + * + * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. + * The GPIO does not have to be exported yet. If poll(2) support has + * been enabled for either rising or falling edge, it will be + * reconfigured to follow the new polarity. + * + * Returns zero on success, else an error. + */ +int gpio_sysfs_set_active_low(unsigned gpio, int value) +{ + struct gpio_desc *desc; + struct device *dev = NULL; + int status = -EINVAL; + + if (!gpio_is_valid(gpio)) + goto done; + + mutex_lock(&sysfs_lock); + + desc = &gpio_desc[gpio]; + + if (test_bit(FLAG_EXPORT, &desc->flags)) { + struct device *dev; + + dev = class_find_device(&gpio_class, NULL, desc, match_export); + if (dev == NULL) { + status = -ENODEV; + goto unlock; + } + } + + status = sysfs_set_active_low(desc, dev, value); + +unlock: + mutex_unlock(&sysfs_lock); + +done: + if (status) + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); + + return status; +} +EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); + /** * gpio_unexport - reverse effect of gpio_export() * @gpio: gpio to make unavailable @@ -1094,6 +1230,7 @@ void gpio_free(unsigned gpio) } desc_set_label(desc, NULL); module_put(desc->chip->owner); + clear_bit(FLAG_ACTIVE_LOW, &desc->flags); clear_bit(FLAG_REQUESTED, &desc->flags); } else WARN_ON(extra_checks); diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index 204bed3..485eeb6 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h @@ -145,6 +145,7 @@ extern int __gpio_to_irq(unsigned gpio); extern int gpio_export(unsigned gpio, bool direction_may_change); extern int gpio_export_link(struct device *dev, const char *name, unsigned gpio); +extern int gpio_sysfs_set_active_low(unsigned gpio, int value); extern void gpio_unexport(unsigned gpio); #endif /* CONFIG_GPIO_SYSFS */ @@ -197,6 +198,11 @@ static inline int gpio_export_link(struct device *dev, const char *name, return -ENOSYS; } +static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) +{ + return -ENOSYS; +} + static inline void gpio_unexport(unsigned gpio) { } diff --git a/include/linux/gpio.h b/include/linux/gpio.h index 059bd18..4e949a5 100644 --- a/include/linux/gpio.h +++ b/include/linux/gpio.h @@ -99,6 +99,12 @@ static inline int gpio_export_link(struct device *dev, const char *name, return -EINVAL; } +static inline int gpio_sysfs_set_active_low(unsigned gpio, int value) +{ + /* GPIO can never have been requested */ + WARN_ON(1); + return -EINVAL; +} static inline void gpio_unexport(unsigned gpio) { -- cgit v0.10.2 From 9cfe4a93e50bb36392c8d74432e4607f297cdbfc Mon Sep 17 00:00:00 2001 From: Michael Hennerich Date: Tue, 15 Dec 2009 16:46:21 -0800 Subject: fbdev: bfin-lq035q1-fb: new Blackfin Landscape LCD EZ-Extender driver Framebuffer driver for the Landscape LCD EZ-Extender (ADZS-BFLLCD-EZEXT) http://docs.blackfin.uclinux.org/doku.php?id=hw:cards:landscape_lcd_ez-extender Signed-off-by: Michael Hennerich Signed-off-by: Bryan Wu Signed-off-by: Mike Frysinger Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/arch/blackfin/include/asm/bfin-lq035q1.h b/arch/blackfin/include/asm/bfin-lq035q1.h new file mode 100644 index 0000000..57bc21a --- /dev/null +++ b/arch/blackfin/include/asm/bfin-lq035q1.h @@ -0,0 +1,28 @@ +/* + * Blackfin LCD Framebuffer driver SHARP LQ035Q1DH02 + * + * Copyright 2008-2009 Analog Devices Inc. + * Licensed under the GPL-2 or later. + */ + +#ifndef BFIN_LQ035Q1_H +#define BFIN_LQ035Q1_H + +#define LQ035_RL (0 << 8) /* Right -> Left Scan */ +#define LQ035_LR (1 << 8) /* Left -> Right Scan */ +#define LQ035_TB (1 << 9) /* Top -> Botton Scan */ +#define LQ035_BT (0 << 9) /* Botton -> Top Scan */ +#define LQ035_BGR (1 << 11) /* Use BGR format */ +#define LQ035_RGB (0 << 11) /* Use RGB format */ +#define LQ035_NORM (1 << 13) /* Reversal */ +#define LQ035_REV (0 << 13) /* Reversal */ + +struct bfin_lq035q1fb_disp_info { + + unsigned mode; + /* GPIOs */ + int use_bl; + unsigned gpio_bl; +}; + +#endif /* BFIN_LQ035Q1_H */ diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 99c0df1..5a5c303 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -614,6 +614,21 @@ config FB_BFIN_T350MCQB This display is a QVGA 320x240 24-bit RGB display interfaced by an 8-bit wide PPI It uses PPI[0..7] PPI_FS1, PPI_FS2 and PPI_CLK. +config FB_BFIN_LQ035Q1 + tristate "SHARP LQ035Q1DH02 TFT LCD" + depends on FB && BLACKFIN && SPI + select FB_CFB_FILLRECT + select FB_CFB_COPYAREA + select FB_CFB_IMAGEBLIT + select BFIN_GPTIMERS + help + This is the framebuffer device driver for a SHARP LQ035Q1DH02 TFT display found on + the Blackfin Landscape LCD EZ-Extender Card. + This display is a QVGA 320x240 18-bit RGB display interfaced by an 16-bit wide PPI + It uses PPI[0..15] PPI_FS1, PPI_FS2 and PPI_CLK. + + To compile this driver as a module, choose M here: the + module will be called bfin-lq035q1-fb. config FB_STI tristate "HP STI frame buffer device support" diff --git a/drivers/video/Makefile b/drivers/video/Makefile index 0f8da33..4ecb30c 100644 --- a/drivers/video/Makefile +++ b/drivers/video/Makefile @@ -137,6 +137,7 @@ obj-$(CONFIG_FB_EFI) += efifb.o obj-$(CONFIG_FB_VGA16) += vga16fb.o obj-$(CONFIG_FB_OF) += offb.o obj-$(CONFIG_FB_BF54X_LQ043) += bf54x-lq043fb.o +obj-$(CONFIG_FB_BFIN_LQ035Q1) += bfin-lq035q1-fb.o obj-$(CONFIG_FB_BFIN_T350MCQB) += bfin-t350mcqb-fb.o obj-$(CONFIG_FB_MX3) += mx3fb.o obj-$(CONFIG_FB_DA8XX) += da8xx-fb.o diff --git a/drivers/video/bfin-lq035q1-fb.c b/drivers/video/bfin-lq035q1-fb.c new file mode 100644 index 0000000..b690c26 --- /dev/null +++ b/drivers/video/bfin-lq035q1-fb.c @@ -0,0 +1,826 @@ +/* + * Blackfin LCD Framebuffer driver SHARP LQ035Q1DH02 + * + * Copyright 2008-2009 Analog Devices Inc. + * Licensed under the GPL-2 or later. + */ + +#define DRIVER_NAME "bfin-lq035q1" +#define pr_fmt(fmt) DRIVER_NAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#if defined(BF533_FAMILY) || defined(BF538_FAMILY) +#define TIMER_HSYNC_id TIMER1_id +#define TIMER_HSYNCbit TIMER1bit +#define TIMER_HSYNC_STATUS_TRUN TIMER_STATUS_TRUN1 +#define TIMER_HSYNC_STATUS_TIMIL TIMER_STATUS_TIMIL1 +#define TIMER_HSYNC_STATUS_TOVF TIMER_STATUS_TOVF1 + +#define TIMER_VSYNC_id TIMER2_id +#define TIMER_VSYNCbit TIMER2bit +#define TIMER_VSYNC_STATUS_TRUN TIMER_STATUS_TRUN2 +#define TIMER_VSYNC_STATUS_TIMIL TIMER_STATUS_TIMIL2 +#define TIMER_VSYNC_STATUS_TOVF TIMER_STATUS_TOVF2 +#else +#define TIMER_HSYNC_id TIMER0_id +#define TIMER_HSYNCbit TIMER0bit +#define TIMER_HSYNC_STATUS_TRUN TIMER_STATUS_TRUN0 +#define TIMER_HSYNC_STATUS_TIMIL TIMER_STATUS_TIMIL0 +#define TIMER_HSYNC_STATUS_TOVF TIMER_STATUS_TOVF0 + +#define TIMER_VSYNC_id TIMER1_id +#define TIMER_VSYNCbit TIMER1bit +#define TIMER_VSYNC_STATUS_TRUN TIMER_STATUS_TRUN1 +#define TIMER_VSYNC_STATUS_TIMIL TIMER_STATUS_TIMIL1 +#define TIMER_VSYNC_STATUS_TOVF TIMER_STATUS_TOVF1 +#endif + +#define LCD_X_RES 320 /* Horizontal Resolution */ +#define LCD_Y_RES 240 /* Vertical Resolution */ +#define DMA_BUS_SIZE 16 + +#define USE_RGB565_16_BIT_PPI + +#ifdef USE_RGB565_16_BIT_PPI +#define LCD_BPP 16 /* Bit Per Pixel */ +#define CLOCKS_PER_PIX 1 +#define CPLD_PIPELINE_DELAY_COR 0 /* NO CPLB */ +#endif + +/* Interface 16/18-bit TFT over an 8-bit wide PPI using a small Programmable Logic Device (CPLD) + * http://blackfin.uclinux.org/gf/project/stamp/frs/?action=FrsReleaseBrowse&frs_package_id=165 + */ + +#ifdef USE_RGB565_8_BIT_PPI +#define LCD_BPP 16 /* Bit Per Pixel */ +#define CLOCKS_PER_PIX 2 +#define CPLD_PIPELINE_DELAY_COR 3 /* RGB565 */ +#endif + +#ifdef USE_RGB888_8_BIT_PPI +#define LCD_BPP 24 /* Bit Per Pixel */ +#define CLOCKS_PER_PIX 3 +#define CPLD_PIPELINE_DELAY_COR 5 /* RGB888 */ +#endif + + /* + * HS and VS timing parameters (all in number of PPI clk ticks) + */ + +#define U_LINE 4 /* Blanking Lines */ + +#define H_ACTPIX (LCD_X_RES * CLOCKS_PER_PIX) /* active horizontal pixel */ +#define H_PERIOD (336 * CLOCKS_PER_PIX) /* HS period */ +#define H_PULSE (2 * CLOCKS_PER_PIX) /* HS pulse width */ +#define H_START (7 * CLOCKS_PER_PIX + CPLD_PIPELINE_DELAY_COR) /* first valid pixel */ + +#define V_LINES (LCD_Y_RES + U_LINE) /* total vertical lines */ +#define V_PULSE (2 * CLOCKS_PER_PIX) /* VS pulse width (1-5 H_PERIODs) */ +#define V_PERIOD (H_PERIOD * V_LINES) /* VS period */ + +#define ACTIVE_VIDEO_MEM_OFFSET ((U_LINE / 2) * LCD_X_RES * (LCD_BPP / 8)) + +#define BFIN_LCD_NBR_PALETTE_ENTRIES 256 + +#define PPI_TX_MODE 0x2 +#define PPI_XFER_TYPE_11 0xC +#define PPI_PORT_CFG_01 0x10 +#define PPI_POLS_1 0x8000 + +#if (CLOCKS_PER_PIX > 1) +#define PPI_PMODE (DLEN_8 | PACK_EN) +#else +#define PPI_PMODE (DLEN_16) +#endif + +#define LQ035_INDEX 0x74 +#define LQ035_DATA 0x76 + +#define LQ035_DRIVER_OUTPUT_CTL 0x1 +#define LQ035_SHUT_CTL 0x11 + +#define LQ035_DRIVER_OUTPUT_MASK (LQ035_LR | LQ035_TB | LQ035_BGR | LQ035_REV) +#define LQ035_DRIVER_OUTPUT_DEFAULT (0x2AEF & ~LQ035_DRIVER_OUTPUT_MASK) + +#define LQ035_SHUT (1 << 0) /* Shutdown */ +#define LQ035_ON (0 << 0) /* Shutdown */ + +struct bfin_lq035q1fb_info { + struct fb_info *fb; + struct device *dev; + struct spi_driver spidrv; + struct bfin_lq035q1fb_disp_info *disp_info; + unsigned char *fb_buffer; /* RGB Buffer */ + dma_addr_t dma_handle; + int lq035_open_cnt; + int irq; + spinlock_t lock; /* lock */ + u32 pseudo_pal[16]; +}; + +static int nocursor; +module_param(nocursor, int, 0644); +MODULE_PARM_DESC(nocursor, "cursor enable/disable"); + +struct spi_control { + unsigned short mode; +}; + +static int lq035q1_control(struct spi_device *spi, unsigned char reg, unsigned short value) +{ + int ret; + u8 regs[3] = { LQ035_INDEX, 0, 0 }; + u8 dat[3] = { LQ035_DATA, 0, 0 }; + + if (!spi) + return -ENODEV; + + regs[2] = reg; + dat[1] = value >> 8; + dat[2] = value & 0xFF; + + ret = spi_write(spi, regs, ARRAY_SIZE(regs)); + ret |= spi_write(spi, dat, ARRAY_SIZE(dat)); + return ret; +} + +static int __devinit lq035q1_spidev_probe(struct spi_device *spi) +{ + int ret; + struct spi_control *ctl; + struct bfin_lq035q1fb_info *info = container_of(spi->dev.driver, + struct bfin_lq035q1fb_info, + spidrv.driver); + + ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); + + if (!ctl) + return -ENOMEM; + + ctl->mode = (info->disp_info->mode & + LQ035_DRIVER_OUTPUT_MASK) | LQ035_DRIVER_OUTPUT_DEFAULT; + + ret = lq035q1_control(spi, LQ035_SHUT_CTL, LQ035_ON); + ret |= lq035q1_control(spi, LQ035_DRIVER_OUTPUT_CTL, ctl->mode); + if (ret) + return ret; + + spi_set_drvdata(spi, ctl); + + return 0; +} + +static int lq035q1_spidev_remove(struct spi_device *spi) +{ + return lq035q1_control(spi, LQ035_SHUT_CTL, LQ035_SHUT); +} + +#ifdef CONFIG_PM +static int lq035q1_spidev_suspend(struct spi_device *spi, pm_message_t state) +{ + return lq035q1_control(spi, LQ035_SHUT_CTL, LQ035_SHUT); +} + +static int lq035q1_spidev_resume(struct spi_device *spi) +{ + int ret; + struct spi_control *ctl = spi_get_drvdata(spi); + + ret = lq035q1_control(spi, LQ035_DRIVER_OUTPUT_CTL, ctl->mode); + if (ret) + return ret; + + return lq035q1_control(spi, LQ035_SHUT_CTL, LQ035_ON); +} +#else +# define lq035q1_spidev_suspend NULL +# define lq035q1_spidev_resume NULL +#endif + +/* Power down all displays on reboot, poweroff or halt */ +static void lq035q1_spidev_shutdown(struct spi_device *spi) +{ + lq035q1_control(spi, LQ035_SHUT_CTL, LQ035_SHUT); +} + +static int lq035q1_backlight(struct bfin_lq035q1fb_info *info, unsigned arg) +{ + if (info->disp_info->use_bl) + gpio_set_value(info->disp_info->gpio_bl, arg); + + return 0; +} + +static void bfin_lq035q1_config_ppi(struct bfin_lq035q1fb_info *fbi) +{ + bfin_write_PPI_DELAY(H_START); + bfin_write_PPI_COUNT(H_ACTPIX - 1); + bfin_write_PPI_FRAME(V_LINES); + + bfin_write_PPI_CONTROL(PPI_TX_MODE | /* output mode , PORT_DIR */ + PPI_XFER_TYPE_11 | /* sync mode XFR_TYPE */ + PPI_PORT_CFG_01 | /* two frame sync PORT_CFG */ + PPI_PMODE | /* 8/16 bit data length / PACK_EN? */ + PPI_POLS_1); /* faling edge syncs POLS */ +} + +static inline void bfin_lq035q1_disable_ppi(void) +{ + bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() & ~PORT_EN); +} + +static inline void bfin_lq035q1_enable_ppi(void) +{ + bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN); +} + +static void bfin_lq035q1_start_timers(void) +{ + enable_gptimers(TIMER_VSYNCbit | TIMER_HSYNCbit); +} + +static void bfin_lq035q1_stop_timers(void) +{ + disable_gptimers(TIMER_HSYNCbit | TIMER_VSYNCbit); + + set_gptimer_status(0, TIMER_HSYNC_STATUS_TRUN | TIMER_VSYNC_STATUS_TRUN | + TIMER_HSYNC_STATUS_TIMIL | TIMER_VSYNC_STATUS_TIMIL | + TIMER_HSYNC_STATUS_TOVF | TIMER_VSYNC_STATUS_TOVF); + +} + +static void bfin_lq035q1_init_timers(void) +{ + + bfin_lq035q1_stop_timers(); + + set_gptimer_period(TIMER_HSYNC_id, H_PERIOD); + set_gptimer_pwidth(TIMER_HSYNC_id, H_PULSE); + set_gptimer_config(TIMER_HSYNC_id, TIMER_MODE_PWM | TIMER_PERIOD_CNT | + TIMER_TIN_SEL | TIMER_CLK_SEL| + TIMER_EMU_RUN); + + set_gptimer_period(TIMER_VSYNC_id, V_PERIOD); + set_gptimer_pwidth(TIMER_VSYNC_id, V_PULSE); + set_gptimer_config(TIMER_VSYNC_id, TIMER_MODE_PWM | TIMER_PERIOD_CNT | + TIMER_TIN_SEL | TIMER_CLK_SEL | + TIMER_EMU_RUN); + +} + +static void bfin_lq035q1_config_dma(struct bfin_lq035q1fb_info *fbi) +{ + + set_dma_config(CH_PPI, + set_bfin_dma_config(DIR_READ, DMA_FLOW_AUTO, + INTR_DISABLE, DIMENSION_2D, + DATA_SIZE_16, + DMA_NOSYNC_KEEP_DMA_BUF)); + set_dma_x_count(CH_PPI, (LCD_X_RES * LCD_BPP) / DMA_BUS_SIZE); + set_dma_x_modify(CH_PPI, DMA_BUS_SIZE / 8); + set_dma_y_count(CH_PPI, V_LINES); + + set_dma_y_modify(CH_PPI, DMA_BUS_SIZE / 8); + set_dma_start_addr(CH_PPI, (unsigned long)fbi->fb_buffer); + +} + +#if (CLOCKS_PER_PIX == 1) +static const u16 ppi0_req_16[] = {P_PPI0_CLK, P_PPI0_FS1, P_PPI0_FS2, + P_PPI0_D0, P_PPI0_D1, P_PPI0_D2, + P_PPI0_D3, P_PPI0_D4, P_PPI0_D5, + P_PPI0_D6, P_PPI0_D7, P_PPI0_D8, + P_PPI0_D9, P_PPI0_D10, P_PPI0_D11, + P_PPI0_D12, P_PPI0_D13, P_PPI0_D14, + P_PPI0_D15, 0}; +#else +static const u16 ppi0_req_16[] = {P_PPI0_CLK, P_PPI0_FS1, P_PPI0_FS2, + P_PPI0_D0, P_PPI0_D1, P_PPI0_D2, + P_PPI0_D3, P_PPI0_D4, P_PPI0_D5, + P_PPI0_D6, P_PPI0_D7, 0}; +#endif + +static inline void bfin_lq035q1_free_ports(void) +{ + peripheral_free_list(ppi0_req_16); + if (ANOMALY_05000400) + gpio_free(P_IDENT(P_PPI0_FS3)); +} + +static int __devinit bfin_lq035q1_request_ports(struct platform_device *pdev) +{ + /* ANOMALY_05000400 - PPI Does Not Start Properly In Specific Mode: + * Drive PPI_FS3 Low + */ + if (ANOMALY_05000400) { + int ret = gpio_request(P_IDENT(P_PPI0_FS3), "PPI_FS3"); + if (ret) + return ret; + gpio_direction_output(P_IDENT(P_PPI0_FS3), 0); + } + + if (peripheral_request_list(ppi0_req_16, DRIVER_NAME)) { + dev_err(&pdev->dev, "requesting peripherals failed\n"); + return -EFAULT; + } + + return 0; +} + +static int bfin_lq035q1_fb_open(struct fb_info *info, int user) +{ + struct bfin_lq035q1fb_info *fbi = info->par; + + spin_lock(&fbi->lock); + fbi->lq035_open_cnt++; + + if (fbi->lq035_open_cnt <= 1) { + + bfin_lq035q1_disable_ppi(); + SSYNC(); + + bfin_lq035q1_config_dma(fbi); + bfin_lq035q1_config_ppi(fbi); + bfin_lq035q1_init_timers(); + + /* start dma */ + enable_dma(CH_PPI); + bfin_lq035q1_enable_ppi(); + bfin_lq035q1_start_timers(); + lq035q1_backlight(fbi, 1); + } + + spin_unlock(&fbi->lock); + + return 0; +} + +static int bfin_lq035q1_fb_release(struct fb_info *info, int user) +{ + struct bfin_lq035q1fb_info *fbi = info->par; + + spin_lock(&fbi->lock); + + fbi->lq035_open_cnt--; + + if (fbi->lq035_open_cnt <= 0) { + lq035q1_backlight(fbi, 0); + bfin_lq035q1_disable_ppi(); + SSYNC(); + disable_dma(CH_PPI); + bfin_lq035q1_stop_timers(); + } + + spin_unlock(&fbi->lock); + + return 0; +} + +static int bfin_lq035q1_fb_check_var(struct fb_var_screeninfo *var, + struct fb_info *info) +{ + switch (var->bits_per_pixel) { +#if (LCD_BPP == 24) + case 24:/* TRUECOLOUR, 16m */ +#else + case 16:/* DIRECTCOLOUR, 64k */ +#endif + var->red.offset = info->var.red.offset; + var->green.offset = info->var.green.offset; + var->blue.offset = info->var.blue.offset; + var->red.length = info->var.red.length; + var->green.length = info->var.green.length; + var->blue.length = info->var.blue.length; + var->transp.offset = 0; + var->transp.length = 0; + var->transp.msb_right = 0; + var->red.msb_right = 0; + var->green.msb_right = 0; + var->blue.msb_right = 0; + break; + default: + pr_debug("%s: depth not supported: %u BPP\n", __func__, + var->bits_per_pixel); + return -EINVAL; + } + + if (info->var.xres != var->xres || info->var.yres != var->yres || + info->var.xres_virtual != var->xres_virtual || + info->var.yres_virtual != var->yres_virtual) { + pr_debug("%s: Resolution not supported: X%u x Y%u \n", + __func__, var->xres, var->yres); + return -EINVAL; + } + + /* + * Memory limit + */ + + if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) { + pr_debug("%s: Memory Limit requested yres_virtual = %u\n", + __func__, var->yres_virtual); + return -ENOMEM; + } + + + return 0; +} + +int bfin_lq035q1_fb_cursor(struct fb_info *info, struct fb_cursor *cursor) +{ + if (nocursor) + return 0; + else + return -EINVAL; /* just to force soft_cursor() call */ +} + +static int bfin_lq035q1_fb_setcolreg(u_int regno, u_int red, u_int green, + u_int blue, u_int transp, + struct fb_info *info) +{ + if (regno >= BFIN_LCD_NBR_PALETTE_ENTRIES) + return -EINVAL; + + if (info->var.grayscale) { + /* grayscale = 0.30*R + 0.59*G + 0.11*B */ + red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8; + } + + if (info->fix.visual == FB_VISUAL_TRUECOLOR) { + + u32 value; + /* Place color in the pseudopalette */ + if (regno > 16) + return -EINVAL; + + red >>= (16 - info->var.red.length); + green >>= (16 - info->var.green.length); + blue >>= (16 - info->var.blue.length); + + value = (red << info->var.red.offset) | + (green << info->var.green.offset) | + (blue << info->var.blue.offset); + value &= 0xFFFFFF; + + ((u32 *) (info->pseudo_palette))[regno] = value; + + } + + return 0; +} + +static struct fb_ops bfin_lq035q1_fb_ops = { + .owner = THIS_MODULE, + .fb_open = bfin_lq035q1_fb_open, + .fb_release = bfin_lq035q1_fb_release, + .fb_check_var = bfin_lq035q1_fb_check_var, + .fb_fillrect = cfb_fillrect, + .fb_copyarea = cfb_copyarea, + .fb_imageblit = cfb_imageblit, + .fb_cursor = bfin_lq035q1_fb_cursor, + .fb_setcolreg = bfin_lq035q1_fb_setcolreg, +}; + +static irqreturn_t bfin_lq035q1_irq_error(int irq, void *dev_id) +{ + /*struct bfin_lq035q1fb_info *info = (struct bfin_lq035q1fb_info *)dev_id;*/ + + u16 status = bfin_read_PPI_STATUS(); + bfin_write_PPI_STATUS(-1); + + if (status) { + bfin_lq035q1_disable_ppi(); + disable_dma(CH_PPI); + + /* start dma */ + enable_dma(CH_PPI); + bfin_lq035q1_enable_ppi(); + bfin_write_PPI_STATUS(-1); + } + + return IRQ_HANDLED; +} + +static int __devinit bfin_lq035q1_probe(struct platform_device *pdev) +{ + struct bfin_lq035q1fb_info *info; + struct fb_info *fbinfo; + int ret; + + ret = request_dma(CH_PPI, DRIVER_NAME"_CH_PPI"); + if (ret < 0) { + dev_err(&pdev->dev, "PPI DMA unavailable\n"); + goto out1; + } + + fbinfo = framebuffer_alloc(sizeof(*info), &pdev->dev); + if (!fbinfo) { + ret = -ENOMEM; + goto out2; + } + + info = fbinfo->par; + info->fb = fbinfo; + info->dev = &pdev->dev; + + info->disp_info = pdev->dev.platform_data; + + platform_set_drvdata(pdev, fbinfo); + + strcpy(fbinfo->fix.id, DRIVER_NAME); + + fbinfo->fix.type = FB_TYPE_PACKED_PIXELS; + fbinfo->fix.type_aux = 0; + fbinfo->fix.xpanstep = 0; + fbinfo->fix.ypanstep = 0; + fbinfo->fix.ywrapstep = 0; + fbinfo->fix.accel = FB_ACCEL_NONE; + fbinfo->fix.visual = FB_VISUAL_TRUECOLOR; + + fbinfo->var.nonstd = 0; + fbinfo->var.activate = FB_ACTIVATE_NOW; + fbinfo->var.height = -1; + fbinfo->var.width = -1; + fbinfo->var.accel_flags = 0; + fbinfo->var.vmode = FB_VMODE_NONINTERLACED; + + fbinfo->var.xres = LCD_X_RES; + fbinfo->var.xres_virtual = LCD_X_RES; + fbinfo->var.yres = LCD_Y_RES; + fbinfo->var.yres_virtual = LCD_Y_RES; + fbinfo->var.bits_per_pixel = LCD_BPP; + + if (info->disp_info->mode & LQ035_BGR) { +#if (LCD_BPP == 24) + fbinfo->var.red.offset = 0; + fbinfo->var.green.offset = 8; + fbinfo->var.blue.offset = 16; +#else + fbinfo->var.red.offset = 0; + fbinfo->var.green.offset = 5; + fbinfo->var.blue.offset = 11; +#endif + } else { +#if (LCD_BPP == 24) + fbinfo->var.red.offset = 16; + fbinfo->var.green.offset = 8; + fbinfo->var.blue.offset = 0; +#else + fbinfo->var.red.offset = 11; + fbinfo->var.green.offset = 5; + fbinfo->var.blue.offset = 0; +#endif + } + + fbinfo->var.transp.offset = 0; + +#if (LCD_BPP == 24) + fbinfo->var.red.length = 8; + fbinfo->var.green.length = 8; + fbinfo->var.blue.length = 8; +#else + fbinfo->var.red.length = 5; + fbinfo->var.green.length = 6; + fbinfo->var.blue.length = 5; +#endif + + fbinfo->var.transp.length = 0; + + fbinfo->fix.smem_len = LCD_X_RES * LCD_Y_RES * LCD_BPP / 8 + + ACTIVE_VIDEO_MEM_OFFSET; + + fbinfo->fix.line_length = fbinfo->var.xres_virtual * + fbinfo->var.bits_per_pixel / 8; + + + fbinfo->fbops = &bfin_lq035q1_fb_ops; + fbinfo->flags = FBINFO_FLAG_DEFAULT; + + info->fb_buffer = + dma_alloc_coherent(NULL, fbinfo->fix.smem_len, &info->dma_handle, + GFP_KERNEL); + + if (NULL == info->fb_buffer) { + dev_err(&pdev->dev, "couldn't allocate dma buffer\n"); + ret = -ENOMEM; + goto out3; + } + + fbinfo->screen_base = (void *)info->fb_buffer + ACTIVE_VIDEO_MEM_OFFSET; + fbinfo->fix.smem_start = (int)info->fb_buffer + ACTIVE_VIDEO_MEM_OFFSET; + + fbinfo->fbops = &bfin_lq035q1_fb_ops; + + fbinfo->pseudo_palette = &info->pseudo_pal; + + ret = fb_alloc_cmap(&fbinfo->cmap, BFIN_LCD_NBR_PALETTE_ENTRIES, 0); + if (ret < 0) { + dev_err(&pdev->dev, "failed to allocate colormap (%d entries)\n", + BFIN_LCD_NBR_PALETTE_ENTRIES); + goto out4; + } + + ret = bfin_lq035q1_request_ports(pdev); + if (ret) { + dev_err(&pdev->dev, "couldn't request gpio port\n"); + goto out6; + } + + info->irq = platform_get_irq(pdev, 0); + if (info->irq < 0) { + ret = -EINVAL; + goto out7; + } + + ret = request_irq(info->irq, bfin_lq035q1_irq_error, IRQF_DISABLED, + DRIVER_NAME" PPI ERROR", info); + if (ret < 0) { + dev_err(&pdev->dev, "unable to request PPI ERROR IRQ\n"); + goto out7; + } + + info->spidrv.driver.name = DRIVER_NAME"-spi"; + info->spidrv.probe = lq035q1_spidev_probe; + info->spidrv.remove = __devexit_p(lq035q1_spidev_remove); + info->spidrv.shutdown = lq035q1_spidev_shutdown; + info->spidrv.suspend = lq035q1_spidev_suspend; + info->spidrv.resume = lq035q1_spidev_resume; + + ret = spi_register_driver(&info->spidrv); + if (ret < 0) { + dev_err(&pdev->dev, "couldn't register SPI Interface\n"); + goto out8; + } + + if (info->disp_info->use_bl) { + ret = gpio_request(info->disp_info->gpio_bl, "LQ035 Backlight"); + + if (ret) { + dev_err(&pdev->dev, "failed to request GPIO %d\n", + info->disp_info->gpio_bl); + goto out9; + } + gpio_direction_output(info->disp_info->gpio_bl, 0); + } + + ret = register_framebuffer(fbinfo); + if (ret < 0) { + dev_err(&pdev->dev, "unable to register framebuffer\n"); + goto out10; + } + + dev_info(&pdev->dev, "%dx%d %d-bit RGB FrameBuffer initialized\n", + LCD_X_RES, LCD_Y_RES, LCD_BPP); + + return 0; + + out10: + if (info->disp_info->use_bl) + gpio_free(info->disp_info->gpio_bl); + out9: + spi_unregister_driver(&info->spidrv); + out8: + free_irq(info->irq, info); + out7: + bfin_lq035q1_free_ports(); + out6: + fb_dealloc_cmap(&fbinfo->cmap); + out4: + dma_free_coherent(NULL, fbinfo->fix.smem_len, info->fb_buffer, + info->dma_handle); + out3: + framebuffer_release(fbinfo); + out2: + free_dma(CH_PPI); + out1: + platform_set_drvdata(pdev, NULL); + + return ret; +} + +static int __devexit bfin_lq035q1_remove(struct platform_device *pdev) +{ + struct fb_info *fbinfo = platform_get_drvdata(pdev); + struct bfin_lq035q1fb_info *info = fbinfo->par; + + if (info->disp_info->use_bl) + gpio_free(info->disp_info->gpio_bl); + + spi_unregister_driver(&info->spidrv); + + unregister_framebuffer(fbinfo); + + free_dma(CH_PPI); + free_irq(info->irq, info); + + if (info->fb_buffer != NULL) + dma_free_coherent(NULL, fbinfo->fix.smem_len, info->fb_buffer, + info->dma_handle); + + fb_dealloc_cmap(&fbinfo->cmap); + + bfin_lq035q1_free_ports(); + + platform_set_drvdata(pdev, NULL); + framebuffer_release(fbinfo); + + dev_info(&pdev->dev, "unregistered LCD driver\n"); + + return 0; +} + +#ifdef CONFIG_PM +static int bfin_lq035q1_suspend(struct device *dev) +{ + struct fb_info *fbinfo = dev_get_drvdata(dev); + struct bfin_lq035q1fb_info *info = fbinfo->par; + + if (info->lq035_open_cnt) { + lq035q1_backlight(info, 0); + bfin_lq035q1_disable_ppi(); + SSYNC(); + disable_dma(CH_PPI); + bfin_lq035q1_stop_timers(); + bfin_write_PPI_STATUS(-1); + } + + return 0; +} + +static int bfin_lq035q1_resume(struct device *dev) +{ + struct fb_info *fbinfo = dev_get_drvdata(dev); + struct bfin_lq035q1fb_info *info = fbinfo->par; + + if (info->lq035_open_cnt) { + bfin_lq035q1_disable_ppi(); + SSYNC(); + + bfin_lq035q1_config_dma(info); + bfin_lq035q1_config_ppi(info); + bfin_lq035q1_init_timers(); + + /* start dma */ + enable_dma(CH_PPI); + bfin_lq035q1_enable_ppi(); + bfin_lq035q1_start_timers(); + lq035q1_backlight(info, 1); + } + + return 0; +} + +static struct dev_pm_ops bfin_lq035q1_dev_pm_ops = { + .suspend = bfin_lq035q1_suspend, + .resume = bfin_lq035q1_resume, +}; +#endif + +static struct platform_driver bfin_lq035q1_driver = { + .probe = bfin_lq035q1_probe, + .remove = __devexit_p(bfin_lq035q1_remove), + .driver = { + .name = DRIVER_NAME, +#ifdef CONFIG_PM + .pm = &bfin_lq035q1_dev_pm_ops, +#endif + }, +}; + +static int __init bfin_lq035q1_driver_init(void) +{ + return platform_driver_register(&bfin_lq035q1_driver); +} +module_init(bfin_lq035q1_driver_init); + +static void __exit bfin_lq035q1_driver_cleanup(void) +{ + platform_driver_unregister(&bfin_lq035q1_driver); +} +module_exit(bfin_lq035q1_driver_cleanup); + +MODULE_DESCRIPTION("Blackfin TFT LCD Driver"); +MODULE_LICENSE("GPL"); -- cgit v0.10.2 From 6841bcfa6de5712a04531480fd1970ba462461f9 Mon Sep 17 00:00:00 2001 From: Michael Hennerich Date: Tue, 15 Dec 2009 16:46:21 -0800 Subject: fbdev: bfin-t350mcqb-fb: handle all resources in suspend/resume The LCD commands DMA/timers as well as PPI which we need to save/restore. Signed-off-by: Michael Hennerich Signed-off-by: Mike Frysinger Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/bfin-t350mcqb-fb.c b/drivers/video/bfin-t350mcqb-fb.c index 5cc36cf..46cb07f 100644 --- a/drivers/video/bfin-t350mcqb-fb.c +++ b/drivers/video/bfin-t350mcqb-fb.c @@ -634,17 +634,35 @@ static int __devexit bfin_t350mcqb_remove(struct platform_device *pdev) #ifdef CONFIG_PM static int bfin_t350mcqb_suspend(struct platform_device *pdev, pm_message_t state) { - bfin_t350mcqb_disable_ppi(); - disable_dma(CH_PPI); - bfin_write_PPI_STATUS(0xFFFF); + struct fb_info *fbinfo = platform_get_drvdata(pdev); + struct bfin_t350mcqbfb_info *fbi = fbinfo->par; + + if (fbi->lq043_open_cnt) { + bfin_t350mcqb_disable_ppi(); + disable_dma(CH_PPI); + bfin_t350mcqb_stop_timers(); + bfin_write_PPI_STATUS(-1); + } + return 0; } static int bfin_t350mcqb_resume(struct platform_device *pdev) { - enable_dma(CH_PPI); - bfin_t350mcqb_enable_ppi(); + struct fb_info *fbinfo = platform_get_drvdata(pdev); + struct bfin_t350mcqbfb_info *fbi = fbinfo->par; + + if (fbi->lq043_open_cnt) { + bfin_t350mcqb_config_dma(fbi); + bfin_t350mcqb_config_ppi(fbi); + bfin_t350mcqb_init_timers(); + + /* start dma */ + enable_dma(CH_PPI); + bfin_t350mcqb_enable_ppi(); + bfin_t350mcqb_start_timers(); + } return 0; } -- cgit v0.10.2 From 09335865c34c2e74c7ba0a2c7224e302e0f1a5d8 Mon Sep 17 00:00:00 2001 From: Michael Hennerich Date: Tue, 15 Dec 2009 16:46:22 -0800 Subject: fbdev: bfin-t350mcqb-fb: fix LCD dimensions The height/width framebuffer members need to be set in millimeters so that software can do proper scaling. Signed-off-by: Michael Hennerich Signed-off-by: Mike Frysinger Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/bfin-t350mcqb-fb.c b/drivers/video/bfin-t350mcqb-fb.c index 46cb07f..2549c53 100644 --- a/drivers/video/bfin-t350mcqb-fb.c +++ b/drivers/video/bfin-t350mcqb-fb.c @@ -487,8 +487,8 @@ static int __devinit bfin_t350mcqb_probe(struct platform_device *pdev) fbinfo->var.nonstd = 0; fbinfo->var.activate = FB_ACTIVATE_NOW; - fbinfo->var.height = -1; - fbinfo->var.width = -1; + fbinfo->var.height = 53; + fbinfo->var.width = 70; fbinfo->var.accel_flags = 0; fbinfo->var.vmode = FB_VMODE_NONINTERLACED; -- cgit v0.10.2 From 5ab948153a1e409afdc0f78a376ad3710df5b109 Mon Sep 17 00:00:00 2001 From: Roel Kluin Date: Tue, 15 Dec 2009 16:46:23 -0800 Subject: fbdev: TV_PALN bit set twice in sisfb_detect_VB_connect() The TV_PALN bit was tested twice, replace one by TV_PALM. Signed-off-by: Roel Kluin Cc: Thomas Winischhofer Cc: Krzysztof Helt Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/sis/sis_main.c b/drivers/video/sis/sis_main.c index a4e05e4..9d2b6bc 100644 --- a/drivers/video/sis/sis_main.c +++ b/drivers/video/sis/sis_main.c @@ -2115,7 +2115,7 @@ sisfb_detect_VB_connect(struct sis_video_info *ivideo) if( (!(ivideo->vbflags2 & VB2_SISBRIDGE)) && (!((ivideo->sisvga_engine == SIS_315_VGA) && (ivideo->vbflags2 & VB2_CHRONTEL))) ) { - if(ivideo->sisfb_tvstd & (TV_PALN | TV_PALN | TV_NTSCJ)) { + if(ivideo->sisfb_tvstd & (TV_PALM | TV_PALN | TV_NTSCJ)) { ivideo->sisfb_tvstd = -1; printk(KERN_ERR "sisfb: PALM/PALN/NTSCJ not supported\n"); } -- cgit v0.10.2 From 2cb96f86628d6e97fcbda5fe4d8d74876239834c Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 15 Dec 2009 16:46:24 -0800 Subject: fbdev: drop custom atoi from drivers/video/modedb.c Kernel has simple_strtol() implementation which could be used as atoi(). Signed-off-by: Andy Shevchenko Cc: Krzysztof Helt Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/modedb.c b/drivers/video/modedb.c index 34e4e79..0129f1b 100644 --- a/drivers/video/modedb.c +++ b/drivers/video/modedb.c @@ -13,6 +13,7 @@ #include #include +#include #undef DEBUG @@ -402,21 +403,6 @@ const struct fb_videomode vesa_modes[] = { EXPORT_SYMBOL(vesa_modes); #endif /* CONFIG_FB_MODE_HELPERS */ -static int my_atoi(const char *name) -{ - int val = 0; - - for (;; name++) { - switch (*name) { - case '0' ... '9': - val = 10*val+(*name-'0'); - break; - default: - return val; - } - } -} - /** * fb_try_mode - test a video mode * @var: frame buffer user defined part of display @@ -539,7 +525,7 @@ int fb_find_mode(struct fb_var_screeninfo *var, namelen = i; if (!refresh_specified && !bpp_specified && !yres_specified) { - refresh = my_atoi(&name[i+1]); + refresh = simple_strtol(&name[i+1], NULL, 10); refresh_specified = 1; if (cvt || rb) cvt = 0; @@ -549,7 +535,7 @@ int fb_find_mode(struct fb_var_screeninfo *var, case '-': namelen = i; if (!bpp_specified && !yres_specified) { - bpp = my_atoi(&name[i+1]); + bpp = simple_strtol(&name[i+1], NULL, 10); bpp_specified = 1; if (cvt || rb) cvt = 0; @@ -558,7 +544,7 @@ int fb_find_mode(struct fb_var_screeninfo *var, break; case 'x': if (!yres_specified) { - yres = my_atoi(&name[i+1]); + yres = simple_strtol(&name[i+1], NULL, 10); yres_specified = 1; } else goto done; @@ -586,7 +572,7 @@ int fb_find_mode(struct fb_var_screeninfo *var, } } if (i < 0 && yres_specified) { - xres = my_atoi(name); + xres = simple_strtol(name, NULL, 10); res_specified = 1; } done: -- cgit v0.10.2 From 2f390380ca69e1617cdddb12d8da94f0a9f4319d Mon Sep 17 00:00:00 2001 From: Krzysztof Helt Date: Tue, 15 Dec 2009 16:46:25 -0800 Subject: fbdev: add palette register check to several drivers Add check if palette register number is in correct range for few drivers which miss it. The regno value comes indirectly from user space. Two drivers has converted check from BUG_ON() macro to just return an error (non-zero value). Signed-off-by: Krzysztof Helt Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/atafb.c b/drivers/video/atafb.c index b7687c5..2051c9d 100644 --- a/drivers/video/atafb.c +++ b/drivers/video/atafb.c @@ -2245,6 +2245,9 @@ static int ext_setcolreg(unsigned int regno, unsigned int red, if (regno > 255) return 1; + if (regno > 255) + return 1; + switch (external_card_type) { case IS_VGA: OUTB(0x3c8, regno); diff --git a/drivers/video/ep93xx-fb.c b/drivers/video/ep93xx-fb.c index bd9d46f..27aab4a 100644 --- a/drivers/video/ep93xx-fb.c +++ b/drivers/video/ep93xx-fb.c @@ -358,6 +358,8 @@ static int ep93xxfb_setcolreg(unsigned int regno, unsigned int red, switch (info->fix.visual) { case FB_VISUAL_PSEUDOCOLOR: + if (regno > 255) + return 1; rgb = ((red & 0xff00) << 8) | (green & 0xff00) | ((blue & 0xff00) >> 8); diff --git a/drivers/video/maxinefb.c b/drivers/video/maxinefb.c index 5e91c2b..7854c7a 100644 --- a/drivers/video/maxinefb.c +++ b/drivers/video/maxinefb.c @@ -92,6 +92,9 @@ static int maxinefb_setcolreg(unsigned regno, unsigned red, unsigned green, /* value to be written into the palette reg. */ unsigned long hw_colorvalue = 0; + if (regno > 255) + return 1; + red >>= 8; /* The cmap fields are 16 bits */ green >>= 8; /* wide, but the harware colormap */ blue >>= 8; /* registers are only 8 bits wide */ diff --git a/drivers/video/pmag-ba-fb.c b/drivers/video/pmag-ba-fb.c index 0573ec6..0f361b6 100644 --- a/drivers/video/pmag-ba-fb.c +++ b/drivers/video/pmag-ba-fb.c @@ -98,7 +98,8 @@ static int pmagbafb_setcolreg(unsigned int regno, unsigned int red, { struct pmagbafb_par *par = info->par; - BUG_ON(regno >= info->cmap.len); + if (regno >= info->cmap.len) + return 1; red >>= 8; /* The cmap fields are 16 bits */ green >>= 8; /* wide, but the hardware colormap */ diff --git a/drivers/video/pmagb-b-fb.c b/drivers/video/pmagb-b-fb.c index 9874872..2de0806 100644 --- a/drivers/video/pmagb-b-fb.c +++ b/drivers/video/pmagb-b-fb.c @@ -102,7 +102,8 @@ static int pmagbbfb_setcolreg(unsigned int regno, unsigned int red, { struct pmagbbfb_par *par = info->par; - BUG_ON(regno >= info->cmap.len); + if (regno >= info->cmap.len) + return 1; red >>= 8; /* The cmap fields are 16 bits */ green >>= 8; /* wide, but the hardware colormap */ -- cgit v0.10.2 From d2a34c13e7ccec5d06eafd60e6f80ea531b34668 Mon Sep 17 00:00:00 2001 From: Julia Lawall Date: Tue, 15 Dec 2009 16:46:26 -0800 Subject: drivers/video: Move dereference after NULL test If the NULL test on fbi is needed, then the dereference should be after the NULL test. A simplified version of the semantic match that detects this problem is as follows (http://coccinelle.lip6.fr/): // @match exists@ expression x, E; identifier fld; @@ * x->fld ... when != \(x = E\|&x\) * x == NULL // Signed-off-by: Julia Lawall Cc: Krzysztof Helt Cc: Eric Miao Cc: Daniel Mack Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/pxafb.c b/drivers/video/pxafb.c index b7e5805..415858b 100644 --- a/drivers/video/pxafb.c +++ b/drivers/video/pxafb.c @@ -1221,13 +1221,14 @@ static void setup_smart_timing(struct pxafb_info *fbi, static int pxafb_smart_thread(void *arg) { struct pxafb_info *fbi = arg; - struct pxafb_mach_info *inf = fbi->dev->platform_data; + struct pxafb_mach_info *inf; - if (!fbi || !inf->smart_update) { + if (!fbi || !fbi->dev->platform_data->smart_update) { pr_err("%s: not properly initialized, thread terminated\n", __func__); return -EINVAL; } + inf = fbi->dev->platform_data; pr_debug("%s(): task starting\n", __func__); -- cgit v0.10.2 From 2ec509b96cce5e6d19ee43fdd818eab78e76328d Mon Sep 17 00:00:00 2001 From: Valentin Sitdikov Date: Tue, 15 Dec 2009 16:46:28 -0800 Subject: mb862xxfb: add acceleration support for Coral-P/Coral-PA. * imageblt * copyarea * fillrect [akpm@linux-foundation.org: clean up comments, remove do-nothing tests, use min()] [akpm@linux-foundation.org: fix Alexandr's email address] [akpm@linux-foundation.org: export mb862xxfb_init_accel] Signed-off-by: Valentin Sitdikov Cc: Krzysztof Helt Cc: Anatolij Gustschin Signed-off-by: Alexander Shishkin Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/mb862xx/Makefile b/drivers/video/mb862xx/Makefile index 0766481..d777771 100644 --- a/drivers/video/mb862xx/Makefile +++ b/drivers/video/mb862xx/Makefile @@ -2,4 +2,4 @@ # Makefile for the MB862xx framebuffer driver # -obj-$(CONFIG_FB_MB862XX) := mb862xxfb.o +obj-$(CONFIG_FB_MB862XX) := mb862xxfb.o mb862xxfb_accel.o diff --git a/drivers/video/mb862xx/mb862xxfb.c b/drivers/video/mb862xx/mb862xxfb.c index a28e3cf..fabb0c5 100644 --- a/drivers/video/mb862xx/mb862xxfb.c +++ b/drivers/video/mb862xx/mb862xxfb.c @@ -214,6 +214,8 @@ static int mb862xxfb_set_par(struct fb_info *fbi) unsigned long reg, sc; dev_dbg(par->dev, "%s\n", __func__); + if (par->type == BT_CORALP) + mb862xxfb_init_accel(fbi, fbi->var.xres); if (par->pre_init) return 0; @@ -453,6 +455,18 @@ static ssize_t mb862xxfb_show_dispregs(struct device *dev, ptr += sprintf(ptr, "%08x = %08x\n", reg, inreg(disp, reg)); + for (reg = 0x400; reg <= 0x410; reg += 4) + ptr += sprintf(ptr, "geo %08x = %08x\n", + reg, inreg(geo, reg)); + + for (reg = 0x400; reg <= 0x410; reg += 4) + ptr += sprintf(ptr, "draw %08x = %08x\n", + reg, inreg(draw, reg)); + + for (reg = 0x440; reg <= 0x450; reg += 4) + ptr += sprintf(ptr, "draw %08x = %08x\n", + reg, inreg(draw, reg)); + return ptr - buf; } diff --git a/drivers/video/mb862xx/mb862xxfb.h b/drivers/video/mb862xx/mb862xxfb.h index c4c8f4d..d7e7cb7 100644 --- a/drivers/video/mb862xx/mb862xxfb.h +++ b/drivers/video/mb862xx/mb862xxfb.h @@ -61,6 +61,8 @@ struct mb862xxfb_par { u32 pseudo_palette[16]; }; +extern void mb862xxfb_init_accel(struct fb_info *info, int xres); + #if defined(CONFIG_FB_MB862XX_LIME) && defined(CONFIG_FB_MB862XX_PCI_GDC) #error "Select Lime GDC or CoralP/Carmine support, but not both together" #endif diff --git a/drivers/video/mb862xx/mb862xxfb_accel.c b/drivers/video/mb862xx/mb862xxfb_accel.c new file mode 100644 index 0000000..0492560 --- /dev/null +++ b/drivers/video/mb862xx/mb862xxfb_accel.c @@ -0,0 +1,331 @@ +/* + * drivers/mb862xx/mb862xxfb_accel.c + * + * Fujitsu Carmine/Coral-P(A)/Lime framebuffer driver acceleration support + * + * (C) 2007 Alexander Shishkin + * (C) 2009 Valentin Sitdikov + * (C) 2009 Siemens AG + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ +#include +#include +#include +#include +#include +#if defined(CONFIG_OF) +#include +#endif +#include "mb862xxfb.h" +#include "mb862xx_reg.h" +#include "mb862xxfb_accel.h" + +static void mb862xxfb_write_fifo(u32 count, u32 *data, struct fb_info *info) +{ + struct mb862xxfb_par *par = info->par; + static u32 free; + + u32 total = 0; + while (total < count) { + if (free) { + outreg(geo, GDC_GEO_REG_INPUT_FIFO, data[total]); + total++; + free--; + } else { + free = (u32) inreg(draw, GDC_REG_FIFO_COUNT); + } + } +} + +static void mb86290fb_copyarea(struct fb_info *info, + const struct fb_copyarea *area) +{ + __u32 cmd[6]; + + cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP; + /* Set raster operation */ + cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9); + cmd[2] = GDC_TYPE_BLTCOPYP << 24; + + if (area->sx >= area->dx && area->sy >= area->dy) + cmd[2] |= GDC_CMD_BLTCOPY_TOP_LEFT << 16; + else if (area->sx >= area->dx && area->sy <= area->dy) + cmd[2] |= GDC_CMD_BLTCOPY_BOTTOM_LEFT << 16; + else if (area->sx <= area->dx && area->sy >= area->dy) + cmd[2] |= GDC_CMD_BLTCOPY_TOP_RIGHT << 16; + else + cmd[2] |= GDC_CMD_BLTCOPY_BOTTOM_RIGHT << 16; + + cmd[3] = (area->sy << 16) | area->sx; + cmd[4] = (area->dy << 16) | area->dx; + cmd[5] = (area->height << 16) | area->width; + mb862xxfb_write_fifo(6, cmd, info); +} + +/* + * Fill in the cmd array /GDC FIFO commands/ to draw a 1bit image. + * Make sure cmd has enough room! + */ +static void mb86290fb_imageblit1(u32 *cmd, u16 step, u16 dx, u16 dy, + u16 width, u16 height, u32 fgcolor, + u32 bgcolor, const struct fb_image *image, + struct fb_info *info) +{ + int i; + unsigned const char *line; + u16 bytes; + + /* set colors and raster operation regs */ + cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP; + /* Set raster operation */ + cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9); + cmd[2] = + (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_FORE_COLOR << 16); + cmd[3] = fgcolor; + cmd[4] = + (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_BACK_COLOR << 16); + cmd[5] = bgcolor; + + i = 0; + line = image->data; + bytes = (image->width + 7) >> 3; + + /* and the image */ + cmd[6] = (GDC_TYPE_DRAWBITMAPP << 24) | + (GDC_CMD_BITMAP << 16) | (2 + (step * height)); + cmd[7] = (dy << 16) | dx; + cmd[8] = (height << 16) | width; + + while (i < height) { + memcpy(&cmd[9 + i * step], line, step << 2); +#ifdef __LITTLE_ENDIAN + { + int k = 0; + for (k = 0; k < step; k++) + cmd[9 + i * step + k] = + cpu_to_be32(cmd[9 + i * step + k]); + } +#endif + line += bytes; + i++; + } +} + +/* + * Fill in the cmd array /GDC FIFO commands/ to draw a 8bit image. + * Make sure cmd has enough room! + */ +static void mb86290fb_imageblit8(u32 *cmd, u16 step, u16 dx, u16 dy, + u16 width, u16 height, u32 fgcolor, + u32 bgcolor, const struct fb_image *image, + struct fb_info *info) +{ + int i, j; + unsigned const char *line, *ptr; + u16 bytes; + + cmd[0] = (GDC_TYPE_DRAWBITMAPP << 24) | + (GDC_CMD_BLT_DRAW << 16) | (2 + (height * step)); + cmd[1] = (dy << 16) | dx; + cmd[2] = (height << 16) | width; + + i = 0; + line = ptr = image->data; + bytes = image->width; + + while (i < height) { + ptr = line; + for (j = 0; j < step; j++) { + cmd[3 + i * step + j] = + (((u32 *) (info->pseudo_palette))[*ptr]) & 0xffff; + ptr++; + cmd[3 + i * step + j] |= + ((((u32 *) (info-> + pseudo_palette))[*ptr]) & 0xffff) << 16; + ptr++; + } + + line += bytes; + i++; + } +} + +/* + * Fill in the cmd array /GDC FIFO commands/ to draw a 16bit image. + * Make sure cmd has enough room! + */ +static void mb86290fb_imageblit16(u32 *cmd, u16 step, u16 dx, u16 dy, + u16 width, u16 height, u32 fgcolor, + u32 bgcolor, const struct fb_image *image, + struct fb_info *info) +{ + int i; + unsigned const char *line; + u16 bytes; + + i = 0; + line = image->data; + bytes = image->width << 1; + + cmd[0] = (GDC_TYPE_DRAWBITMAPP << 24) | + (GDC_CMD_BLT_DRAW << 16) | (2 + step * height); + cmd[1] = (dy << 16) | dx; + cmd[2] = (height << 16) | width; + + while (i < height) { + memcpy(&cmd[3 + i * step], line, step); + line += bytes; + i++; + } +} + +static void mb86290fb_imageblit(struct fb_info *info, + const struct fb_image *image) +{ + int mdr; + u32 *cmd = NULL; + void (*cmdfn) (u32 *, u16, u16, u16, u16, u16, u32, u32, + const struct fb_image *, struct fb_info *) = NULL; + u32 cmdlen; + u32 fgcolor = 0, bgcolor = 0; + u16 step; + + u16 width = image->width, height = image->height; + u16 dx = image->dx, dy = image->dy; + int x2, y2, vxres, vyres; + + mdr = (GDC_ROP_COPY << 9); + x2 = image->dx + image->width; + y2 = image->dy + image->height; + vxres = info->var.xres_virtual; + vyres = info->var.yres_virtual; + x2 = min(x2, vxres); + y2 = min(y2, vyres); + width = x2 - dx; + height = y2 - dy; + + switch (image->depth) { + case 1: + step = (width + 31) >> 5; + cmdlen = 9 + height * step; + cmdfn = mb86290fb_imageblit1; + if (info->fix.visual == FB_VISUAL_TRUECOLOR || + info->fix.visual == FB_VISUAL_DIRECTCOLOR) { + fgcolor = + ((u32 *) (info->pseudo_palette))[image->fg_color]; + bgcolor = + ((u32 *) (info->pseudo_palette))[image->bg_color]; + } else { + fgcolor = image->fg_color; + bgcolor = image->bg_color; + } + + break; + + case 8: + step = (width + 1) >> 1; + cmdlen = 3 + height * step; + cmdfn = mb86290fb_imageblit8; + break; + + case 16: + step = (width + 1) >> 1; + cmdlen = 3 + height * step; + cmdfn = mb86290fb_imageblit16; + break; + + default: + cfb_imageblit(info, image); + return; + } + + cmd = kmalloc(cmdlen * 4, GFP_DMA); + if (!cmd) + return cfb_imageblit(info, image); + cmdfn(cmd, step, dx, dy, width, height, fgcolor, bgcolor, image, info); + mb862xxfb_write_fifo(cmdlen, cmd, info); + kfree(cmd); +} + +static void mb86290fb_fillrect(struct fb_info *info, + const struct fb_fillrect *rect) +{ + + u32 x2, y2, vxres, vyres, height, width, fg; + u32 cmd[7]; + + vxres = info->var.xres_virtual; + vyres = info->var.yres_virtual; + + if (!rect->width || !rect->height || rect->dx > vxres + || rect->dy > vyres) + return; + + /* We could use hardware clipping but on many cards you get around + * hardware clipping by writing to framebuffer directly. */ + x2 = rect->dx + rect->width; + y2 = rect->dy + rect->height; + x2 = min(x2, vxres); + y2 = min(y2, vyres); + width = x2 - rect->dx; + height = y2 - rect->dy; + if (info->fix.visual == FB_VISUAL_TRUECOLOR || + info->fix.visual == FB_VISUAL_DIRECTCOLOR) + fg = ((u32 *) (info->pseudo_palette))[rect->color]; + else + fg = rect->color; + + switch (rect->rop) { + + case ROP_XOR: + /* Set raster operation */ + cmd[1] = (2 << 7) | (GDC_ROP_XOR << 9); + break; + + case ROP_COPY: + /* Set raster operation */ + cmd[1] = (2 << 7) | (GDC_ROP_COPY << 9); + break; + + } + + cmd[0] = (GDC_TYPE_SETREGISTER << 24) | (1 << 16) | GDC_REG_MODE_BITMAP; + /* cmd[1] set earlier */ + cmd[2] = + (GDC_TYPE_SETCOLORREGISTER << 24) | (GDC_CMD_BODY_FORE_COLOR << 16); + cmd[3] = fg; + cmd[4] = (GDC_TYPE_DRAWRECTP << 24) | (GDC_CMD_BLT_FILL << 16); + cmd[5] = (rect->dy << 16) | (rect->dx); + cmd[6] = (height << 16) | width; + + mb862xxfb_write_fifo(7, cmd, info); +} + +void mb862xxfb_init_accel(struct fb_info *info, int xres) +{ + struct mb862xxfb_par *par = info->par; + + if (info->var.bits_per_pixel == 32) { + info->fbops->fb_fillrect = cfb_fillrect; + info->fbops->fb_copyarea = cfb_copyarea; + info->fbops->fb_imageblit = cfb_imageblit; + } else { + outreg(disp, GC_L0EM, 3); + info->fbops->fb_fillrect = mb86290fb_fillrect; + info->fbops->fb_copyarea = mb86290fb_copyarea; + info->fbops->fb_imageblit = mb86290fb_imageblit; + } + outreg(draw, GDC_REG_DRAW_BASE, 0); + outreg(draw, GDC_REG_MODE_MISC, 0x8000); + outreg(draw, GDC_REG_X_RESOLUTION, xres); + + info->flags |= + FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT | + FBINFO_HWACCEL_IMAGEBLIT; + info->fix.accel = 0xff; /*FIXME: add right define */ +} +EXPORT_SYMBOL(mb862xxfb_init_accel); diff --git a/drivers/video/mb862xx/mb862xxfb_accel.h b/drivers/video/mb862xx/mb862xxfb_accel.h new file mode 100644 index 0000000..96a2dfe --- /dev/null +++ b/drivers/video/mb862xx/mb862xxfb_accel.h @@ -0,0 +1,203 @@ +#ifndef __MB826XXFB_ACCEL_H__ +#define __MB826XXFB_ACCEL_H__ + +/* registers */ +#define GDC_GEO_REG_INPUT_FIFO 0x00000400L + +/* Special Registers */ +#define GDC_REG_CTRL 0x00000400L +#define GDC_REG_FIFO_STATUS 0x00000404L +#define GDC_REG_FIFO_COUNT 0x00000408L +#define GDC_REG_SETUP_STATUS 0x0000040CL +#define GDC_REG_DDA_STATUS 0x00000410L +#define GDC_REG_ENGINE_STATUS 0x00000414L +#define GDC_REG_ERROR_STATUS 0x00000418L +#define GDC_REG_MODE_MISC 0x00000420L /* MDR0 */ +#define GDC_REG_MODE_LINE 0x00000424L /* MDR1 */ +#define GDC_REG_MODE_POLYGON 0x00000428L /* MDR2 */ +#define GDC_REG_MODE_TEXTURE 0x0000042CL /* MDR3 */ +#define GDC_REG_MODE_BITMAP 0x00000430L /* MDR4 */ +#define GDC_REG_MODE_EXTENSION 0x0000043CL /* MDR7 */ + +/* Configuration Registers */ +#define GDC_REG_DRAW_BASE 0x00000440L +#define GDC_REG_X_RESOLUTION 0x00000444L +#define GDC_REG_Z_BASE 0x00000448L +#define GDC_REG_TEXTURE_BASE 0x0000044CL +#define GDC_REG_POLYGON_FLAG_BASE 0x00000450L +#define GDC_REG_CLIP_XMIN 0x00000454L +#define GDC_REG_CLIP_XMAX 0x00000458L +#define GDC_REG_CLIP_YMIN 0x0000045CL +#define GDC_REG_CLIP_YMAX 0x00000460L +#define GDC_REG_TEXURE_SIZE 0x00000464L +#define GDC_REG_TILE_SIZE 0x00000468L +#define GDC_REG_TEX_BUF_OFFSET 0x0000046CL + +/* for MB86293 or later */ +#define GDC_REG_ALPHA_MAP_BASE 0x00000474L /* ABR */ + +/* Constant Registers */ +#define GDC_REG_FOREGROUND_COLOR 0x00000480L +#define GDC_REG_BACKGROUND_COLOR 0x00000484L +#define GDC_REG_ALPHA 0x00000488L +#define GDC_REG_LINE_PATTERN 0x0000048CL +#define GDC_REG_TEX_BORDER_COLOR 0x00000494L +#define GDC_REG_LINE_PATTERN_OFFSET 0x000003E0L + +/* Coomand Code */ +#define GDC_CMD_PIXEL 0x00000000L +#define GDC_CMD_PIXEL_Z 0x00000001L + +#define GDC_CMD_X_VECTOR 0x00000020L +#define GDC_CMD_Y_VECTOR 0x00000021L +#define GDC_CMD_X_VECTOR_NOEND 0x00000022L +#define GDC_CMD_Y_VECTOR_NOEND 0x00000023L +#define GDC_CMD_X_VECTOR_BLPO 0x00000024L +#define GDC_CMD_Y_VECTOR_BLPO 0x00000025L +#define GDC_CMD_X_VECTOR_NOEND_BLPO 0x00000026L +#define GDC_CMD_Y_VECTOR_NOEND_BLPO 0x00000027L +#define GDC_CMD_AA_X_VECTOR 0x00000028L +#define GDC_CMD_AA_Y_VECTOR 0x00000029L +#define GDC_CMD_AA_X_VECTOR_NOEND 0x0000002AL +#define GDC_CMD_AA_Y_VECTOR_NOEND 0x0000002BL +#define GDC_CMD_AA_X_VECTOR_BLPO 0x0000002CL +#define GDC_CMD_AA_Y_VECTOR_BLPO 0x0000002DL +#define GDC_CMD_AA_X_VECTOR_NOEND_BLPO 0x0000002EL +#define GDC_CMD_AA_Y_VECTOR_NOEND_BLPO 0x0000002FL + +#define GDC_CMD_0_VECTOR 0x00000030L +#define GDC_CMD_1_VECTOR 0x00000031L +#define GDC_CMD_0_VECTOR_NOEND 0x00000032L +#define GDC_CMD_1_VECTOR_NOEND 0x00000033L +#define GDC_CMD_0_VECTOR_BLPO 0x00000034L +#define GDC_CMD_1_VECTOR_BLPO 0x00000035L +#define GDC_CMD_0_VECTOR_NOEND_BLPO 0x00000036L +#define GDC_CMD_1_VECTOR_NOEND_BLPO 0x00000037L +#define GDC_CMD_AA_0_VECTOR 0x00000038L +#define GDC_CMD_AA_1_VECTOR 0x00000039L +#define GDC_CMD_AA_0_VECTOR_NOEND 0x0000003AL +#define GDC_CMD_AA_1_VECTOR_NOEND 0x0000003BL +#define GDC_CMD_AA_0_VECTOR_BLPO 0x0000003CL +#define GDC_CMD_AA_1_VECTOR_BLPO 0x0000003DL +#define GDC_CMD_AA_0_VECTOR_NOEND_BLPO 0x0000003EL +#define GDC_CMD_AA_1_VECTOR_NOEND_BLPO 0x0000003FL + +#define GDC_CMD_BLT_FILL 0x00000041L +#define GDC_CMD_BLT_DRAW 0x00000042L +#define GDC_CMD_BITMAP 0x00000043L +#define GDC_CMD_BLTCOPY_TOP_LEFT 0x00000044L +#define GDC_CMD_BLTCOPY_TOP_RIGHT 0x00000045L +#define GDC_CMD_BLTCOPY_BOTTOM_LEFT 0x00000046L +#define GDC_CMD_BLTCOPY_BOTTOM_RIGHT 0x00000047L +#define GDC_CMD_LOAD_TEXTURE 0x00000048L +#define GDC_CMD_LOAD_TILE 0x00000049L + +#define GDC_CMD_TRAP_RIGHT 0x00000060L +#define GDC_CMD_TRAP_LEFT 0x00000061L +#define GDC_CMD_TRIANGLE_FAN 0x00000062L +#define GDC_CMD_FLAG_TRIANGLE_FAN 0x00000063L + +#define GDC_CMD_FLUSH_FB 0x000000C1L +#define GDC_CMD_FLUSH_Z 0x000000C2L + +#define GDC_CMD_POLYGON_BEGIN 0x000000E0L +#define GDC_CMD_POLYGON_END 0x000000E1L +#define GDC_CMD_CLEAR_POLY_FLAG 0x000000E2L +#define GDC_CMD_NORMAL 0x000000FFL + +#define GDC_CMD_VECTOR_BLPO_FLAG 0x00040000L +#define GDC_CMD_FAST_VECTOR_BLPO_FLAG 0x00000004L + +/* for MB86293 or later */ +#define GDC_CMD_MDR1 0x00000000L +#define GDC_CMD_MDR1S 0x00000002L +#define GDC_CMD_MDR1B 0x00000004L +#define GDC_CMD_MDR2 0x00000001L +#define GDC_CMD_MDR2S 0x00000003L +#define GDC_CMD_MDR2TL 0x00000007L +#define GDC_CMD_GMDR1E 0x00000010L +#define GDC_CMD_GMDR2E 0x00000020L +#define GDC_CMD_OVERLAP_SHADOW_XY 0x00000000L +#define GDC_CMD_OVERLAP_SHADOW_XY_COMPOSITION 0x00000001L +#define GDC_CMD_OVERLAP_Z_PACKED_ONBS 0x00000007L +#define GDC_CMD_OVERLAP_Z_ORIGIN 0x00000000L +#define GDC_CMD_OVERLAP_Z_NON_TOPLEFT 0x00000001L +#define GDC_CMD_OVERLAP_Z_BORDER 0x00000002L +#define GDC_CMD_OVERLAP_Z_SHADOW 0x00000003L +#define GDC_CMD_BLTCOPY_ALT_ALPHA 0x00000000L /* Reserverd */ +#define GDC_CMD_DC_LOGOUT 0x00000000L /* Reserverd */ +#define GDC_CMD_BODY_FORE_COLOR 0x00000000L +#define GDC_CMD_BODY_BACK_COLOR 0x00000001L +#define GDC_CMD_SHADOW_FORE_COLOR 0x00000002L +#define GDC_CMD_SHADOW_BACK_COLOR 0x00000003L +#define GDC_CMD_BORDER_FORE_COLOR 0x00000004L +#define GDC_CMD_BORDER_BACK_COLOR 0x00000005L + +/* Type Code Table */ +#define GDC_TYPE_G_NOP 0x00000020L +#define GDC_TYPE_G_BEGIN 0x00000021L +#define GDC_TYPE_G_BEGINCONT 0x00000022L +#define GDC_TYPE_G_END 0x00000023L +#define GDC_TYPE_G_VERTEX 0x00000030L +#define GDC_TYPE_G_VERTEXLOG 0x00000032L +#define GDC_TYPE_G_VERTEXNOPLOG 0x00000033L +#define GDC_TYPE_G_INIT 0x00000040L +#define GDC_TYPE_G_VIEWPORT 0x00000041L +#define GDC_TYPE_G_DEPTHRANGE 0x00000042L +#define GDC_TYPE_G_LOADMATRIX 0x00000043L +#define GDC_TYPE_G_VIEWVOLUMEXYCLIP 0x00000044L +#define GDC_TYPE_G_VIEWVOLUMEZCLIP 0x00000045L +#define GDC_TYPE_G_VIEWVOLUMEWCLIP 0x00000046L +#define GDC_TYPE_SETLVERTEX2I 0x00000072L +#define GDC_TYPE_SETLVERTEX2IP 0x00000073L +#define GDC_TYPE_SETMODEREGISTER 0x000000C0L +#define GDC_TYPE_SETGMODEREGISTER 0x000000C1L +#define GDC_TYPE_OVERLAPXYOFFT 0x000000C8L +#define GDC_TYPE_OVERLAPZOFFT 0x000000C9L +#define GDC_TYPE_DC_LOGOUTADDR 0x000000CCL +#define GDC_TYPE_SETCOLORREGISTER 0x000000CEL +#define GDC_TYPE_G_BEGINE 0x000000E1L +#define GDC_TYPE_G_BEGINCONTE 0x000000E2L +#define GDC_TYPE_G_ENDE 0x000000E3L +#define GDC_TYPE_DRAWPIXEL 0x00000000L +#define GDC_TYPE_DRAWPIXELZ 0x00000001L +#define GDC_TYPE_DRAWLINE 0x00000002L +#define GDC_TYPE_DRAWLINE2I 0x00000003L +#define GDC_TYPE_DRAWLINE2IP 0x00000004L +#define GDC_TYPE_DRAWTRAP 0x00000005L +#define GDC_TYPE_DRAWVERTEX2I 0x00000006L +#define GDC_TYPE_DRAWVERTEX2IP 0x00000007L +#define GDC_TYPE_DRAWRECTP 0x00000009L +#define GDC_TYPE_DRAWBITMAPP 0x0000000BL +#define GDC_TYPE_BLTCOPYP 0x0000000DL +#define GDC_TYPE_BLTCOPYALTERNATEP 0x0000000FL +#define GDC_TYPE_LOADTEXTUREP 0x00000011L +#define GDC_TYPE_BLTTEXTUREP 0x00000013L +#define GDC_TYPE_BLTCOPYALTALPHABLENDP 0x0000001FL +#define GDC_TYPE_SETVERTEX2I 0x00000070L +#define GDC_TYPE_SETVERTEX2IP 0x00000071L +#define GDC_TYPE_DRAW 0x000000F0L +#define GDC_TYPE_SETREGISTER 0x000000F1L +#define GDC_TYPE_SYNC 0x000000FCL +#define GDC_TYPE_INTERRUPT 0x000000FDL +#define GDC_TYPE_NOP 0x0 + +/* Raster operation */ +#define GDC_ROP_CLEAR 0x0000 +#define GDC_ROP_AND 0x0001 +#define GDC_ROP_AND_REVERSE 0x0002 +#define GDC_ROP_COPY 0x0003 +#define GDC_ROP_AND_INVERTED 0x0004 +#define GDC_ROP_NOP 0x0005 +#define GDC_ROP_XOR 0x0006 +#define GDC_ROP_OR 0x0007 +#define GDC_ROP_NOR 0x0008 +#define GDC_ROP_EQUIV 0x0009 +#define GDC_ROP_INVERT 0x000A +#define GDC_ROP_OR_REVERSE 0x000B +#define GDC_ROP_COPY_INVERTED 0x000C +#define GDC_ROP_OR_INVERTED 0x000D +#define GDC_ROP_NAND 0x000E +#define GDC_ROP_SET 0x000F + +#endif -- cgit v0.10.2 From 8097b1749f9265be0f3dbc43c3700da31eb422fd Mon Sep 17 00:00:00 2001 From: Chaithrika U S Date: Tue, 15 Dec 2009 16:46:29 -0800 Subject: davinci: fb: calculate the clock divider from pixel clock info The clock divider value can be calculated from the pixel clock value for the panel. This gives more flexiblity to the driver to change the divider value on the fly as in the case of cpufreq feature- support for which will be added shortly. Signed-off-by: Chaithrika U S Cc: Sudhakar Rajashekhara Cc: Steve Chen Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/da8xx-fb.c b/drivers/video/da8xx-fb.c index ea1fd3f..fe00252 100644 --- a/drivers/video/da8xx-fb.c +++ b/drivers/video/da8xx-fb.c @@ -113,6 +113,7 @@ struct da8xx_fb_par { unsigned short pseudo_palette[16]; unsigned int databuf_sz; unsigned int palette_sz; + unsigned int pxl_clk; }; /* Variable Screen Information */ @@ -155,7 +156,7 @@ struct da8xx_panel { int vfp; /* Vertical front porch */ int vbp; /* Vertical back porch */ int vsw; /* Vertical Sync Pulse Width */ - int pxl_clk; /* Pixel clock */ + unsigned int pxl_clk; /* Pixel clock */ unsigned char invert_pxl_clk; /* Invert Pixel clock */ }; @@ -171,7 +172,7 @@ static struct da8xx_panel known_lcd_panels[] = { .vfp = 2, .vbp = 2, .vsw = 0, - .pxl_clk = 0x10, + .pxl_clk = 4608000, .invert_pxl_clk = 1, }, /* Sharp LK043T1DG01 */ @@ -185,7 +186,7 @@ static struct da8xx_panel known_lcd_panels[] = { .vfp = 2, .vbp = 2, .vsw = 10, - .pxl_clk = 0x12, + .pxl_clk = 7833600, .invert_pxl_clk = 0, }, }; @@ -451,6 +452,18 @@ static void lcd_reset(struct da8xx_fb_par *par) lcdc_write(0, LCD_RASTER_CTRL_REG); } +static void lcd_calc_clk_divider(struct da8xx_fb_par *par) +{ + unsigned int lcd_clk, div; + + lcd_clk = clk_get_rate(par->lcdc_clk); + div = lcd_clk / par->pxl_clk; + + /* Configure the LCD clock divisor. */ + lcdc_write(LCD_CLK_DIVISOR(div) | + (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG); +} + static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg, struct da8xx_panel *panel) { @@ -459,9 +472,8 @@ static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg, lcd_reset(par); - /* Configure the LCD clock divisor. */ - lcdc_write(LCD_CLK_DIVISOR(panel->pxl_clk) | - (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG); + /* Calculate the divider */ + lcd_calc_clk_divider(par); if (panel->invert_pxl_clk) lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) | @@ -721,6 +733,8 @@ static int __init fb_probe(struct platform_device *device) } par = da8xx_fb_info->par; + par->lcdc_clk = fb_clk; + par->pxl_clk = lcdc_info->pxl_clk; if (lcd_init(par, lcd_cfg, lcdc_info) < 0) { dev_err(&device->dev, "lcd_init failed\n"); @@ -754,8 +768,6 @@ static int __init fb_probe(struct platform_device *device) da8xx_fb_fix.smem_len = par->databuf_sz - par->palette_sz; da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8; - par->lcdc_clk = fb_clk; - par->irq = platform_get_irq(device, 0); if (par->irq < 0) { ret = -ENOENT; -- cgit v0.10.2 From e04e54835fdaaeebbd95f9508bc814859fcd7afd Mon Sep 17 00:00:00 2001 From: Chaithrika U S Date: Tue, 15 Dec 2009 16:46:29 -0800 Subject: davinci: fb: add cpufreq support Add cpufreq support for DA8xx/OMAP-L1xx frame buffer driver Signed-off-by: Chaithrika U S Cc: Krzysztof Helt Cc: Kevin Hilman Cc: Dave Jones Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds diff --git a/drivers/video/da8xx-fb.c b/drivers/video/da8xx-fb.c index fe00252..8966274 100644 --- a/drivers/video/da8xx-fb.c +++ b/drivers/video/da8xx-fb.c @@ -28,6 +28,7 @@ #include #include #include +#include #include