blob: 0ecea851b12839bc7cc53c0e6f10f50da471f055 [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 Boyde4861c02012-05-14 12:57:40 -0700353 if (!pil->count) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700354 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 Boyde4861c02012-05-14 12:57:40 -0700360 pil->count++;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700361 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700363out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700365err_load:
366 mutex_unlock(&pil->lock);
367 pil_put(pil_d);
368err_depends:
369 put_device(&pil->dev);
370 module_put(pil->owner);
371 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372}
373EXPORT_SYMBOL(pil_get);
374
Stephen Boyd36974ec2012-03-22 01:30:59 -0700375static void pil_shutdown(struct pil_device *pil)
376{
377 pil->desc->ops->shutdown(pil->desc);
378 flush_delayed_work(&pil->proxy);
379 pil_set_state(pil, PIL_OFFLINE);
380}
381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382/**
383 * pil_put() - Inform PIL the peripheral no longer needs to be active
384 * @peripheral_handle: pointer from a previous call to pil_get()
385 *
386 * This doesn't imply that a peripheral is shutdown or in reset since another
387 * driver could be using the peripheral.
388 */
389void pil_put(void *peripheral_handle)
390{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700391 struct pil_device *pil_d, *pil = peripheral_handle;
392
393 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
396 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700397 if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
398 goto err_out;
Stephen Boyd36974ec2012-03-22 01:30:59 -0700399 if (!--pil->count)
400 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 mutex_unlock(&pil->lock);
402
Stephen Boyd3f4da322011-08-30 01:03:23 -0700403 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700404 module_put(pil->owner);
405 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700407 put_device(&pil_d->dev);
408 }
409 put_device(&pil->dev);
410 return;
411err_out:
412 mutex_unlock(&pil->lock);
413 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414}
415EXPORT_SYMBOL(pil_put);
416
417void pil_force_shutdown(const char *name)
418{
419 struct pil_device *pil;
420
421 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700422 if (!pil) {
423 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700425 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426
427 mutex_lock(&pil->lock);
428 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
Stephen Boyd36974ec2012-03-22 01:30:59 -0700429 pil_shutdown(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700431
Stephen Boyd6d67d252011-09-27 11:50:05 -0700432 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433}
434EXPORT_SYMBOL(pil_force_shutdown);
435
436int pil_force_boot(const char *name)
437{
438 int ret = -EINVAL;
439 struct pil_device *pil;
440
441 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700442 if (!pil) {
443 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700445 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
447 mutex_lock(&pil->lock);
448 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
449 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700450 if (!ret)
451 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700453 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
455 return ret;
456}
457EXPORT_SYMBOL(pil_force_boot);
458
459#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700460static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461{
462 filp->private_data = inode->i_private;
463 return 0;
464}
465
466static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
467 size_t cnt, loff_t *ppos)
468{
469 int r;
470 char buf[40];
471 struct pil_device *pil = filp->private_data;
472
473 mutex_lock(&pil->lock);
474 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
475 mutex_unlock(&pil->lock);
476 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
477}
478
479static ssize_t msm_pil_debugfs_write(struct file *filp,
480 const char __user *ubuf, size_t cnt, loff_t *ppos)
481{
482 struct pil_device *pil = filp->private_data;
483 char buf[4];
484
485 if (cnt > sizeof(buf))
486 return -EINVAL;
487
488 if (copy_from_user(&buf, ubuf, cnt))
489 return -EFAULT;
490
491 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700492 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 return -EIO;
494 } else if (!strncmp(buf, "put", 3))
495 pil_put(pil);
496 else
497 return -EINVAL;
498
499 return cnt;
500}
501
502static const struct file_operations msm_pil_debugfs_fops = {
503 .open = msm_pil_debugfs_open,
504 .read = msm_pil_debugfs_read,
505 .write = msm_pil_debugfs_write,
506};
507
508static struct dentry *pil_base_dir;
509
Stephen Boyd6d67d252011-09-27 11:50:05 -0700510static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511{
512 pil_base_dir = debugfs_create_dir("pil", NULL);
513 if (!pil_base_dir) {
514 pil_base_dir = NULL;
515 return -ENOMEM;
516 }
517
518 return 0;
519}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700520
521static void __exit msm_pil_debugfs_exit(void)
522{
523 debugfs_remove_recursive(pil_base_dir);
524}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
526static int msm_pil_debugfs_add(struct pil_device *pil)
527{
528 if (!pil_base_dir)
529 return -ENOMEM;
530
Stephen Boyd6d67d252011-09-27 11:50:05 -0700531 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
532 pil_base_dir, pil, &msm_pil_debugfs_fops);
533 return !pil->dentry ? -ENOMEM : 0;
534}
535
536static void msm_pil_debugfs_remove(struct pil_device *pil)
537{
538 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539}
540#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700541static int __init msm_pil_debugfs_init(void) { return 0; };
542static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700544static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545#endif
546
Stephen Boyd6d67d252011-09-27 11:50:05 -0700547static int __msm_pil_shutdown(struct device *dev, void *data)
548{
Stephen Boyd36974ec2012-03-22 01:30:59 -0700549 pil_shutdown(to_pil_device(dev));
Stephen Boyd6d67d252011-09-27 11:50:05 -0700550 return 0;
551}
552
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553static int msm_pil_shutdown_at_boot(void)
554{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700555 return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556}
557late_initcall(msm_pil_shutdown_at_boot);
558
Stephen Boyd6d67d252011-09-27 11:50:05 -0700559static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700561 struct pil_device *pil = to_pil_device(dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700562 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700563 mutex_destroy(&pil->lock);
564 kfree(pil);
565}
566
567struct pil_device *msm_pil_register(struct pil_desc *desc)
568{
569 int err;
570 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700571 struct pil_device *pil;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700572
Stephen Boyd36974ec2012-03-22 01:30:59 -0700573 /* Ignore users who don't make any sense */
574 if (WARN(desc->ops->proxy_unvote && !desc->ops->proxy_vote,
575 "invalid proxy voting. ignoring\n"))
576 ((struct pil_reset_ops *)desc->ops)->proxy_unvote = NULL;
577
578 pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700579 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700580 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581
582 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700583 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700584 pil->owner = desc->owner;
585 pil->dev.parent = desc->dev;
586 pil->dev.bus = &pil_bus_type;
587 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
Stephen Boyd36974ec2012-03-22 01:30:59 -0700589 snprintf(pil->wake_name, sizeof(pil->wake_name), "pil-%s", desc->name);
590 wake_lock_init(&pil->wlock, WAKE_LOCK_SUSPEND, pil->wake_name);
591 INIT_DELAYED_WORK(&pil->proxy, pil_proxy_work);
592
Stephen Boyd6d67d252011-09-27 11:50:05 -0700593 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
594 err = device_register(&pil->dev);
595 if (err) {
596 put_device(&pil->dev);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700597 wake_lock_destroy(&pil->wlock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700598 mutex_destroy(&pil->lock);
599 kfree(pil);
600 return ERR_PTR(err);
601 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
Stephen Boyd6d67d252011-09-27 11:50:05 -0700603 err = msm_pil_debugfs_add(pil);
604 if (err) {
605 device_unregister(&pil->dev);
606 return ERR_PTR(err);
607 }
608
609 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700611EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612
Stephen Boyd6d67d252011-09-27 11:50:05 -0700613void msm_pil_unregister(struct pil_device *pil)
614{
615 if (IS_ERR_OR_NULL(pil))
616 return;
617
618 if (get_device(&pil->dev)) {
619 mutex_lock(&pil->lock);
620 WARN_ON(pil->count);
Stephen Boyd36974ec2012-03-22 01:30:59 -0700621 flush_delayed_work_sync(&pil->proxy);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700622 msm_pil_debugfs_remove(pil);
623 device_unregister(&pil->dev);
624 mutex_unlock(&pil->lock);
625 put_device(&pil->dev);
626 }
627}
628EXPORT_SYMBOL(msm_pil_unregister);
629
Stephen Boyd80bde032012-03-16 00:14:42 -0700630static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
631{
632 switch (event) {
633 case PM_SUSPEND_PREPARE:
634 down_write(&pil_pm_rwsem);
635 break;
636 case PM_POST_SUSPEND:
637 up_write(&pil_pm_rwsem);
638 break;
639 }
640 return NOTIFY_DONE;
641}
642
643static struct notifier_block pil_pm_notifier = {
644 .notifier_call = pil_pm_notify,
645};
646
Stephen Boyd6d67d252011-09-27 11:50:05 -0700647static int __init msm_pil_init(void)
648{
649 int ret = msm_pil_debugfs_init();
650 if (ret)
651 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700652 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700653 return bus_register(&pil_bus_type);
654}
655subsys_initcall(msm_pil_init);
656
657static void __exit msm_pil_exit(void)
658{
659 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700660 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700661 msm_pil_debugfs_exit();
662}
663module_exit(msm_pil_exit);
664
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665MODULE_LICENSE("GPL v2");
666MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");