blob: 9d0ce0d8ebdf1a39f7b78e591eb53d8a1ec8cb5d [file] [log] [blame]
Matt Wagantall39f90cb2012-02-08 14:09:11 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
Stephen Boyd3f4da322011-08-30 01:03:23 -070015#include <linux/device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070016#include <linux/firmware.h>
17#include <linux/io.h>
18#include <linux/debugfs.h>
19#include <linux/elf.h>
20#include <linux/mutex.h>
21#include <linux/memblock.h>
Stephen Boyd3f4da322011-08-30 01:03:23 -070022#include <linux/slab.h>
Stephen Boyd6d67d252011-09-27 11:50:05 -070023#include <linux/atomic.h>
Stephen Boyd80bde032012-03-16 00:14:42 -070024#include <linux/suspend.h>
25#include <linux/rwsem.h>
Stephen Boyd20ad8102011-10-09 21:28:01 -070026#include <linux/sysfs.h>
Stephen Boyd36974ec2012-03-22 01:30:59 -070027#include <linux/workqueue.h>
28#include <linux/jiffies.h>
29#include <linux/wakelock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <asm/uaccess.h>
32#include <asm/setup.h>
Stephen Boyd6d67d252011-09-27 11:50:05 -070033#include <mach/peripheral-loader.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#include "peripheral-loader.h"
36
Stephen Boyd20ad8102011-10-09 21:28:01 -070037enum pil_state {
38 PIL_OFFLINE,
39 PIL_ONLINE,
40};
41
42static const char *pil_states[] = {
43 [PIL_OFFLINE] = "OFFLINE",
44 [PIL_ONLINE] = "ONLINE",
45};
46
Stephen Boyd3f4da322011-08-30 01:03:23 -070047struct pil_device {
48 struct pil_desc *desc;
49 int count;
Stephen Boyd20ad8102011-10-09 21:28:01 -070050 enum pil_state state;
Stephen Boyd3f4da322011-08-30 01:03:23 -070051 struct mutex lock;
Stephen Boyd6d67d252011-09-27 11:50:05 -070052 struct device dev;
53 struct module *owner;
54#ifdef CONFIG_DEBUG_FS
55 struct dentry *dentry;
56#endif
Stephen Boyd36974ec2012-03-22 01:30:59 -070057 struct delayed_work proxy;
58 struct wake_lock wlock;
59 char wake_name[32];
Stephen Boyd3f4da322011-08-30 01:03:23 -070060};
61
Stephen Boyd6d67d252011-09-27 11:50:05 -070062#define to_pil_device(d) container_of(d, struct pil_device, dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063
Stephen Boyd20ad8102011-10-09 21:28:01 -070064static ssize_t name_show(struct device *dev, struct device_attribute *attr,
65 char *buf)
66{
67 return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name);
68}
69
70static ssize_t state_show(struct device *dev, struct device_attribute *attr,
71 char *buf)
72{
73 enum pil_state state = to_pil_device(dev)->state;
74 return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]);
75}
76
77static struct device_attribute pil_attrs[] = {
78 __ATTR_RO(name),
79 __ATTR_RO(state),
80 { },
81};
82
Stephen Boyd6d67d252011-09-27 11:50:05 -070083struct bus_type pil_bus_type = {
84 .name = "pil",
Stephen Boyd20ad8102011-10-09 21:28:01 -070085 .dev_attrs = pil_attrs,
Stephen Boyd6d67d252011-09-27 11:50:05 -070086};
87
88static int __find_peripheral(struct device *dev, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089{
Stephen Boyd6d67d252011-09-27 11:50:05 -070090 struct pil_device *pdev = to_pil_device(dev);
91 return !strncmp(pdev->desc->name, data, INT_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092}
93
94static struct pil_device *find_peripheral(const char *str)
95{
Stephen Boyd6d67d252011-09-27 11:50:05 -070096 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097
98 if (!str)
99 return NULL;
100
Stephen Boyd6d67d252011-09-27 11:50:05 -0700101 dev = bus_find_device(&pil_bus_type, NULL, (void *)str,
102 __find_peripheral);
103 return dev ? to_pil_device(dev) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104}
105
Stephen Boyd36974ec2012-03-22 01:30:59 -0700106static void pil_proxy_work(struct work_struct *work)
107{
108 struct pil_device *pil;
109
110 pil = container_of(work, struct pil_device, proxy.work);
111 pil->desc->ops->proxy_unvote(pil->desc);
112 wake_unlock(&pil->wlock);
113}
114
115static int pil_proxy_vote(struct pil_device *pil)
116{
117 if (pil->desc->ops->proxy_vote) {
118 wake_lock(&pil->wlock);
119 return pil->desc->ops->proxy_vote(pil->desc);
120 }
121 return 0;
122}
123
124static void pil_proxy_unvote(struct pil_device *pil, unsigned long timeout)
125{
126 if (pil->desc->ops->proxy_unvote)
127 schedule_delayed_work(&pil->proxy, msecs_to_jiffies(timeout));
128}
129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130#define IOMAP_SIZE SZ_4M
131
132static int load_segment(const struct elf32_phdr *phdr, unsigned num,
133 struct pil_device *pil)
134{
Stephen Boydb0f1f802012-02-03 11:28:08 -0800135 int ret = 0, count, paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 char fw_name[30];
137 const struct firmware *fw = NULL;
138 const u8 *data;
139
Stephen Boydf79af532012-05-14 18:57:26 -0700140 if (memblock_overlaps_memory(phdr->p_paddr, phdr->p_memsz)) {
141 dev_err(&pil->dev,
142 "kernel memory would be overwritten [%#08lx, %#08lx)\n",
143 (unsigned long)phdr->p_paddr,
144 (unsigned long)(phdr->p_paddr + phdr->p_memsz));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 return -EPERM;
146 }
147
148 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700149 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
150 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700151 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700153 dev_err(&pil->dev, "Failed to locate blob %s\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154 fw_name);
155 return ret;
156 }
157
158 if (fw->size != phdr->p_filesz) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700159 dev_err(&pil->dev, "Blob size %u doesn't match %u\n",
160 fw->size, phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 ret = -EPERM;
162 goto release_fw;
163 }
164 }
165
166 /* Load the segment into memory */
167 count = phdr->p_filesz;
168 paddr = phdr->p_paddr;
169 data = fw ? fw->data : NULL;
170 while (count > 0) {
171 int size;
172 u8 __iomem *buf;
173
174 size = min_t(size_t, IOMAP_SIZE, count);
175 buf = ioremap(paddr, size);
176 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700177 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 ret = -ENOMEM;
179 goto release_fw;
180 }
181 memcpy(buf, data, size);
182 iounmap(buf);
183
184 count -= size;
185 paddr += size;
186 data += size;
187 }
188
189 /* Zero out trailing memory */
190 count = phdr->p_memsz - phdr->p_filesz;
191 while (count > 0) {
192 int size;
193 u8 __iomem *buf;
194
195 size = min_t(size_t, IOMAP_SIZE, count);
196 buf = ioremap(paddr, size);
197 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700198 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 ret = -ENOMEM;
200 goto release_fw;
201 }
202 memset(buf, 0, size);
203 iounmap(buf);
204
205 count -= size;
206 paddr += size;
207 }
208
Stephen Boydb0f1f802012-02-03 11:28:08 -0800209 if (pil->desc->ops->verify_blob) {
210 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700211 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800212 if (ret)
213 dev_err(&pil->dev, "Blob%u failed verification\n", num);
214 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215
216release_fw:
217 release_firmware(fw);
218 return ret;
219}
220
221#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
222
223static int segment_is_loadable(const struct elf32_phdr *p)
224{
225 return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags);
226}
227
Stephen Boyd80bde032012-03-16 00:14:42 -0700228/* Sychronize request_firmware() with suspend */
229static DECLARE_RWSEM(pil_pm_rwsem);
230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231static int load_image(struct pil_device *pil)
232{
233 int i, ret;
234 char fw_name[30];
235 struct elf32_hdr *ehdr;
236 const struct elf32_phdr *phdr;
237 const struct firmware *fw;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700238 unsigned long proxy_timeout = pil->desc->proxy_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
Stephen Boyd80bde032012-03-16 00:14:42 -0700240 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700241 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700242 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700244 dev_err(&pil->dev, "Failed to locate %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 goto out;
246 }
247
248 if (fw->size < sizeof(*ehdr)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700249 dev_err(&pil->dev, "Not big enough to be an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 ret = -EIO;
251 goto release_fw;
252 }
253
254 ehdr = (struct elf32_hdr *)fw->data;
255 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700256 dev_err(&pil->dev, "Not an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 ret = -EIO;
258 goto release_fw;
259 }
260
261 if (ehdr->e_phnum == 0) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700262 dev_err(&pil->dev, "No loadable segments\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 ret = -EIO;
264 goto release_fw;
265 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700266 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
267 sizeof(struct elf32_hdr) > fw->size) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700268 dev_err(&pil->dev, "Program headers not within mdt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 ret = -EIO;
270 goto release_fw;
271 }
272
Stephen Boyd3f4da322011-08-30 01:03:23 -0700273 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700275 dev_err(&pil->dev, "Invalid firmware metadata\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 goto release_fw;
277 }
278
Stephen Boydc9753e12011-07-13 17:58:48 -0700279 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
281 if (!segment_is_loadable(phdr))
282 continue;
283
284 ret = load_segment(phdr, i, pil);
285 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700286 dev_err(&pil->dev, "Failed to load segment %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 i);
288 goto release_fw;
289 }
290 }
291
Stephen Boyd36974ec2012-03-22 01:30:59 -0700292 ret = pil_proxy_vote(pil);
293 if (ret) {
294 dev_err(&pil->dev, "Failed to proxy vote\n");
295 goto release_fw;
296 }
297
Stephen Boyd3f4da322011-08-30 01:03:23 -0700298 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700300 dev_err(&pil->dev, "Failed to bring out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700301 proxy_timeout = 0; /* Remove proxy vote immediately on error */
302 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700304 dev_info(&pil->dev, "brought %s out of reset\n", pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700305err_boot:
306 pil_proxy_unvote(pil, proxy_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307release_fw:
308 release_firmware(fw);
309out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700310 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 return ret;
312}
313
Stephen Boyd20ad8102011-10-09 21:28:01 -0700314static void pil_set_state(struct pil_device *pil, enum pil_state state)
315{
316 if (pil->state != state) {
317 pil->state = state;
318 sysfs_notify(&pil->dev.kobj, NULL, "state");
319 }
320}
321
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322/**
323 * pil_get() - Load a peripheral into memory and take it out of reset
324 * @name: pointer to a string containing the name of the peripheral to load
325 *
326 * This function returns a pointer if it succeeds. If an error occurs an
327 * ERR_PTR is returned.
328 *
329 * If PIL is not enabled in the kernel, the value %NULL will be returned.
330 */
331void *pil_get(const char *name)
332{
333 int ret;
334 struct pil_device *pil;
335 struct pil_device *pil_d;
336 void *retval;
337
Stephen Boyd6d67d252011-09-27 11:50:05 -0700338 if (!name)
339 return NULL;
340
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 pil = retval = find_peripheral(name);
342 if (!pil)
343 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700344 if (!try_module_get(pil->owner)) {
345 put_device(&pil->dev);
346 return ERR_PTR(-ENODEV);
347 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348
Stephen Boyd6d67d252011-09-27 11:50:05 -0700349 pil_d = pil_get(pil->desc->depends_on);
350 if (IS_ERR(pil_d)) {
351 retval = pil_d;
352 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 }
354
355 mutex_lock(&pil->lock);
Stephen Boyde4861c02012-05-14 12:57:40 -0700356 if (!pil->count) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700357 ret = load_image(pil);
358 if (ret) {
359 retval = ERR_PTR(ret);
360 goto err_load;
361 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 }
Stephen Boyde4861c02012-05-14 12:57:40 -0700363 pil->count++;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700364 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700366out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700368err_load:
369 mutex_unlock(&pil->lock);
370 pil_put(pil_d);
371err_depends:
372 put_device(&pil->dev);
373 module_put(pil->owner);
374 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375}
376EXPORT_SYMBOL(pil_get);
377
Stephen Boyd36974ec2012-03-22 01:30:59 -0700378static void pil_shutdown(struct pil_device *pil)
379{
380 pil->desc->ops->shutdown(pil->desc);
381 flush_delayed_work(&pil->proxy);
382 pil_set_state(pil, PIL_OFFLINE);
383}
384
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385/**
386 * pil_put() - Inform PIL the peripheral no longer needs to be active
387 * @peripheral_handle: pointer from a previous call to pil_get()
388 *
389 * This doesn't imply that a peripheral is shutdown or in reset since another
390 * driver could be using the peripheral.
391 */
392void pil_put(void *peripheral_handle)
393{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700394 struct pil_device *pil_d, *pil = peripheral_handle;
395
396 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
399 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700400 if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
401 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700402 if (!--pil->count)
403 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 mutex_unlock(&pil->lock);
405
Stephen Boyd3f4da322011-08-30 01:03:23 -0700406 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700407 module_put(pil->owner);
408 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700410 put_device(&pil_d->dev);
411 }
412 put_device(&pil->dev);
413 return;
414err_out:
415 mutex_unlock(&pil->lock);
416 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417}
418EXPORT_SYMBOL(pil_put);
419
420void pil_force_shutdown(const char *name)
421{
422 struct pil_device *pil;
423
424 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700425 if (!pil) {
426 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700428 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429
430 mutex_lock(&pil->lock);
431 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700432 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700434
Stephen Boyd6d67d252011-09-27 11:50:05 -0700435 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436}
437EXPORT_SYMBOL(pil_force_shutdown);
438
439int pil_force_boot(const char *name)
440{
441 int ret = -EINVAL;
442 struct pil_device *pil;
443
444 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700445 if (!pil) {
446 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700448 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 mutex_lock(&pil->lock);
451 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
452 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700453 if (!ret)
454 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700456 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457
458 return ret;
459}
460EXPORT_SYMBOL(pil_force_boot);
461
462#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700463static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464{
465 filp->private_data = inode->i_private;
466 return 0;
467}
468
469static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
470 size_t cnt, loff_t *ppos)
471{
472 int r;
473 char buf[40];
474 struct pil_device *pil = filp->private_data;
475
476 mutex_lock(&pil->lock);
477 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
478 mutex_unlock(&pil->lock);
479 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
480}
481
482static ssize_t msm_pil_debugfs_write(struct file *filp,
483 const char __user *ubuf, size_t cnt, loff_t *ppos)
484{
485 struct pil_device *pil = filp->private_data;
486 char buf[4];
487
488 if (cnt > sizeof(buf))
489 return -EINVAL;
490
491 if (copy_from_user(&buf, ubuf, cnt))
492 return -EFAULT;
493
494 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700495 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 return -EIO;
497 } else if (!strncmp(buf, "put", 3))
498 pil_put(pil);
499 else
500 return -EINVAL;
501
502 return cnt;
503}
504
505static const struct file_operations msm_pil_debugfs_fops = {
506 .open = msm_pil_debugfs_open,
507 .read = msm_pil_debugfs_read,
508 .write = msm_pil_debugfs_write,
509};
510
511static struct dentry *pil_base_dir;
512
Stephen Boyd6d67d252011-09-27 11:50:05 -0700513static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514{
515 pil_base_dir = debugfs_create_dir("pil", NULL);
516 if (!pil_base_dir) {
517 pil_base_dir = NULL;
518 return -ENOMEM;
519 }
520
521 return 0;
522}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700523
524static void __exit msm_pil_debugfs_exit(void)
525{
526 debugfs_remove_recursive(pil_base_dir);
527}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528
529static int msm_pil_debugfs_add(struct pil_device *pil)
530{
531 if (!pil_base_dir)
532 return -ENOMEM;
533
Stephen Boyd6d67d252011-09-27 11:50:05 -0700534 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
535 pil_base_dir, pil, &msm_pil_debugfs_fops);
536 return !pil->dentry ? -ENOMEM : 0;
537}
538
539static void msm_pil_debugfs_remove(struct pil_device *pil)
540{
541 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542}
543#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700544static int __init msm_pil_debugfs_init(void) { return 0; };
545static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700547static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548#endif
549
Stephen Boyd6d67d252011-09-27 11:50:05 -0700550static int __msm_pil_shutdown(struct device *dev, void *data)
551{
Stephen Boyd36974ec2012-03-22 01:30:59 -0700552 pil_shutdown(to_pil_device(dev));
Stephen Boyd6d67d252011-09-27 11:50:05 -0700553 return 0;
554}
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556static int msm_pil_shutdown_at_boot(void)
557{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700558 return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559}
560late_initcall(msm_pil_shutdown_at_boot);
561
Stephen Boyd6d67d252011-09-27 11:50:05 -0700562static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700564 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700565 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700566 mutex_destroy(&pil->lock);
567 kfree(pil);
568}
569
570struct pil_device *msm_pil_register(struct pil_desc *desc)
571{
572 int err;
573 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700574 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700575
Stephen Boyd36974ec2012-03-22 01:30:59 -0700576 /* Ignore users who don't make any sense */
577 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
578 "invalid proxy voting. ignoring\n"))
579 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
580
Matt Wagantall2475e312012-05-25 19:56:33 -0700581 WARN(desc->ops->proxy_unvote && !desc->proxy_timeout,
582 "A proxy timeout of 0 ms was specified for %s. Specify one in "
583 "desc->proxy_timeout.\n", desc->name);
584
Stephen Boyd36974ec2012-03-22 01:30:59 -0700585 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700586 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700587 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
589 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700590 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700591 pil->owner = desc->owner;
592 pil->dev.parent = desc->dev;
593 pil->dev.bus = &pil_bus_type;
594 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
Stephen Boyd36974ec2012-03-22 01:30:59 -0700596 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
597 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
598 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
599
Stephen Boyd6d67d252011-09-27 11:50:05 -0700600 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
601 err = device_register(&pil->dev);
602 if (err) {
603 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700604 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700605 mutex_destroy(&pil->lock);
606 kfree(pil);
607 return ERR_PTR(err);
608 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Stephen Boyd6d67d252011-09-27 11:50:05 -0700610 err = msm_pil_debugfs_add(pil);
611 if (err) {
612 device_unregister(&pil->dev);
613 return ERR_PTR(err);
614 }
615
616 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700618EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
Stephen Boyd6d67d252011-09-27 11:50:05 -0700620void msm_pil_unregister(struct pil_device *pil)
621{
622 if (IS_ERR_OR_NULL(pil))
623 return;
624
625 if (get_device(&pil->dev)) {
626 mutex_lock(&pil->lock);
627 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700628 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700629 msm_pil_debugfs_remove(pil);
630 device_unregister(&pil->dev);
631 mutex_unlock(&pil->lock);
632 put_device(&pil->dev);
633 }
634}
635EXPORT_SYMBOL(msm_pil_unregister);
636
Stephen Boyd80bde032012-03-16 00:14:42 -0700637static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
638{
639 switch (event) {
640 case PM_SUSPEND_PREPARE:
641 down_write(&pil_pm_rwsem);
642 break;
643 case PM_POST_SUSPEND:
644 up_write(&pil_pm_rwsem);
645 break;
646 }
647 return NOTIFY_DONE;
648}
649
650static struct notifier_block pil_pm_notifier = {
651 .notifier_call = pil_pm_notify,
652};
653
Stephen Boyd6d67d252011-09-27 11:50:05 -0700654static int __init msm_pil_init(void)
655{
656 int ret = msm_pil_debugfs_init();
657 if (ret)
658 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700659 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700660 return bus_register(&pil_bus_type);
661}
662subsys_initcall(msm_pil_init);
663
664static void __exit msm_pil_exit(void)
665{
666 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700667 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700668 msm_pil_debugfs_exit();
669}
670module_exit(msm_pil_exit);
671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672MODULE_LICENSE("GPL v2");
673MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");