blob: fa9159e690b7da52260d6a811e4cb307dfb5831f [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
140 if (memblock_is_region_memory(phdr->p_paddr, phdr->p_memsz)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700141 dev_err(&pil->dev, "Kernel memory would be overwritten");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 return -EPERM;
143 }
144
145 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700146 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
147 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700148 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700150 dev_err(&pil->dev, "Failed to locate blob %s\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 fw_name);
152 return ret;
153 }
154
155 if (fw->size != phdr->p_filesz) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700156 dev_err(&pil->dev, "Blob size %u doesn't match %u\n",
157 fw->size, phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 ret = -EPERM;
159 goto release_fw;
160 }
161 }
162
163 /* Load the segment into memory */
164 count = phdr->p_filesz;
165 paddr = phdr->p_paddr;
166 data = fw ? fw->data : NULL;
167 while (count > 0) {
168 int size;
169 u8 __iomem *buf;
170
171 size = min_t(size_t, IOMAP_SIZE, count);
172 buf = ioremap(paddr, size);
173 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700174 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175 ret = -ENOMEM;
176 goto release_fw;
177 }
178 memcpy(buf, data, size);
179 iounmap(buf);
180
181 count -= size;
182 paddr += size;
183 data += size;
184 }
185
186 /* Zero out trailing memory */
187 count = phdr->p_memsz - phdr->p_filesz;
188 while (count > 0) {
189 int size;
190 u8 __iomem *buf;
191
192 size = min_t(size_t, IOMAP_SIZE, count);
193 buf = ioremap(paddr, size);
194 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700195 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 ret = -ENOMEM;
197 goto release_fw;
198 }
199 memset(buf, 0, size);
200 iounmap(buf);
201
202 count -= size;
203 paddr += size;
204 }
205
Stephen Boydb0f1f802012-02-03 11:28:08 -0800206 if (pil->desc->ops->verify_blob) {
207 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700208 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800209 if (ret)
210 dev_err(&pil->dev, "Blob%u failed verification\n", num);
211 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213release_fw:
214 release_firmware(fw);
215 return ret;
216}
217
218#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
219
220static int segment_is_loadable(const struct elf32_phdr *p)
221{
222 return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags);
223}
224
Stephen Boyd80bde032012-03-16 00:14:42 -0700225/* Sychronize request_firmware() with suspend */
226static DECLARE_RWSEM(pil_pm_rwsem);
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228static int load_image(struct pil_device *pil)
229{
230 int i, ret;
231 char fw_name[30];
232 struct elf32_hdr *ehdr;
233 const struct elf32_phdr *phdr;
234 const struct firmware *fw;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700235 unsigned long proxy_timeout = pil->desc->proxy_timeout;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236
Stephen Boyd80bde032012-03-16 00:14:42 -0700237 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700238 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700239 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700241 dev_err(&pil->dev, "Failed to locate %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 goto out;
243 }
244
245 if (fw->size < sizeof(*ehdr)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700246 dev_err(&pil->dev, "Not big enough to be an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 ret = -EIO;
248 goto release_fw;
249 }
250
251 ehdr = (struct elf32_hdr *)fw->data;
252 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700253 dev_err(&pil->dev, "Not an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 ret = -EIO;
255 goto release_fw;
256 }
257
258 if (ehdr->e_phnum == 0) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700259 dev_err(&pil->dev, "No loadable segments\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 ret = -EIO;
261 goto release_fw;
262 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700263 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
264 sizeof(struct elf32_hdr) > fw->size) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700265 dev_err(&pil->dev, "Program headers not within mdt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 ret = -EIO;
267 goto release_fw;
268 }
269
Stephen Boyd3f4da322011-08-30 01:03:23 -0700270 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700272 dev_err(&pil->dev, "Invalid firmware metadata\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 goto release_fw;
274 }
275
Stephen Boydc9753e12011-07-13 17:58:48 -0700276 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
278 if (!segment_is_loadable(phdr))
279 continue;
280
281 ret = load_segment(phdr, i, pil);
282 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700283 dev_err(&pil->dev, "Failed to load segment %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 i);
285 goto release_fw;
286 }
287 }
288
Stephen Boyd36974ec2012-03-22 01:30:59 -0700289 ret = pil_proxy_vote(pil);
290 if (ret) {
291 dev_err(&pil->dev, "Failed to proxy vote\n");
292 goto release_fw;
293 }
294
Stephen Boyd3f4da322011-08-30 01:03:23 -0700295 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700297 dev_err(&pil->dev, "Failed to bring out of reset\n");
Stephen Boyd36974ec2012-03-22 01:30:59 -0700298 proxy_timeout = 0; /* Remove proxy vote immediately on error */
299 goto err_boot;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700301 dev_info(&pil->dev, "brought %s out of reset\n", pil->desc->name);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700302err_boot:
303 pil_proxy_unvote(pil, proxy_timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304release_fw:
305 release_firmware(fw);
306out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700307 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 return ret;
309}
310
Stephen Boyd20ad8102011-10-09 21:28:01 -0700311static void pil_set_state(struct pil_device *pil, enum pil_state state)
312{
313 if (pil->state != state) {
314 pil->state = state;
315 sysfs_notify(&pil->dev.kobj, NULL, "state");
316 }
317}
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319/**
320 * pil_get() - Load a peripheral into memory and take it out of reset
321 * @name: pointer to a string containing the name of the peripheral to load
322 *
323 * This function returns a pointer if it succeeds. If an error occurs an
324 * ERR_PTR is returned.
325 *
326 * If PIL is not enabled in the kernel, the value %NULL will be returned.
327 */
328void *pil_get(const char *name)
329{
330 int ret;
331 struct pil_device *pil;
332 struct pil_device *pil_d;
333 void *retval;
334
Stephen Boyd6d67d252011-09-27 11:50:05 -0700335 if (!name)
336 return NULL;
337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 pil = retval = find_peripheral(name);
339 if (!pil)
340 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700341 if (!try_module_get(pil->owner)) {
342 put_device(&pil->dev);
343 return ERR_PTR(-ENODEV);
344 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345
Stephen Boyd6d67d252011-09-27 11:50:05 -0700346 pil_d = pil_get(pil->desc->depends_on);
347 if (IS_ERR(pil_d)) {
348 retval = pil_d;
349 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 }
351
352 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700353 if (!pil->count++) {
354 ret = load_image(pil);
355 if (ret) {
356 retval = ERR_PTR(ret);
357 goto err_load;
358 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359 }
Stephen Boyd20ad8102011-10-09 21:28:01 -0700360 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700362out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700364err_load:
365 mutex_unlock(&pil->lock);
366 pil_put(pil_d);
367err_depends:
368 put_device(&pil->dev);
369 module_put(pil->owner);
370 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371}
372EXPORT_SYMBOL(pil_get);
373
Stephen Boyd36974ec2012-03-22 01:30:59 -0700374static void pil_shutdown(struct pil_device *pil)
375{
376 pil->desc->ops->shutdown(pil->desc);
377 flush_delayed_work(&pil->proxy);
378 pil_set_state(pil, PIL_OFFLINE);
379}
380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381/**
382 * pil_put() - Inform PIL the peripheral no longer needs to be active
383 * @peripheral_handle: pointer from a previous call to pil_get()
384 *
385 * This doesn't imply that a peripheral is shutdown or in reset since another
386 * driver could be using the peripheral.
387 */
388void pil_put(void *peripheral_handle)
389{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700390 struct pil_device *pil_d, *pil = peripheral_handle;
391
392 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
395 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700396 if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
397 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700398 if (!--pil->count)
399 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 mutex_unlock(&pil->lock);
401
Stephen Boyd3f4da322011-08-30 01:03:23 -0700402 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700403 module_put(pil->owner);
404 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700406 put_device(&pil_d->dev);
407 }
408 put_device(&pil->dev);
409 return;
410err_out:
411 mutex_unlock(&pil->lock);
412 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413}
414EXPORT_SYMBOL(pil_put);
415
416void pil_force_shutdown(const char *name)
417{
418 struct pil_device *pil;
419
420 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700421 if (!pil) {
422 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700424 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425
426 mutex_lock(&pil->lock);
427 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700428 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700430
Stephen Boyd6d67d252011-09-27 11:50:05 -0700431 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432}
433EXPORT_SYMBOL(pil_force_shutdown);
434
435int pil_force_boot(const char *name)
436{
437 int ret = -EINVAL;
438 struct pil_device *pil;
439
440 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700441 if (!pil) {
442 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700444 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
446 mutex_lock(&pil->lock);
447 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
448 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700449 if (!ret)
450 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700452 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 return ret;
455}
456EXPORT_SYMBOL(pil_force_boot);
457
458#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700459static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460{
461 filp->private_data = inode->i_private;
462 return 0;
463}
464
465static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
466 size_t cnt, loff_t *ppos)
467{
468 int r;
469 char buf[40];
470 struct pil_device *pil = filp->private_data;
471
472 mutex_lock(&pil->lock);
473 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
474 mutex_unlock(&pil->lock);
475 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
476}
477
478static ssize_t msm_pil_debugfs_write(struct file *filp,
479 const char __user *ubuf, size_t cnt, loff_t *ppos)
480{
481 struct pil_device *pil = filp->private_data;
482 char buf[4];
483
484 if (cnt > sizeof(buf))
485 return -EINVAL;
486
487 if (copy_from_user(&buf, ubuf, cnt))
488 return -EFAULT;
489
490 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700491 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 return -EIO;
493 } else if (!strncmp(buf, "put", 3))
494 pil_put(pil);
495 else
496 return -EINVAL;
497
498 return cnt;
499}
500
501static const struct file_operations msm_pil_debugfs_fops = {
502 .open = msm_pil_debugfs_open,
503 .read = msm_pil_debugfs_read,
504 .write = msm_pil_debugfs_write,
505};
506
507static struct dentry *pil_base_dir;
508
Stephen Boyd6d67d252011-09-27 11:50:05 -0700509static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510{
511 pil_base_dir = debugfs_create_dir("pil", NULL);
512 if (!pil_base_dir) {
513 pil_base_dir = NULL;
514 return -ENOMEM;
515 }
516
517 return 0;
518}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700519
520static void __exit msm_pil_debugfs_exit(void)
521{
522 debugfs_remove_recursive(pil_base_dir);
523}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525static int msm_pil_debugfs_add(struct pil_device *pil)
526{
527 if (!pil_base_dir)
528 return -ENOMEM;
529
Stephen Boyd6d67d252011-09-27 11:50:05 -0700530 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
531 pil_base_dir, pil, &msm_pil_debugfs_fops);
532 return !pil->dentry ? -ENOMEM : 0;
533}
534
535static void msm_pil_debugfs_remove(struct pil_device *pil)
536{
537 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538}
539#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700540static int __init msm_pil_debugfs_init(void) { return 0; };
541static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700543static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544#endif
545
Stephen Boyd6d67d252011-09-27 11:50:05 -0700546static int __msm_pil_shutdown(struct device *dev, void *data)
547{
Stephen Boyd36974ec2012-03-22 01:30:59 -0700548 pil_shutdown(to_pil_device(dev));
Stephen Boyd6d67d252011-09-27 11:50:05 -0700549 return 0;
550}
551
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552static int msm_pil_shutdown_at_boot(void)
553{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700554 return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
556late_initcall(msm_pil_shutdown_at_boot);
557
Stephen Boyd6d67d252011-09-27 11:50:05 -0700558static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700560 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700561 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700562 mutex_destroy(&pil->lock);
563 kfree(pil);
564}
565
566struct pil_device *msm_pil_register(struct pil_desc *desc)
567{
568 int err;
569 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700570 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700571
Stephen Boyd36974ec2012-03-22 01:30:59 -0700572 /* Ignore users who don't make any sense */
573 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
574 "invalid proxy voting. ignoring\n"))
575 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
576
577 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700578 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700579 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700582 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700583 pil->owner = desc->owner;
584 pil->dev.parent = desc->dev;
585 pil->dev.bus = &pil_bus_type;
586 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587
Stephen Boyd36974ec2012-03-22 01:30:59 -0700588 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
589 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
590 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
591
Stephen Boyd6d67d252011-09-27 11:50:05 -0700592 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
593 err = device_register(&pil->dev);
594 if (err) {
595 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700596 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700597 mutex_destroy(&pil->lock);
598 kfree(pil);
599 return ERR_PTR(err);
600 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
Stephen Boyd6d67d252011-09-27 11:50:05 -0700602 err = msm_pil_debugfs_add(pil);
603 if (err) {
604 device_unregister(&pil->dev);
605 return ERR_PTR(err);
606 }
607
608 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700610EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
Stephen Boyd6d67d252011-09-27 11:50:05 -0700612void msm_pil_unregister(struct pil_device *pil)
613{
614 if (IS_ERR_OR_NULL(pil))
615 return;
616
617 if (get_device(&pil->dev)) {
618 mutex_lock(&pil->lock);
619 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700620 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700621 msm_pil_debugfs_remove(pil);
622 device_unregister(&pil->dev);
623 mutex_unlock(&pil->lock);
624 put_device(&pil->dev);
625 }
626}
627EXPORT_SYMBOL(msm_pil_unregister);
628
Stephen Boyd80bde032012-03-16 00:14:42 -0700629static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
630{
631 switch (event) {
632 case PM_SUSPEND_PREPARE:
633 down_write(&pil_pm_rwsem);
634 break;
635 case PM_POST_SUSPEND:
636 up_write(&pil_pm_rwsem);
637 break;
638 }
639 return NOTIFY_DONE;
640}
641
642static struct notifier_block pil_pm_notifier = {
643 .notifier_call = pil_pm_notify,
644};
645
Stephen Boyd6d67d252011-09-27 11:50:05 -0700646static int __init msm_pil_init(void)
647{
648 int ret = msm_pil_debugfs_init();
649 if (ret)
650 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700651 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700652 return bus_register(&pil_bus_type);
653}
654subsys_initcall(msm_pil_init);
655
656static void __exit msm_pil_exit(void)
657{
658 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700659 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700660 msm_pil_debugfs_exit();
661}
662module_exit(msm_pil_exit);
663
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664MODULE_LICENSE("GPL v2");
665MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");