blob: 6f7f771cbaf77918a31fd41265ea789c5a654ac8 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include <asm/uaccess.h>
29#include <asm/setup.h>
Stephen Boyd6d67d252011-09-27 11:50:05 -070030#include <mach/peripheral-loader.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include "peripheral-loader.h"
33
Stephen Boyd20ad8102011-10-09 21:28:01 -070034enum pil_state {
35 PIL_OFFLINE,
36 PIL_ONLINE,
37};
38
39static const char *pil_states[] = {
40 [PIL_OFFLINE] = "OFFLINE",
41 [PIL_ONLINE] = "ONLINE",
42};
43
Stephen Boyd3f4da322011-08-30 01:03:23 -070044struct pil_device {
45 struct pil_desc *desc;
46 int count;
Stephen Boyd20ad8102011-10-09 21:28:01 -070047 enum pil_state state;
Stephen Boyd3f4da322011-08-30 01:03:23 -070048 struct mutex lock;
Stephen Boyd6d67d252011-09-27 11:50:05 -070049 struct device dev;
50 struct module *owner;
51#ifdef CONFIG_DEBUG_FS
52 struct dentry *dentry;
53#endif
Stephen Boyd3f4da322011-08-30 01:03:23 -070054};
55
Stephen Boyd6d67d252011-09-27 11:50:05 -070056#define to_pil_device(d) container_of(d, struct pil_device, dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
Stephen Boyd20ad8102011-10-09 21:28:01 -070058static ssize_t name_show(struct device *dev, struct device_attribute *attr,
59 char *buf)
60{
61 return snprintf(buf, PAGE_SIZE, "%s\n", to_pil_device(dev)->desc->name);
62}
63
64static ssize_t state_show(struct device *dev, struct device_attribute *attr,
65 char *buf)
66{
67 enum pil_state state = to_pil_device(dev)->state;
68 return snprintf(buf, PAGE_SIZE, "%s\n", pil_states[state]);
69}
70
71static struct device_attribute pil_attrs[] = {
72 __ATTR_RO(name),
73 __ATTR_RO(state),
74 { },
75};
76
Stephen Boyd6d67d252011-09-27 11:50:05 -070077struct bus_type pil_bus_type = {
78 .name = "pil",
Stephen Boyd20ad8102011-10-09 21:28:01 -070079 .dev_attrs = pil_attrs,
Stephen Boyd6d67d252011-09-27 11:50:05 -070080};
81
82static int __find_peripheral(struct device *dev, void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083{
Stephen Boyd6d67d252011-09-27 11:50:05 -070084 struct pil_device *pdev = to_pil_device(dev);
85 return !strncmp(pdev->desc->name, data, INT_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086}
87
88static struct pil_device *find_peripheral(const char *str)
89{
Stephen Boyd6d67d252011-09-27 11:50:05 -070090 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091
92 if (!str)
93 return NULL;
94
Stephen Boyd6d67d252011-09-27 11:50:05 -070095 dev = bus_find_device(&pil_bus_type, NULL, (void *)str,
96 __find_peripheral);
97 return dev ? to_pil_device(dev) : NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098}
99
100#define IOMAP_SIZE SZ_4M
101
102static int load_segment(const struct elf32_phdr *phdr, unsigned num,
103 struct pil_device *pil)
104{
Stephen Boydb0f1f802012-02-03 11:28:08 -0800105 int ret = 0, count, paddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 char fw_name[30];
107 const struct firmware *fw = NULL;
108 const u8 *data;
109
110 if (memblock_is_region_memory(phdr->p_paddr, phdr->p_memsz)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700111 dev_err(&pil->dev, "Kernel memory would be overwritten");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 return -EPERM;
113 }
114
115 if (phdr->p_filesz) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700116 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d",
117 pil->desc->name, num);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700118 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700120 dev_err(&pil->dev, "Failed to locate blob %s\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121 fw_name);
122 return ret;
123 }
124
125 if (fw->size != phdr->p_filesz) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700126 dev_err(&pil->dev, "Blob size %u doesn't match %u\n",
127 fw->size, phdr->p_filesz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 ret = -EPERM;
129 goto release_fw;
130 }
131 }
132
133 /* Load the segment into memory */
134 count = phdr->p_filesz;
135 paddr = phdr->p_paddr;
136 data = fw ? fw->data : NULL;
137 while (count > 0) {
138 int size;
139 u8 __iomem *buf;
140
141 size = min_t(size_t, IOMAP_SIZE, count);
142 buf = ioremap(paddr, size);
143 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700144 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 ret = -ENOMEM;
146 goto release_fw;
147 }
148 memcpy(buf, data, size);
149 iounmap(buf);
150
151 count -= size;
152 paddr += size;
153 data += size;
154 }
155
156 /* Zero out trailing memory */
157 count = phdr->p_memsz - phdr->p_filesz;
158 while (count > 0) {
159 int size;
160 u8 __iomem *buf;
161
162 size = min_t(size_t, IOMAP_SIZE, count);
163 buf = ioremap(paddr, size);
164 if (!buf) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700165 dev_err(&pil->dev, "Failed to map memory\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 ret = -ENOMEM;
167 goto release_fw;
168 }
169 memset(buf, 0, size);
170 iounmap(buf);
171
172 count -= size;
173 paddr += size;
174 }
175
Stephen Boydb0f1f802012-02-03 11:28:08 -0800176 if (pil->desc->ops->verify_blob) {
177 ret = pil->desc->ops->verify_blob(pil->desc, phdr->p_paddr,
Stephen Boyd3f4da322011-08-30 01:03:23 -0700178 phdr->p_memsz);
Stephen Boydb0f1f802012-02-03 11:28:08 -0800179 if (ret)
180 dev_err(&pil->dev, "Blob%u failed verification\n", num);
181 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
183release_fw:
184 release_firmware(fw);
185 return ret;
186}
187
188#define segment_is_hash(flag) (((flag) & (0x7 << 24)) == (0x2 << 24))
189
190static int segment_is_loadable(const struct elf32_phdr *p)
191{
192 return (p->p_type & PT_LOAD) && !segment_is_hash(p->p_flags);
193}
194
Stephen Boyd80bde032012-03-16 00:14:42 -0700195/* Sychronize request_firmware() with suspend */
196static DECLARE_RWSEM(pil_pm_rwsem);
197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198static int load_image(struct pil_device *pil)
199{
200 int i, ret;
201 char fw_name[30];
202 struct elf32_hdr *ehdr;
203 const struct elf32_phdr *phdr;
204 const struct firmware *fw;
205
Stephen Boyd80bde032012-03-16 00:14:42 -0700206 down_read(&pil_pm_rwsem);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700207 snprintf(fw_name, sizeof(fw_name), "%s.mdt", pil->desc->name);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700208 ret = request_firmware(&fw, fw_name, &pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700210 dev_err(&pil->dev, "Failed to locate %s\n", fw_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 goto out;
212 }
213
214 if (fw->size < sizeof(*ehdr)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700215 dev_err(&pil->dev, "Not big enough to be an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 ret = -EIO;
217 goto release_fw;
218 }
219
220 ehdr = (struct elf32_hdr *)fw->data;
221 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700222 dev_err(&pil->dev, "Not an elf header\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 ret = -EIO;
224 goto release_fw;
225 }
226
227 if (ehdr->e_phnum == 0) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700228 dev_err(&pil->dev, "No loadable segments\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 ret = -EIO;
230 goto release_fw;
231 }
Stephen Boyd96a9f902011-07-18 18:43:00 -0700232 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
233 sizeof(struct elf32_hdr) > fw->size) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700234 dev_err(&pil->dev, "Program headers not within mdt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 ret = -EIO;
236 goto release_fw;
237 }
238
Stephen Boyd3f4da322011-08-30 01:03:23 -0700239 ret = pil->desc->ops->init_image(pil->desc, fw->data, fw->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700241 dev_err(&pil->dev, "Invalid firmware metadata\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 goto release_fw;
243 }
244
Stephen Boydc9753e12011-07-13 17:58:48 -0700245 phdr = (const struct elf32_phdr *)(fw->data + sizeof(struct elf32_hdr));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
247 if (!segment_is_loadable(phdr))
248 continue;
249
250 ret = load_segment(phdr, i, pil);
251 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700252 dev_err(&pil->dev, "Failed to load segment %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253 i);
254 goto release_fw;
255 }
256 }
257
Stephen Boyd3f4da322011-08-30 01:03:23 -0700258 ret = pil->desc->ops->auth_and_reset(pil->desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 if (ret) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700260 dev_err(&pil->dev, "Failed to bring out of reset\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 goto release_fw;
262 }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700263 dev_info(&pil->dev, "brought %s out of reset\n", pil->desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
265release_fw:
266 release_firmware(fw);
267out:
Stephen Boyd80bde032012-03-16 00:14:42 -0700268 up_read(&pil_pm_rwsem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 return ret;
270}
271
Stephen Boyd20ad8102011-10-09 21:28:01 -0700272static void pil_set_state(struct pil_device *pil, enum pil_state state)
273{
274 if (pil->state != state) {
275 pil->state = state;
276 sysfs_notify(&pil->dev.kobj, NULL, "state");
277 }
278}
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280/**
281 * pil_get() - Load a peripheral into memory and take it out of reset
282 * @name: pointer to a string containing the name of the peripheral to load
283 *
284 * This function returns a pointer if it succeeds. If an error occurs an
285 * ERR_PTR is returned.
286 *
287 * If PIL is not enabled in the kernel, the value %NULL will be returned.
288 */
289void *pil_get(const char *name)
290{
291 int ret;
292 struct pil_device *pil;
293 struct pil_device *pil_d;
294 void *retval;
295
Stephen Boyd6d67d252011-09-27 11:50:05 -0700296 if (!name)
297 return NULL;
298
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 pil = retval = find_peripheral(name);
300 if (!pil)
301 return ERR_PTR(-ENODEV);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700302 if (!try_module_get(pil->owner)) {
303 put_device(&pil->dev);
304 return ERR_PTR(-ENODEV);
305 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306
Stephen Boyd6d67d252011-09-27 11:50:05 -0700307 pil_d = pil_get(pil->desc->depends_on);
308 if (IS_ERR(pil_d)) {
309 retval = pil_d;
310 goto err_depends;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 }
312
313 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700314 if (!pil->count++) {
315 ret = load_image(pil);
316 if (ret) {
317 retval = ERR_PTR(ret);
318 goto err_load;
319 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 }
Stephen Boyd20ad8102011-10-09 21:28:01 -0700321 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700323out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 return retval;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700325err_load:
326 mutex_unlock(&pil->lock);
327 pil_put(pil_d);
328err_depends:
329 put_device(&pil->dev);
330 module_put(pil->owner);
331 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332}
333EXPORT_SYMBOL(pil_get);
334
335/**
336 * pil_put() - Inform PIL the peripheral no longer needs to be active
337 * @peripheral_handle: pointer from a previous call to pil_get()
338 *
339 * This doesn't imply that a peripheral is shutdown or in reset since another
340 * driver could be using the peripheral.
341 */
342void pil_put(void *peripheral_handle)
343{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700344 struct pil_device *pil_d, *pil = peripheral_handle;
345
346 if (IS_ERR_OR_NULL(pil))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348
349 mutex_lock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700350 if (WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
351 goto err_out;
Stephen Boyd20ad8102011-10-09 21:28:01 -0700352 if (!--pil->count) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700353 pil->desc->ops->shutdown(pil->desc);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700354 pil_set_state(pil, PIL_OFFLINE);
355 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 mutex_unlock(&pil->lock);
357
Stephen Boyd3f4da322011-08-30 01:03:23 -0700358 pil_d = find_peripheral(pil->desc->depends_on);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700359 module_put(pil->owner);
360 if (pil_d) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 pil_put(pil_d);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700362 put_device(&pil_d->dev);
363 }
364 put_device(&pil->dev);
365 return;
366err_out:
367 mutex_unlock(&pil->lock);
368 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369}
370EXPORT_SYMBOL(pil_put);
371
372void pil_force_shutdown(const char *name)
373{
374 struct pil_device *pil;
375
376 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700377 if (!pil) {
378 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 return;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700380 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381
382 mutex_lock(&pil->lock);
383 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
Stephen Boyd3f4da322011-08-30 01:03:23 -0700384 pil->desc->ops->shutdown(pil->desc);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700385 pil_set_state(pil, PIL_OFFLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 mutex_unlock(&pil->lock);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700387
Stephen Boyd6d67d252011-09-27 11:50:05 -0700388 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389}
390EXPORT_SYMBOL(pil_force_shutdown);
391
392int pil_force_boot(const char *name)
393{
394 int ret = -EINVAL;
395 struct pil_device *pil;
396
397 pil = find_peripheral(name);
Stephen Boyd01c02c12012-03-14 10:12:26 -0700398 if (!pil) {
399 pr_err("%s: Couldn't find %s\n", __func__, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 return -EINVAL;
Stephen Boyd01c02c12012-03-14 10:12:26 -0700401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402
403 mutex_lock(&pil->lock);
404 if (!WARN(!pil->count, "%s: Reference count mismatch\n", __func__))
405 ret = load_image(pil);
Stephen Boyd20ad8102011-10-09 21:28:01 -0700406 if (!ret)
407 pil_set_state(pil, PIL_ONLINE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 mutex_unlock(&pil->lock);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700409 put_device(&pil->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
411 return ret;
412}
413EXPORT_SYMBOL(pil_force_boot);
414
415#ifdef CONFIG_DEBUG_FS
Stephen Boyd6d67d252011-09-27 11:50:05 -0700416static int msm_pil_debugfs_open(struct inode *inode, struct file *filp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417{
418 filp->private_data = inode->i_private;
419 return 0;
420}
421
422static ssize_t msm_pil_debugfs_read(struct file *filp, char __user *ubuf,
423 size_t cnt, loff_t *ppos)
424{
425 int r;
426 char buf[40];
427 struct pil_device *pil = filp->private_data;
428
429 mutex_lock(&pil->lock);
430 r = snprintf(buf, sizeof(buf), "%d\n", pil->count);
431 mutex_unlock(&pil->lock);
432 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
433}
434
435static ssize_t msm_pil_debugfs_write(struct file *filp,
436 const char __user *ubuf, size_t cnt, loff_t *ppos)
437{
438 struct pil_device *pil = filp->private_data;
439 char buf[4];
440
441 if (cnt > sizeof(buf))
442 return -EINVAL;
443
444 if (copy_from_user(&buf, ubuf, cnt))
445 return -EFAULT;
446
447 if (!strncmp(buf, "get", 3)) {
Stephen Boyd3f4da322011-08-30 01:03:23 -0700448 if (IS_ERR(pil_get(pil->desc->name)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 return -EIO;
450 } else if (!strncmp(buf, "put", 3))
451 pil_put(pil);
452 else
453 return -EINVAL;
454
455 return cnt;
456}
457
458static const struct file_operations msm_pil_debugfs_fops = {
459 .open = msm_pil_debugfs_open,
460 .read = msm_pil_debugfs_read,
461 .write = msm_pil_debugfs_write,
462};
463
464static struct dentry *pil_base_dir;
465
Stephen Boyd6d67d252011-09-27 11:50:05 -0700466static int __init msm_pil_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467{
468 pil_base_dir = debugfs_create_dir("pil", NULL);
469 if (!pil_base_dir) {
470 pil_base_dir = NULL;
471 return -ENOMEM;
472 }
473
474 return 0;
475}
Stephen Boyd6d67d252011-09-27 11:50:05 -0700476
477static void __exit msm_pil_debugfs_exit(void)
478{
479 debugfs_remove_recursive(pil_base_dir);
480}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482static int msm_pil_debugfs_add(struct pil_device *pil)
483{
484 if (!pil_base_dir)
485 return -ENOMEM;
486
Stephen Boyd6d67d252011-09-27 11:50:05 -0700487 pil->dentry = debugfs_create_file(pil->desc->name, S_IRUGO | S_IWUSR,
488 pil_base_dir, pil, &msm_pil_debugfs_fops);
489 return !pil->dentry ? -ENOMEM : 0;
490}
491
492static void msm_pil_debugfs_remove(struct pil_device *pil)
493{
494 debugfs_remove(pil->dentry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495}
496#else
Stephen Boyd6d67d252011-09-27 11:50:05 -0700497static int __init msm_pil_debugfs_init(void) { return 0; };
498static void __exit msm_pil_debugfs_exit(void) { return 0; };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499static int msm_pil_debugfs_add(struct pil_device *pil) { return 0; }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700500static void msm_pil_debugfs_remove(struct pil_device *pil) { }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501#endif
502
Stephen Boyd6d67d252011-09-27 11:50:05 -0700503static int __msm_pil_shutdown(struct device *dev, void *data)
504{
505 struct pil_device *pil = to_pil_device(dev);
506 pil->desc->ops->shutdown(pil->desc);
507 return 0;
508}
509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510static int msm_pil_shutdown_at_boot(void)
511{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700512 return bus_for_each_dev(&pil_bus_type, NULL, NULL, __msm_pil_shutdown);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513}
514late_initcall(msm_pil_shutdown_at_boot);
515
Stephen Boyd6d67d252011-09-27 11:50:05 -0700516static void pil_device_release(struct device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517{
Stephen Boyd6d67d252011-09-27 11:50:05 -0700518 struct pil_device *pil = to_pil_device(dev);
519 mutex_destroy(&pil->lock);
520 kfree(pil);
521}
522
523struct pil_device *msm_pil_register(struct pil_desc *desc)
524{
525 int err;
526 static atomic_t pil_count = ATOMIC_INIT(-1);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700527 struct pil_device *pil = kzalloc(sizeof(*pil), GFP_KERNEL);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700528
Stephen Boyd3f4da322011-08-30 01:03:23 -0700529 if (!pil)
Stephen Boyd6d67d252011-09-27 11:50:05 -0700530 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531
532 mutex_init(&pil->lock);
Stephen Boyd3f4da322011-08-30 01:03:23 -0700533 pil->desc = desc;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700534 pil->owner = desc->owner;
535 pil->dev.parent = desc->dev;
536 pil->dev.bus = &pil_bus_type;
537 pil->dev.release = pil_device_release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538
Stephen Boyd6d67d252011-09-27 11:50:05 -0700539 dev_set_name(&pil->dev, "pil%d", atomic_inc_return(&pil_count));
540 err = device_register(&pil->dev);
541 if (err) {
542 put_device(&pil->dev);
543 mutex_destroy(&pil->lock);
544 kfree(pil);
545 return ERR_PTR(err);
546 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
Stephen Boyd6d67d252011-09-27 11:50:05 -0700548 err = msm_pil_debugfs_add(pil);
549 if (err) {
550 device_unregister(&pil->dev);
551 return ERR_PTR(err);
552 }
553
554 return pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
Stephen Boyd3f4da322011-08-30 01:03:23 -0700556EXPORT_SYMBOL(msm_pil_register);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
Stephen Boyd6d67d252011-09-27 11:50:05 -0700558void msm_pil_unregister(struct pil_device *pil)
559{
560 if (IS_ERR_OR_NULL(pil))
561 return;
562
563 if (get_device(&pil->dev)) {
564 mutex_lock(&pil->lock);
565 WARN_ON(pil->count);
566 msm_pil_debugfs_remove(pil);
567 device_unregister(&pil->dev);
568 mutex_unlock(&pil->lock);
569 put_device(&pil->dev);
570 }
571}
572EXPORT_SYMBOL(msm_pil_unregister);
573
Stephen Boyd80bde032012-03-16 00:14:42 -0700574static int pil_pm_notify(struct notifier_block *b, unsigned long event, void *p)
575{
576 switch (event) {
577 case PM_SUSPEND_PREPARE:
578 down_write(&pil_pm_rwsem);
579 break;
580 case PM_POST_SUSPEND:
581 up_write(&pil_pm_rwsem);
582 break;
583 }
584 return NOTIFY_DONE;
585}
586
587static struct notifier_block pil_pm_notifier = {
588 .notifier_call = pil_pm_notify,
589};
590
Stephen Boyd6d67d252011-09-27 11:50:05 -0700591static int __init msm_pil_init(void)
592{
593 int ret = msm_pil_debugfs_init();
594 if (ret)
595 return ret;
Stephen Boyd80bde032012-03-16 00:14:42 -0700596 register_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700597 return bus_register(&pil_bus_type);
598}
599subsys_initcall(msm_pil_init);
600
601static void __exit msm_pil_exit(void)
602{
603 bus_unregister(&pil_bus_type);
Stephen Boyd80bde032012-03-16 00:14:42 -0700604 unregister_pm_notifier(&pil_pm_notifier);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700605 msm_pil_debugfs_exit();
606}
607module_exit(msm_pil_exit);
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609MODULE_LICENSE("GPL v2");
610MODULE_DESCRIPTION("Load peripheral images and bring peripherals out of reset");